Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Protocol-buffers reuse a string pointer when parsing?

I'm working with Protocol Buffers, and trying to minimize calls to the heap.

In the example, PhoneNumber has a required member called number which is a string.

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

When I generate the code I get this pointer member in Person_PhoneNumber:

::std::string* number_;

So I'm wondering if this string is going to be reused when I call parseFrom on an already existing Person_PhoneNumber

I had a look at the generated code, but it's hard to decode, especially considering all the calls to GetEmptyStringAlreadyInited and the SharedCtor

like image 244
0x26res Avatar asked Oct 19 '25 15:10

0x26res


1 Answers

Yes, if you reuse the same message object for multiple parses, all sub-objects will be reused as well. In fact, the Clear() method never deletes anything, but just marks everything for reuse. The only way to cause any memory to be freed is to actually destroy the top-level object.

This is a key performance feature, but it can sometimes lead to undesirable memory usage if you parse messages with many different "shapes", since the total memory footprint will end up being the union of all those "shapes". You can use the SpaceUsed() method to determine how much memory is currently being used by a message and free objects that get too big.

like image 111
Kenton Varda Avatar answered Oct 21 '25 05:10

Kenton Varda