Frame a record into a buffer that lives longer than the record - #735
Merged
Conversation
Every append built the payload, then allocated a second buffer to hold the payload plus about ten
bytes of frame and copied it in. That second allocation is the whole record again, on every write.
The comparable design keeps one long-lived operation-log message and one reusable buffer on its
logger rather than minting them per write. This does the same for the frame: the buffer lives on
the log's inner state, `encode_record_into` clears it and refills it, and its capacity survives.
value 64 B: 34.0 allocations an append -> 33.0, 9,201 B -> 9,096
value 4096 B: 33.9, 29,390 B -> 25,271 (-14%)
The count drops by one; the bytes drop by roughly the size of the record, so the saving grows with
the payload. That is the shape you want here -- the cost it removes is the one that scales.
The buffer is taken with `mem::take` and put back on the success path, so the rest of the inner
state stays usable while it is borrowed. An early return drops it and the next append allocates
once, which is correct, just not free that once.
`encode_record` is unchanged for its other callers; it now allocates a buffer and delegates.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
bjmeetsfo
force-pushed
the
mx/reuse-the-frame-buffer
branch
from
September 3, 2026 17:01
cc70b5f to
c664369
Compare
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.
Every append built the payload, then allocated a second buffer to hold the payload plus about ten
bytes of frame, and copied it in. That second allocation is the whole record again, on every write.
The design this is measured against keeps one long-lived operation-log message and a reusable
buffer on its logger rather than minting them per write. This does the same for the frame: the
buffer lives on the log's inner state,
encode_record_intoclears and refills it, and its capacitysurvives.
The count drops by one; the bytes drop by roughly the size of a record, so the saving grows with
the payload -- which is the right shape, since it removes the cost that scales.
The buffer is taken with
mem::takeand returned on the success path, so the rest of the innerstate stays usable while it is borrowed. An early return drops it and the next append allocates
once: correct either way, just not free that once.
encode_recordis unchanged for its othercallers and now allocates a buffer and delegates.
distributed_raft_chunks_large_sequence_add_under_default_entry_limitfailed once in the localfull suite and is not from this: it passes 3/3 in isolation on this branch and 3/3 on main, and it
failed the same way on an unrelated branch earlier in the week.