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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With