Hi, I haven't actually used the Java protobuf API, but it seems to me from the quick occasional glance that this isn't entirely true. I mean, specifically in response to the code snippet posted in the original message, I would possibly:
1. Reuse the Builder object by calling its clear() method. This would save from the need to create a new Builder object for each iteration of the outermost loop. 2. Iterate over the repeated field using the get*Count() and get* (index) methods instead of the get*List() method. I'm not sure if this would save anything, but depending on how things are implemented in the generated code, this could save from allocating a new List object. Also, might "bytes" type fields perform better than any "string" type fields that you may have in your particular data set? I'm not sure, but it might be worth benchmarking. On Jul 18, 9:22 pm, Kenton Varda <ken...@google.com> wrote: > On Fri, Jul 17, 2009 at 8:13 PM, Alex Black <a...@alexblack.ca> wrote: > > > When I write out messages using C++ I'm careful to clear messages and > > re-use them, is there something equivalent on the java side when > > reading those same messages in? > > No. Sorry. This just doesn't fit at all with the Java library's design, > and even if it did, you cannot reuse Java String objects, which often > account for most of the memory usage. However, memory allocation is cheaper > in Java than in C++, so there's less to gain from it. > > > > > My code looks like: > > > CodedInputStream stream = CodedInputStream.newInstance(inputStream); > > > while ( !stream.isAtEnd() ) > > { > > MyMessage.Builder builder = MyMessage.newBuilder(); > > stream.readMessage(builder, null); > > MyMessage myMessage = builder.build(); > > > for ( MessageValue messageValue : myMessage.getValuesList() ) > > { > > ...... > > } > > } > > > I'm passing 150 messages each with 1000 items, so presumably memory is > > allocated 150 times for each of the messages... > > > - Alex --~--~---------~--~----~------------~-------~--~----~ 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 protobuf+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/protobuf?hl=en -~----------~----~----~----~------~----~------~--~---