8

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;
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900

1 Answers1

6

protobuf-net will never call your Clear() method itself, but for simple cases you can simply do this yourself, and use the Merge method (on the v1 API, or just pass the object into Deserialize in the v2 API). For example:

MyDTO obj = new MyDTO();
for(...) {
    obj.Clear();
    Serializer.Merge(obj, source);        
}

This loads the data into the existing obj rather 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 MyDTO such as:

// this can also accept serialization-context parameters if
// you want to pass your pool in, etc
public static MyDTO Create()
{
    // try to get from the pool; only allocate new obj if necessary
    return SomePool.GetMyDTO() ?? new MyDTO();
}

and, at app-startup, configure protobuf-net to know about it:

RuntimeTypeModel.Default[typeof(MyDTO)].SetFactory("Create");

(SetFactory can also accept a MethodInfo - 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 from Merge.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Hi Marc, what happens if I do not implement the Clear() method? Will all fields get overwritten anyway? – driAn May 13 '14 at 15:07
  • 1
    @driAn only fields with data in the inbound stream will be assigned. If you use a custom factory that recycles instances, you should provide your own reset code inside the factory. If you aren't using a custom factory this is not an issue – Marc Gravell May 13 '14 at 17:41
  • Hi Marc, I'm looking for the aforementioned method Deserialize() with the object instance to fill/merge. But I didn't found it. I find only one method: Serializer.Deserialize(Stream). Is there still a way to specify the object instance to fill ? I would like to reuse objects from an object pool and that way seems to be easier to deal with than the other way with a custom factory... Thanks in advance. – Eric Boumendil Jan 29 '15 at 09:51
  • @Eric on the v1 API (`Serializer.*`) the method you want is `Merge`. On the v2 API (`TypeModel`), there is a `Deserialize` overload. – Marc Gravell Jan 29 '15 at 10:11
  • Thanks a lot. I was using the static class Serializer instead of TypeModel! – Eric Boumendil Jan 29 '15 at 10:19
  • Is it possible to SetFactory when I'm using precompiled serializers? – Michal Dobrodenka Dec 27 '18 at 09:44