Everything you say sounds right.  If you want to know precisely what these
methods do, or how much overhead they incur, I encourage you to look at the
code in repeated_field.h -- it's not very complicated.
However, instead of trying to control all this in detail, it's usually
easiest to simply make sure that you reuse your protobuf objects.  If you
parse a message, clear it, and then parse another message into the same
object, it will reuse the memory for sub-objects and such which was
allocated the first time.  This way it's easy to make sure you aren't doing
it wrong.

On Tue, Jun 16, 2009 at 9:40 AM, RobertFach

> Hi,
> is there anybody out there who can tell me how the special methods
> like addcleared(), etc. for repeated field ptr work?
> I guess I can somehow improve/avoid the allocation overhead, right?
> Say, I have the following messages,
> message test {
>  repeated chunk = 1;
> }
> message chunk {
>  int value = 1;
> }
> The goal is to reduce the memory allocations by using pre-allocated
> objects.
> Assume the chunkPtr points to an already allocated object, if I use
> test.chunk().addAllocated(chunkPtr), does it just attaches the
> chunkPtr to the reaptedFieldPtr chunk object? Is there any overhead
> incured by dynamically adding the chunks to the test message? Can I
> deal with that issue by using the test.chunk().Reserve(xx) method?
> It seems also that I can maintain a pool of objects by using the
> addCleared() method, right? This way, future calls to test.chunk().add
> () will use an element from the pool?
> I was just wondering about these questions while reading the
> documentation... maybe you can just tell me if this usage pattern
> would be ok.
> Thx for the library and thx for the support
> Cheers Robert
> >

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to