Context: this is based on a question that was asked and then deleted before I could answer it – but I think it is a good question, so I’ve tidied it, rephrased it, and re-posted it.
In a high-throughput scenario using protobuf-net, where lots of allocations are a problem (in particular for GC), is it possible to re-use objects? For example by adding a Clear() method?
[ProtoContract]
public class MyDTO
{
[ProtoMember(1)]
public int Foo { get; set; }
[ProtoMember(2)]
public string Bar { get; set; }
[ProtoMember(3, DataFormat = DataFormat.Group)]
public List<int> Values { get { return values; } }
private readonly List<int> values = new List<int>();
public void Clear()
{
values.Clear();
Foo = 0;
Bar = null;
}
}
protobuf-net will never call your
Clear()method itself, but for simple cases you can simply do this yourself, and use theMergemethod (on the v1 API, or just pass the object intoDeserializein the v2 API). For example:This loads the data into the existing
objrather than creating a new object each time.In more complex scenarios where you want to reduce the number of object allocations, and are happy to handle the object pooling / re-use yourself, then you can use a custom factory. For example, you can add a method to
MyDTOsuch as:and, at app-startup, configure protobuf-net to know about it:
(
SetFactorycan also accept aMethodInfo– useful if the factory method is not declared inside the type in question)With this, what should happen is the factory method is used instead of the usual construction mechanisms. It remains, however, entirely your job to cleanse (
Clear()) the objects when you are finished with them, and to return them to your pool. What is particularly nice about the factory approach is that it will work for new sub-items in lists, etc, which you can’t do just fromMerge.