model: fix iobuf over-reservation for null key/value records - #30980
Merged
WillemKauf merged 1 commit intoJul 1, 2026
Merged
Conversation
`append_record_to_buffer()` reserved `key_size() + value_size()` bytes before writing a record. For a record whose key and value are both absent those sizes are `-1` each, so the sum `-2` underflows the size_t argument of `iobuf::reserve_memory()` to ~2^64. reserve_memory() then trims the current fragment and allocates a fresh max-size (128 KiB) fragment for every such record. This is especially problematic for cloud topics, since we call: | `encode_placeholder_batch()` \-> `builder::add_raw_kv()` (for `N` records) \-> `builder::add_raw_kw()` \-> `model::append_record_to_buffer()`
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a performance/pathological-allocation issue in model::append_record_to_buffer() where null key/value sizes (-1) could underflow when summed and be passed to iobuf::reserve_memory(), causing large fragment allocations on hot paths (notably cloud topics placeholder encoding).
Changes:
- Clamp key/value reservation sizes to non-negative values before calling
iobuf::reserve_memory(). - Avoid
size_tunderflow on tombstone / null key+value records during record encoding.
andrwng
approved these changes
Jul 1, 2026
WillemKauf
enabled auto-merge
July 1, 2026 04:10
Collaborator
CI test resultstest results on build#86566 |
Collaborator
|
/backport v26.1.x |
| append_vint_to_iobuf(a, r.offset_delta()); | ||
|
|
||
| a.reserve_memory(r.key_size() + r.value_size()); | ||
| auto key_bytes = std::max(r.key_size(), 0); |
Member
There was a problem hiding this comment.
Why do these methods return -1 and not 0?
Contributor
Author
There was a problem hiding this comment.
-1 is used to distinguish a key/value in a record that is empty (std::nullopt) versus having a key/value that has a length of 0 ("").
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.
append_record_to_buffer()reservedkey_size() + value_size()bytes before writing a record. For a record whose key and value are both absent those sizes are-1each, so the sum-2underflows the size_t argument ofiobuf::reserve_memory()to ~2^64.reserve_memory()then allocates 128 KiB fragments for every record (which is eventually trimmed back down to size).This is especially problematic for cloud topics, since we call:
This seems to mean potentially many (tens? hundreds?) of megabytes allocated and freed on the L0 write path for no reason.
Fix the bug by clamping reserved bytes to 0.
From a test, not checked in:
https://gist.github.com/WillemKauf/1090815d4e566165a0fe1e548ed877f2
Backports Required
Release Notes
Improvements