toBuilder() makes only a shallow copy of the object. It does not copy the contents of individual Strings, ByteStrings, sub-messages, etc.; it just reuses the existing values. This means that it's generally reasonably cheap, although obviously not quite as cheap as if the builders for sub-objects were kept around by the parent. I'd suggest doing tests to see if this is a problem for your app before trying to change protocol buffers.
On Wed, Dec 16, 2009 at 5:37 PM, Mark <mjsc...@gmail.com> wrote: > Yes, this seems nice: > > Foo.Builder builder; > builder.setBar(builder.getBar().toBuilder().setA(...).build()); > > But I guess I'm a little worried about its hidden cost if I use it a > lot. If message Bar was very large, then would converting it to a > builder, setting one value, and converting it back to a message again > incur a large cost? That is, is the time cost of setting one of a > builder's sub-message's values proportional to the total size of the > sub-message? That would be quite different than the cost of setting > one of a builder's values, which is constant. > > If I know that a message of type Foo contains some Foo-specific > information, then I'd want to be able to write code that reads a > message of type Foo and gives me back that information. Like in your > example, I know that a Foo message has some "a" information and some > "b" information. I could write a method GetA(Foo) : int32 ... well, > Option<int32> anyway. (It might be null.) Then if I later decided to > move the field "a" from Bar to Foo I wouldn't have to change client > code of GetA(Foo) at all. It would be just as fast, too, because there > really isn't any difference between msg.getBar().getA() and msg.getA > (). I can do this right now, which is great. > > But writing is a different story. Let's say I moved "a" from Bar to > Foo. I know that I can write to a builder of type Foo.Builder some "a" > information and some "b" information. I could write a method SetA > (Foo.Builder, int32) : unit that just invokes the setA method in > Foo.Builder. But then, if I decided to move "a" back to Bar, then my > implementation of SetA(Foo.Builder, int32) : unit has to change a lot. > I might have to convert Foo's bar from a message to a builder, set "a" > in it, and rebuild the updated Bar message. That doesn't change the > type signature of SetA but it might have a big effect on the cost of > calling the method. > > If builders could contain builders, however, then I'd know for sure > that I could change where "a" lives without significantly affecting > the performance of methods like SetA! > > And I do those kinds of changes to my protocol a lot--I'm always > rearranging how information relates to each other as my applications > evolve. Is it just me? > > On Dec 16, 11:55 am, Jason Hsueh <jas...@google.com> wrote: > > You should be able to pass a builder to some method, and have the method > > modify some value of the builder just fine. > > > > Modifying a value in a sub-message is a bit more inconvenient, but still > > doable. If you have some type Bar in type Foo like: > > message Bar { > > optional int32 a = 1; > > optional int32 b = 2; > > > > } > > > > message Foo { > > optional Bar bar = 1; > > > > } > > > > Foo.Builder builder; > > builder.setBar(builder.getBar().toBuilder().setA(...).build()); > > > > Would that work for you? > > > > > > > > On Wed, Dec 16, 2009 at 9:24 AM, Mark <mjsc...@gmail.com> wrote: > > > I wish I could pass a builder object to a method and have the method > > > modify either a value of the builder or a value of a sub-message in > > > the builder! > > > > > I came across this thread, which described exactly the problem I have. > > > The Car/Engine example in the thread is perfectly illustrative of my > > > scenario. > > > > >http://groups.google.com/group/protobuf/browse_thread/thread/16997910. > .. > > > > > Has anyone else been in this same situation? What have you done to > > > ameliorate the problem? > > > > > -- > > > > > 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> > <protobuf%2bunsubscr...@googlegroups.c om> > > > . > > > 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<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.