On Sat, Dec 12, 2009 at 11:21 AM, atkretsch <atkret...@gmail.com> wrote:
> You can reuse the builder object by clearing it after each iteration > like so: > No you can't. This coincidentally appeared to work in earlier versions but it was never supposed to work according to the docs. In 2.2.0 it throws an exception. We were actually going to introduce an optimization to automatically freelist the builders, which would have possibly lead to data corruption for people who tried to clear and reuse builders illegally. We ended up giving up on the optimization for other reasons. > > Person.Builder builder = Person.newBuilder(); > for(...) { > builder.clear(); > builder.setFname(...); > builder.setLName(...); > builder.setEmail(...) > myResponseBuilder.addPerson(builder.build()); > } > > This is a bit more efficient since you're only creating one new object > per iteration instead of two. If you're creating a lot of these > objects or this loop is in a critical path, this could be a decent > speedup. > > On Dec 11, 12:35 pm, Programmer 09 <macster2...@gmail.com> wrote: > > I am new to ProtocolBuffer and wanted to verify that I am doing the > > right thing here: > > > > I have proto definitions as: > > > > Person { > > required string fname =1; > > required string lname = 2; > > required string email = 3; > > > > } > > > > MyResponse { > > repeated Person persons = 1; > > > > } > > > > What is the most effecient way to generate a myResponse with 100 > > persons? Is this effecieint? > > > > // assume I have a array of hashmap of person data. > > // there is the myResponseBuilder which is a builder for MyResponse. > > > > for (int i = 0; i < 100; i++) > > Person.Builder personBuilder = Person.newBuilder(); > > personBuilder.setFname(nameFromMap); > > personBuilder.setLname(nameFromMap); > > personBuilder.setEmail(emailFromMap); > > > > myResponseBuilder.addPerson(personBuilder); > > > > } > > > > Is this the right way to do this? Or is there a better effecient way > > instead of creating a builder for each person data? Any pointers > > appreciated! Thanks! > > -- > > 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<protobuf%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > > -- 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.