Skip to content

Conversation

@BrianXu0623
Copy link
Owner

@BrianXu0623 BrianXu0623 commented Sep 22, 2025

Summary

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

This PR adds support for lazy zero segement allocation strategy to pycapnp.

  • Introduces BuilderOptions (lazyZeroSegment, skipZeroData).
  • MessageBuilder and struct new_message APIs now accept these options.
  • Adds an example (examples/lazy_zero_allocation.py) to demonstrate usage.

This allows users to fine-tune memory allocation behavior for performance or special environments.
No breaking changes; default behavior is unchanged.

Example Usage

class Allocator:
    def __init__(self):
        return

    def __call__(self, minimum_size: int) -> bytearray:
        # dirty the memory on purpose, to simulate reusing shared memory
        return bytearray(random.getrandbits(8) for _ in range(minimum_size * WORD_SIZE))


# Set lazyZeroSegment and skipZeroData to True
lazy_zero = capnp.LazyZeroSegmentAlloc(enableLazyZero=True, skipLazyZeroTypes={capnp.types.Data})
builder_options=capnp.BuilderOptions(lazyZeroSegmentAlloc=lazy_zero)
# Create a new Person message with custom allocation options
person = addressbook_capnp.Person.new_message(allocate_seg_callable=Allocator(), builder_options=builder_options)
print(person.name) # guaranteed empty string "" 
person.init("extraData", 100)
print(bytes(person.extraData)) # random bytes

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.

2 participants