[protobuf] Re: ProtocolBuffer repeated tasks in Java..
Kenton, Thanks for your support! I have a situation to create 1000's of objects inside loop, I am bit worried cause those manny builder objects are also created. is there any workaround for that? Mohan On Dec 13 2009, 5:44 am, Kenton Varda ken...@google.com wrote: 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.comprotobuf%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.
Re: [protobuf] Re: ProtocolBuffer repeated tasks in Java..
On Tue, Feb 2, 2010 at 12:54 AM, Mohan mohan.narayanasw...@credit-suisse.com wrote: I have a situation to create 1000's of objects inside loop, I am bit worried cause those manny builder objects are also created. is there any workaround for that? With a modern JVM, creating short-lived objects should be cheap -- like, as cheap as stack allocation. -- 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.
[protobuf] Re: ProtocolBuffer repeated tasks in Java..
Thanks for clearing that up, looks like we'll have some cleanup to do next time we upgrade... On Dec 12, 3:44 pm, Kenton Varda ken...@google.com wrote: 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.comprotobuf%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.
Re: [protobuf] Re: ProtocolBuffer repeated tasks in Java..
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.comprotobuf%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.