Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

VictorNicollet
Copy link
Contributor

@VictorNicollet VictorNicollet commented Jul 18, 2025

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 using Write (which uses the smallest possible number of bytes for its argument).

Union type keys set with 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 using `Write` (which uses the smallest possible
number of bytes for its argument).
@VictorNicollet
Copy link
Contributor Author

VictorNicollet commented Jul 18, 2025 via email

@VictorNicollet VictorNicollet marked this pull request as ready for review July 18, 2025 13:34
@AArnott AArnott merged commit 65e5b0f into MessagePack-CSharp:master Aug 17, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants