Home > Blockchain >  Protobuf-net IsPacked=true and DataFormat = DataFormat.Group for list/array data of primitive types
Protobuf-net IsPacked=true and DataFormat = DataFormat.Group for list/array data of primitive types

Time:10-15

As explained here, using "group" mode on the collections makes serialization faster and I was wondering whether is good to use it also for list/array data of primitive types packed, like in the below sample class.

[ProtoContract]
public class Foo
{
    [ProtoMember(1, IsPacked = true, DataFormat = DataFormat.Group)]
    public float[] Numbers { get; set; }
}

I'm using protobuf-net 2.4.0.

CodePudding user response:

Group encoding simply doesn't apply here. Group encoding is specifically for sub-objects - in simple terms it is akin to using { and } sentinels to start/end a JSON sub-object, instead of saying "the next 542 bytes are the sub-object" - using the sentinels just requires adding known values, instead of first having to calculate that the object you haven't yet serialized needs 542 bytes.

In the case of float, they are always encoded as a fixed size (32-bit in this case) value. The distinction between packed and not-packed here is best shown by writing 5 values; without packed, it will say "the next value is field 1, fixed-32 (followed by 4 bytes)", 5 times (for a total of 25 bytes); where-as with packed encoding it will say "the next value is field 1, a string of length 20 (followed by 20 bytes)" (simply by doing 5 items x 4 bytes) for a total of 22 bytes. This difference gets more significant for large collections, although there are also some processing benefits in not having to parse multiple field headers, etc. In many cases, "packed" is a bit more automatic than you might think, so you may find that it already uses packed (as long as you don't specify Packed=false)

  • Related