capnproto-rust icon indicating copy to clipboard operation
capnproto-rust copied to clipboard

Document that Builders cannot be re-used?

Open FrancisRussell opened this issue 5 years ago • 5 comments

From the documentation, it's unclear if Builders can be re-used. The presence of init_root() gives the impression that a Builder can be re-initialized for construction of a new message. Given that one wishes to avoid repeated memory allocations, this seems like an obvious thing to do, but attempting to do so results in increasingly larger messages (presumably all the previous messages concatenated, but I've not checked this in depth).

FrancisRussell avatar Nov 07 '18 16:11 FrancisRussell

Yep, the init_ methods clear out the old data, but don't reclaim the old space. More documentation about this would be good.

dwrensha avatar Nov 08 '18 01:11 dwrensha

Does this mean, as appeared to be the case, that a Builder cannot be re-used? If so, is this behaviour deliberate?

FrancisRussell avatar Nov 08 '18 10:11 FrancisRussell

You can reuse a message::Builder, but the usefulness of doing so is limited because you will continue to accumulate unused memory that can only be reclaimed by dropping the message::Builder.

The best way to avoid allocations between messages is to use message::ScratchSpaceHeapAllocator. That allows you to specify a buffer that will be used for the first segment of your message and can be reused between messages.

dwrensha avatar Nov 08 '18 12:11 dwrensha