I have a fairly old version of the protobuf library, so if this has
been changed let me know, but I have a situation where Message::Clear
() is causing my cpu to go to like 70% for an extended period of time.

It's also possible this is user error, so please correct me if that's
the case.

Basically what I have is a top level message with a bunch of optional
messages, which I send across the wire.

One of these optional messages is defined as follows:

message DataChunkList {
    required bool             is_end_of_list = 1;
    repeated DataChunk  data = 2;

message DataChunk {
    optional bytes    data = 1;
    //Other fields here

The "data" field will almost always be exactly 4k, and I will usually
not want to send 1 chunk at a time, but a list of around 32 at a

So I save an instance of the top level message in the class containing
my sending code, and right before I'm about to send data I do the

net::DataChunkList* pChunks = m_CachedTopLevel.mutable_data_chunk_list

//Should already be clear, but just in case
prevCount = pChunks->mutable_data()->ClearedCount();

for (int i=prevCount; i < num_chunks; ++i)
    net::DataChunk* pChunk = new net::DataChunk();

for (int i=0; i < num_chunks; ++i)
   net::DataChunk* pChunk = pChunks->mutable_data()->ReleaseCleared();
   pChunk->mutable_data()->assign(global_4k_buffer, 4096);



I ran a profiler on my code, and the very last line  (the Clear())
takes up almost 95% of the CPU usage for the function, and the
function takes up about about 30% of the CPU usage of the entire app.
So obviously this is a big problem.

The comment on the code says that clear "does not free any memory"
however.  So why could it be using so much CPU?  Am I misunderstanding
the purpose / usage of these methods?  What I'm trying to do is just
re-use a pool of 4k buffers for all of these sends.
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