(Java) Message.Builder build()/clear()

461 views
Skip to first unread message

pwr

unread,
Aug 19, 2009, 6:00:23 PM8/19/09
to Protocol Buffers
Hello,

In 2.1, one could build() multiple objects in sequence, with the same
builder object, by clear()ing it repeatedly.
In 2.2, attempting this throws an exception -- from what I can tell a
builder can only be used to create a single PB object.

What's the rationale behind this decision?

Cheers,
-pwr

Kenton Varda

unread,
Aug 19, 2009, 6:09:30 PM8/19/09
to pwr, Protocol Buffers
On Wed, Aug 19, 2009 at 3:00 PM, pwr <daniel...@gmail.com> wrote:
In 2.1, one could build() multiple objects in sequence, with the same
builder object, by clear()ing it repeatedly.

No, you couldn't, at least according to the docs.
 
In 2.2, attempting this throws an exception -- from what I can tell a
builder can only be used to create a single PB object.

What's the rationale behind this decision?

As the docs say, once you call build(), the builder is no longer usable.  The fact that it seemed to work with 2.1.0 and earlier was a mistake, and officially allowing this behavior would limit our ability to implement certain features and optimizations in the future.  In particular there was an optimization involving putting builders on a freelist which seemed to improve performance on Android -- though this was removed just before the 2.2.0 release due to other problems.

Sorry for the inconvenience.  However, note that there is very little to gain by reusing a single builder object in this way, since the object is very small (just a pointer).  Allocating a new builder for every iteration should not significantly harm performance.

pwr

unread,
Aug 19, 2009, 6:21:13 PM8/19/09
to Protocol Buffers


On Aug 20, 1:09 am, Kenton Varda <ken...@google.com> wrote:
> On Wed, Aug 19, 2009 at 3:00 PM, pwr <daniel.pa...@gmail.com> wrote:
> > In 2.1, one could build() multiple objects in sequence, with the same
> > builder object, by clear()ing it repeatedly.
>
> No, you couldn't, at least according to the docs.

Weeeell, you could. Or I found myself into a few lucky corner
cases :).

> As the docs say, once you call build(), the builder is no longer usable.

I have to admit I didn't read the docs that thoroughly. They do say
so.

>  The fact that it seemed to work with 2.1.0 and earlier was a mistake, and
> officially allowing this behavior would limit our ability to implement
> certain features and optimizations in the future.  In particular there was
> an optimization involving putting builders on a freelist which seemed to
> improve performance on Android -- though this was removed just before the
> 2.2.0 release due to other problems.
>
> Sorry for the inconvenience.  However, note that there is very little to
> gain by reusing a single builder object in this way, since the object is
> very small (just a pointer).  Allocating a new builder for every iteration
> should not significantly harm performance.

Noted.

Thanks for the reply,
-pwr
Reply all
Reply to author
Forward
0 new messages