Skip to content

Lazy zero segment allocation strategy - #1

Open
BrianXu0623 wants to merge 20 commits into
v2from
lazy_zero_segment_allocation_strategy
Open

Lazy zero segment allocation strategy#1
BrianXu0623 wants to merge 20 commits into
v2from
lazy_zero_segment_allocation_strategy

Conversation

@BrianXu0623

@BrianXu0623 BrianXu0623 commented Sep 22, 2025

Copy link
Copy Markdown
Owner

This PR is for resolving the following issue:
Skip zeroing for guaranteed-overwritten data in SegmentAllocator #2400

Summary

Introduce an opt-in BuilderOptions::LazyZeroSegmentAlloc that lets callers/allocators avoid pre-zeroing entire segments. By default Cap’n Proto requires allocators (including overridden allocateSegment) to return zero-initialized memory. When a caller opts in via BuilderOptions::LazyZeroSegmentAlloc and uses a custom/overridden allocateSegment, 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 unnecessary memset work for very large buffers (e.g., big DATA blobs).

Important: A message builder with custom/overridden allocateSegment must be used together with BuilderOptions::LazyZeroSegmentAlloc. When opted-in, the allocator no longer needs to call memset(0) on the whole segment — Cap’n Proto will perform lazy zeroing and will skip configured areas.

Key changes

  • Added BuilderOptions with a nested LazyZeroSegmentAlloc struct to control lazy zeroing.
  • Introduced SegmentBuilder::doLazyZeroSegment() to handle conditional zeroing based on type and options.
  • Modified memory allocation paths in BuilderArena and WireHelpers to use the new lazy zeroing logic.
  • Added validation logic to ensure only supported types are skipped for zeroing.
  • Exposed new MessageBuilder methods to set and get builder options.
  • Future extension: extend to other field types (large lists, text, nested containers) or to per-field control (by fieldId / fieldName) so callers can mark specific fields to skip lazy-zero.

Example

// Example usage of a custom `MessageBuilder` that overrides the `allocateSegment` method,
// and enables lazy zero segment allocation for DATA type.
BuilderOptions options;
options.lazyZeroSegmentAlloc.enableLazyZero = true;
options.lazyZeroSegmentAlloc.skipLazyZeroTypes.insert(schema::Type::DATA);
MyCustomMessageBuilder builder(options);

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.

image

Safety

  • Users must ensure correctness for lazily-zeroed fields: if a lazily-skipped region is read before being fully overwritten, the read may return random/dirty (uninitialized) memory — avoid reading skipped regions until you have fully written them.

@BrianXu0623
BrianXu0623 force-pushed the lazy_zero_segment_allocation_strategy branch from 8f01d5d to 3a818a3 Compare September 30, 2025 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant