-
Notifications
You must be signed in to change notification settings - Fork 1
Adding support for shallow hashes #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,3 +120,91 @@ def visit_enum(self, root: mir.Enum) -> str: | |
| pairs = sorted([(value, name) for name, value in root.variants.items()]) | ||
| variants = ",".join([f"{value}={name}" for value, name in pairs]) | ||
| return f"Enum(name={root.name},underlying={root.underlying_type.accept(self)},variants={{{variants}}})" | ||
|
|
||
| ## This is used to construct the shallow hash. | ||
| @dataclasses.dataclass | ||
| class ShallowReprStr( | ||
| mir.RootTypeVisitor[str], | ||
| mir.VariantVisitor[str], | ||
| mir.SeqTypeVisitor[str], | ||
| mir.LengthVisitor[str], | ||
| ): | ||
| types: t.Dict[QName, mir.RootType] | ||
|
|
||
| def visit_int(self, type_: mir.Int) -> str: | ||
| return f"Int(width={type_.width},sign={type_.sign.name},endianness={type_.endianness.name})" | ||
|
|
||
| def visit_float(self, type_: mir.Float) -> str: | ||
| return f"Float(width={type_.width},endianness={type_.endianness.name})" | ||
|
|
||
| def repr_field_reference(self, fr: mir.FieldReference) -> str: | ||
| return f"FieldReference(name={fr.name})" | ||
|
|
||
| def visit_seq(self, type_: mir.Seq) -> str: | ||
| # Note that the repr is done on the type before seq reduce | ||
| # -- there is only Seq, but not List, Vector, or Array. | ||
| # This is deliberate - the representation of the type is the intended to | ||
| # be a representation that is as simple as possible, but conveys everything | ||
| # needed to parse or serialize a type from a wire representation. | ||
| # List, Vector, and Array do not impact the wire representation, and hence | ||
| # are not used. | ||
| length = type_.length.accept(self) | ||
| return f"Seq(inner={type_.inner.accept(self)},length={length})" | ||
|
|
||
| def visit_unbound_seq(self, type_: mir.UnboundSeq) -> str: | ||
| raise InternalError() | ||
|
|
||
| def visit_fixed_length(self, length: mir.FixedLength) -> str: | ||
| return f"{length.length}" | ||
|
|
||
| def visit_variable_length(self, length: mir.VariableLength) -> str: | ||
| return self.repr_field_reference(length.length) | ||
|
|
||
| def visit_detached_variant(self, type_: mir.DetachedVariant) -> str: | ||
| variant = type_.variant.resolve(self.types) | ||
| return f"DetachedVariant(variant={variant.accept(self)},tag={self.repr_field_reference(type_.tag)})" | ||
|
|
||
| def visit_virtual(self, type_: mir.Virtual) -> str: | ||
| # Virtual types contribute to the hash just like normal types, even though they have no | ||
| # effect on the wire representation. They allow a type to represent that there is | ||
| # some other data on the wire related to it -- virtual fields are parsed | ||
| # using the context of the rest of the type. | ||
| return f"Virtual(inner={type_.inner.accept(self)})" | ||
|
|
||
| def visit_ref(self, type_: mir.Ref) -> str: | ||
| return type_.resolve(self.types).accept(self) | ||
|
|
||
| def visit_struct(self, root: mir.Struct) -> str: | ||
| # Including the name of the fields is critical -- otherwise the struct Foo { x: int, y: int } | ||
| # is the same as the struct Foo { y: int, x: int }. | ||
| # Names provide meaning to fields. | ||
| # Note that no derrived information, like the size or offset of fields is included. That could all | ||
| # be computed from this and is extraneous. | ||
| l = [] | ||
| for name, type_ in root.fields.items(): | ||
| if isinstance(type_, mir.DetachedVariant) and type_.is_hash_tag: | ||
| l.append(f"{name}=HashVariant") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This crucially prevents recursion and keeps the hash variants shallow?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep!
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's still important to include the type, but that's exactly right: "i dont need to include the inner types in the hash b/c access is protected by the hash" |
||
| else: | ||
| l.append(f"{name}={type_.accept(self)}") | ||
| fields = ",".join(l) | ||
| return f"Struct(name={root.name},fields={{{fields}}})" | ||
|
|
||
| def visit_variant(self, root: mir.Variant) -> str: | ||
| return root.accept_v(self) | ||
|
|
||
| def visit_fixed_variant(self, root: mir.FixedVariant) -> str: | ||
| # Sort the variant by tag to ensure order doesn't matter | ||
| pairs = sorted([(tag, sr) for sr, tag in root.tags.items()]) | ||
| variants = ",".join( | ||
| [f"{tag}={value.resolve(self.types).accept(self)}" for tag, value in pairs] | ||
| ) | ||
| return f"Variant(name={root.name},tag_type={root.tag_type.accept(self)},variants={{{variants}}})" | ||
|
|
||
| def visit_hash_variant(self, root: mir.HashVariant) -> str: | ||
| raise InternalError() | ||
|
|
||
| def visit_enum(self, root: mir.Enum) -> str: | ||
| # Like variant | ||
| pairs = sorted([(value, name) for name, value in root.variants.items()]) | ||
| variants = ",".join([f"{value}={name}" for value, name in pairs]) | ||
| return f"Enum(name={root.name},underlying={root.underlying_type.accept(self)},variants={{{variants}}})" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| [](const ::test_types::bakery::v4::Message&) { return static_cast<::std::uint32_t>(UINT32_C(2458638600)); } | ||
|
|
||
| This is the hash of a v4 message, which inside has a hash variant. Hopefully it changes when i add to the inner HV | ||
|
|
||
| [](const ::test_types::bakery::v4::Message&) { return static_cast<::std::uint32_t>(UINT32_C(3267984003)); } | ||
|
|
||
| Indeed it does. | ||
|
|
||
| I made ShallowReprHash. This might not be the most efficient way to do it but initially I just want to see it preserve the hashes | ||
|
|
||
| [](const ::test_types::bakery::v4::Message&) { return static_cast<::std::uint32_t>(UINT32_C(2458638600)); } | ||
|
|
||
| Seems like it's back to the old hash when I remove the new sprinkle_quantity from the cupcakeorder, great. | ||
|
|
||
| Ok that was more work than expected, but before: | ||
|
|
||
| 5 [](const ::test_types::bakery::v4::Message&) { return static_cast<::std::uint32_t>(UINT32_C(390615788)); } | ||
|
|
||
| after changing a field on a struct only present in an HV | ||
| 5 [](const ::test_types::bakery::v4::Message&) { return static_cast<::std::uint32_t>(UINT32_C(390615788)); } | ||
|
|
||
| after changing a field on a struct elsewhere in v4 | ||
| ::std::uint32_t tag() const { | ||
| 1 return match( | ||
| 2 [](const ::test_types::bakery::v2::Message&) { return static_cast<::std::uint32_t>(UINT32_C(3831964682)); }, | ||
| 3 [](const ::test_types::bakery::v1::Message&) { return static_cast<::std::uint32_t>(UINT32_C(2782154402)); }, | ||
| 4 [](const ::test_types::bakery::v3::Message&) { return static_cast<::std::uint32_t>(UINT32_C(716972100)); }, | ||
| 5 [](const ::test_types::bakery::v4::Message&) { return static_cast<::std::uint32_t>(UINT32_C(1691428639)); } | ||
| 6 ); | ||
|
|
||
| after changing a field on a struct elsewhere in v2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the purpose of this so that we can differentiate between the two?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it's either this or a field on variantref