#2400·capnproto

Skip zeroing for guaranteed-overwritten data in SegmentAllocator

Author: BrianXu0623Created Sep 1, 2025Updated May 8, 2026

Background

I'm implementing a custom MessageBuilder::SegmentAllocator function to allocate segments within shared memory and populate them in place. And I'm transferring AVFrame as a Cap’n Proto message, which consists of roughly 99.9% video frame blob data (schema::type::DATA) and about 0.1% other metadata fields.

I found that Cap’n Proto requires developers to zero-initialize the allocated memory before returning the segment to the arena. However, zeroing a 5.93 MB 1080P AVFrame takes around 29.5 µs, while zeroing only the 5 KB of metadata fields takes about 0.7 µs (measured on my local device).

In my use case, zeroing the video frame data is not necessary because I can guarantee that it will be overwritten later. I think this is a common use case in multimedia processing, and possibly in other scenarios as well, where the message is primarily composed of a large Data or List field that is guaranteed to be overwritten.

Question

Does Cap’n Proto provide options for users, for skipping zeroing of non-essential memory regions? I understand that this could be achieved if users carefully control the memory layout before allocation, but that would require a complete understanding of Cap’n Proto’s wire format. It would be ideal if there's other simple and flexible ways to achieve this.

I previously did a patch to implement a LazyZero approach, which allows developer to choose not to zero out memory during the AllocateSegment stage. Instead, the necessary segment space is lazily zeroed as the message is being constructed, while certain types/fields can be skipped from zeroing.

I’d like to know if there are already better approaches supported, or if you have any suggestions. Thank you!