Is the intention of the C++ CodedStreams that they are "cheap" and so I should use one per message? Or should I reuse one if I have a large sequence of messages to read/write? For writing, I've been using a single CodedOutputStream, something like:

CodedOutputStream out(...)
for (message in long sequence) {
  out.WriteVarint32((int32_t) message.ByteSize());
  message.SerializeToCodedStream(&out);
}

This works fine. However, doing the same on input does not work, because I run into the total byte limit. In Java, I call resetSizeCounter() to avoid this problem. Is there a reason a similar function does not exist for the C++ side? I'm working around this by moving the CodedInputStream inside my loop, which is fine, but seems sub-optimal. At the very least, since I have lots of small messages, this means my ZeroCopyInputStream's methods get called many times.


Related question: If I am writing length-prefixed values, what is the "best" way to do this? I think there are 3 approaches:

1. out.WriteVarint32(msg.ByteSize()); msg.SerializeToCodedStream(&out);

This approach will choose between the "ToArray" and "regular" versions of the Serialize method, as appropriate. However, it computes the ByteSize twice.

2. out.WriteVarint32(msg.ByteSize()); msg.SerializeWithCachedSizes(&out);

This computes the ByteSize once, but never uses the "fast path" ToArray method.

3. Some custom code that calls ByteSize once, then chooses between the "fast" and "slow" methods, as appropriate. Is there actually any significant difference in performance between SerializeWithCachedSizes and SerializeWithCachedSizesToArray, so it would be worth me actually doing this?

Thanks,

Evan

--
Evan Jones
http://evanjones.ca/

--
You received this message because you are subscribed to the Google Groups "Protocol 
Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to