Reuse Builder buffer for zero-alloc tight-loop encoding - #9
Merged
Conversation
Add Builder::reset(), reserve(size_t) and capacity() so a single Builder can encode many varying blobs in a hot loop while reusing one heap allocation instead of mallocing each iteration. - reset() rewinds via std::vector::clear() (keeps capacity); paired with the existing zero-copy buf_data()/buf_size() accessors, the loop does zero per-iteration allocations after warm-up. - reserve(size_t) pre-grows the buffer; capacity() inspects it. - build() still moves the buffer out for one-shot ownership handoff. - Removed the dead private reserve(size_t) helper (never called). Tests: new test_builder_buffer_reuse proves reset keeps capacity, reserve grows it, and a 100-iteration loop reuses the same allocation (stable data pointer + capacity) while producing correct varying blobs. Unit tests 320 -> 632 passing; fuzz corpus and C++<->SQLite interop tests green. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a buffer-reuse API to
msgpack::Builderso a single Builder can encode many varying blobs in a tight loop while reusing one heap allocation — no per-iterationmalloc.Motivation
Builder::build()moves the internal buffer into the returnedBlob, so a Builder built and discarded each iteration allocates a fresh buffer every time. Hot serialization loops (sockets, files, DB rows) paid amalloc/freeper blob.API
Builder& reset() noexceptBuilder& reserve(size_t bytes)size_t capacity() const noexceptreset()is built onstd::vector::clear(), which keeps capacity. Combined with the existing zero-copybuf_data()/buf_size()accessors, the loop performs zero allocations after warm-up.build()is unchanged and still hands off ownership for one-shot use.Also removes the dead private
reserve(size_t)helper (never called) to free the name for the public capacity-reserve.Tests
test_builder_buffer_reuse()provesreset()keeps capacity,reserve()grows it,reset()chains, and a 100-iteration loop reuses the same allocation (stablebuf_data()pointer + capacity) while producing correct varying blobs.-Wall -Wextra.Docs
cpp/README.md: new "Buffer reuse (tight loops)" section + usage example; updated low-level accessors and Finalize notes.cpp/README.mdand rootREADME.md.