Use 'Write' instead of 'WriteInt32' for union type keys #2212
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Union type keys are usually small positive integers, likely to fit in a single byte. As late as version 2.5.198, the library would encode [Union(0, typeof(Foo))] as two bytes 0x92 0x00 (one for "two-element array", and one for the 0 integer key), followed by the serialization of type Foo.
In version 3.1.4, [Union(0, typeof(Foo))] is instead encoded as six bytes 0x92 0xD2 0x00 0x00 0x00 0x00 (one for "two-element array", one for "32-bit integer", and the four bytes of the 0 integer key). Although the two representations are compatible, for code bases that use many small polymorphic objects, the migration from 2.x to 3.x would lead to a significant increase in the size of the serialized data.
This behavior is caused by the generated code for the interface using
WriteInt32
(which always encodes the integer as five bytes), and is fixed by instead usingWrite
(which uses the smallest possible number of bytes for its argument).