Using protobuf-net, is it possible to deserialize a message without allocating memory?

I have a C # application that needs to deserialize many thousands of protocols per second. In the interest of avoiding unnecessary garbage collections, I wonder if there is a way to use pre-allocated memory so that each deserialization operation does not require allocating new memory.

I assume that I should allocate a pool of message objects before executing it, and then instruct the protobuff code to use the next available message from this pool for each deserialization.

Does this functionality exist or is there another way to optimize memory usage in this scenario?

Thank!

+5
source share
1 answer

, ! -, , , GC , factory; , :

RuntimeTypeModel.Default.Add(typeof (Foo), true).SetFactory(factory);

factory :

  • static Foo (.. "CreateFoo"), Foo
  • MethodInfo static ( Foo), Foo

, , , . :

public static Foo CreateFoo() {
    return GetFromYourOwnMicroPool();
}

, , factory reset ; protobuf-net . , protobuf-net - , .

​​ , GC ( ... , p)

: , , protobuf-net struct ; , / , - struct s.

+10

All Articles