J4N
J4N

Reputation: 20727

What is the performance impact of using AsReferenceDefault =true on ProtoBuf

I'm migrating from the DataContractSerializer in favor of ProtoBuf. One thing that we did previously noticed is that there was a huge impact by using the IsReference =true of the DataContract with the XML serializer.

I would like to know if there is some reference somewhere about what happens we use the the AsReferenceDefault = true of the ProtoContract.

What are the impact on the speed of serialization/deserialization? What are the impacts on what is serialized?

Side question: Is it mandatory that all reference to one object have the IsReference = true, or can I have the main one(the one that really own the object, the place I would expect to have the object serialized) with a IsReference = false and still have only one instance of the same object?

Upvotes: 2

Views: 292

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

This flag changes a number of things:

  • when serializing, it means that for every object marked in this way, it needs to check a reference lookup to see if it has already seen it; this shouldn't be particularly slow, but it is extra work that needs to happen
  • it then needs to write an extra layer of abstraction - essentially a wrapper object that includes either the pre-existing object id, or the new object id plus the object payload (this is shaped as a NetObjectProxy from bcl.proto, for the curious - with field 1 (existing object), or fields 2 and 10 (new objects) set); this will also take a little space per reference (but less space than serializing the object repeatedly)
  • this extra layer is slightly awkward (but not impossible) for other implementations to consume; essentially the core protobuf APIs have no concept of object identity, so this is a library-specific addition; if you're just using protobuf-net you should be fine

As for how that adds up in reality: it will depend on the scenario, and I'd love to hear your findings.

Re where you need AsReference: if it isn't specified via AsReferenceDefault, it will need to be specified on all members where you expect reference tracking to occur. The check is applied at the individual member level, essentially of the form (although this isn't the actual code):

bool asRef = member.AsReferenceSpecified()
    ? member.AsReference : metaType.AsReferenceDefault;

Upvotes: 1

Related Questions