Lazy zero segment allocation strategy - #1
Open
BrianXu0623 wants to merge 20 commits into
Open
Conversation
added 3 commits
September 1, 2025 01:00
This was referenced Sep 17, 2025
added 16 commits
September 25, 2025 22:23
BrianXu0623
force-pushed
the
lazy_zero_segment_allocation_strategy
branch
from
September 30, 2025 04:12
8f01d5d to
3a818a3
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.
This PR is for resolving the following issue:
Skip zeroing for guaranteed-overwritten data in SegmentAllocator #2400
Summary
Introduce an opt-in
BuilderOptions::LazyZeroSegmentAllocthat lets callers/allocators avoid pre-zeroing entire segments. By default Cap’n Proto requires allocators (including overriddenallocateSegment) to return zero-initialized memory. When a caller opts in viaBuilderOptions::LazyZeroSegmentAllocand uses a custom/overriddenallocateSegment, the allocator may return an unzeroed segment and Cap’n Proto takes responsibility for zeroing during message construction: it will lazily zero the parts that must be zeroed and skip the regions explicitly configured to be safe to leave uninitialized. This moves the zeroing responsibility from the allocator to the builder in an explicit, opt-in way and reduces unnecessarymemsetwork for very large buffers (e.g., bigDATAblobs).Important: A message builder with custom/overridden
allocateSegmentmust be used together withBuilderOptions::LazyZeroSegmentAlloc. When opted-in, the allocator no longer needs to callmemset(0)on the whole segment — Cap’n Proto will perform lazy zeroing and will skip configured areas.Key changes
BuilderOptionswith a nestedLazyZeroSegmentAllocstruct to control lazy zeroing.SegmentBuilder::doLazyZeroSegment()to handle conditional zeroing based on type and options.BuilderArenaandWireHelpersto use the new lazy zeroing logic.MessageBuildermethods to set and get builder options.fieldId/fieldName) so callers can mark specific fields to skip lazy-zero.Example
Verification & testing
In a media-processing validation using an AVFrame-style Cap’n Proto message, skipping pre-zero for a 1080p frame raw-bytes DATA field (~5.93 MB) reduced message-construction zeroing cost from ~29.5 µs to ~0.7 µs in my measurement.
Safety