You should do the following instead:
1. Create a CodedOutputStream wrapping your OutputStream.
2. Use CodedOutputStream.writeVarInt32 to write the size
3. use msg.writeTo() to write it. It will look something like:
CodedOutputStream out = CodedOutputStream.newInstance(outputStream);
for ( ... ) {
out.writeVarInt32(msg.getSerializedSize());
msg.writeTo(out);
}
out.flush();
This should avoid a whole ton of extra allocations/deallocations that
are being done by your current approach. If you try this, please let
me know what the performance numbers look like.
Evan
--
Evan Jones
http://evanjones.ca/
Hm. It is a little interesting that it would use so much more memory
than using Java serializable ...
> Protocol Buffers (CodedOutputStream, but no flush() after each write):
> 76,600 msgs/sec
This is what I was attempting to describe (one flush() after writing
all messages). This matches my expectations that this should be a
little faster, which is nice.
Hm. It is a little interesting that it would use so much more memorythan using Java serializable ...