How to allocate compound objects from a pool in Google.Protobuf C#

528 views Asked by At

I'm using the Google.Protobuf 3.2.0 implementation in C#. I'm trying to make it allocate objects from a pool. The reason for doing this is that they need to be passed via a queue to another thread for processing, and I want my application to be garbage-free in steady state running.

This is easy enough for a simple object.

E.g. with the following proto file:

syntax = "proto3"
message SimpleMessage {
    int32 number = 1;
}

I can implement a custom parser:

var parser = new MessageParser<SimpleMessage>(() => 
             {
                 // allocate from pool
                 return pool.GetObject();
             });
parser.ParseDelimitedFrom(stream)

It will call the delegate every time it needs a new object, so I can implement a pool.

But how do I do this for a compound object, with nested members?

syntax = "proto3"
message CompoundMessage {
    oneof Alternatives {
        SimpleMessage1 simple1 = 1;
        SimpleMessage2 simple2 = 2;
    }
}

I can create the top level custom parser for CompoundMessage, but how do I change the parser (or factory) it uses for creating nested objects?

0

There are 0 answers