Support uninitialised fields for skipping pre-zeroing - #2
Open
BrianXu0623 wants to merge 9 commits into
Open
Conversation
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.
Refers to issue capnproto#2400
Replaces capnproto#2423
Motivation
As discussed in issue capnproto#2400 and the previous PR capnproto#2423, the current memory allocation strategy (relying strictly on
callocor pre-zeroed memory) incurs a significant performance penalty for large allocations, specifically for largeDATAblobs.When a user allocates a large buffer (e.g., for file uploads or large serialized payloads) using the standard
initData(), the memory is zeroed out by the allocator, only to be immediately overwritten by the user. This "double-write" (zeroing + copying) consumes unnecessary memory bandwidth and CPU cycles.Proposed Changes
This PR introduces a non-invasive, opt-in mechanism to support Lazy Zeroing. Instead of fundamentally changing the allocator interface for all users, this change allows
MallocMessageBuilder(and custom builders) to switch allocation strategies via a new configuration enum.Key Changes
MessageBuilderBase Class Update:virtual bool needLazyZero() const. By default, it returnsfalse, maintaining the existing behavior (allocators must return zeroed memory).true, it changes the allocation contract:allocateSegment()is permitted to return uninitialized (dirty) memory.MessageBuildertakes responsibility for proactively zero-initializing Structs, Fields and Pointer Tables, ensuring safety.MallocMessageBuilderUpdate:InitializationStrategyenum:PRE_ZERO_MEMORY(Default): Usescalloc, strictly preserves legacy behavior and safety guarantees.LAZY_ZERO_MEMORY: Usesmalloc, returns dirty memory to avoid redundant OS-level zeroing.MallocMessageBuilderconstructors to accept this new strategy.New API for DATA Fields:
uninitializedData()(oruninitializedFoogenerated methods) forDATAtype fields.InitializationStrategy::LAZY_ZERO_MEMORY, this avoids the "double-write" penalty while automatically ensuring that alignment padding bytes remain zeroed for security.Usage Example
Safety & Correctness
This PR strictly adheres to the safety requirements discussed in the previous PR to prevent information leaks or undefined behavior:
MessageBuilderlogic takes full responsibility for zero-initializing struct fields and pointer tables upon allocation. This guarantees deterministic behavior and valid pointers even when the underlying segment contains "dirty" memory.DATAblobs where zeroing is skipped (viauninitializedFoo), the logic strictly enforces zeroing of alignment padding bytes. This ensures that residual heap data does not leak into the wire format.InitializationStrategyremainsPRE_ZERO_MEMORY. Existing codebases will see no behavioral change and will continue to receive zero-filled segments from the allocator by default.