Skip to content

Capnp message growing unbounded when reusing Builder to save on allocation #523

Open
@gtsui

Description

@gtsui

In my code below, I want to reuse my capnp message object because I dont want to have to allocate new memory on every cycle of my loop. However, when I run it this way, the size of my message object seems to grow and grow.

I'm sure there must be some way to have capnp write at the beginning of my message rather than appending to it, or clear it, but I'm not sure how? Am I missing something simple?

fn main() -> Result<(), anyhow::Error> {
    let mut message = capnp::message::Builder::new(HeapAllocator::new().first_segment_words(128));
    let mut buffer: Vec<u8> = Vec::with_capacity(1024);
    for i in 0..100 {
        buffer.clear();
        let mut builder = message.init_root::<book_capnp::book::Builder>();
        builder.set_author("Mark Twain");
        builder.set_title("Huckleberry Finn");
        builder.set_pages(400);
        capnp::serialize::write_message(&mut buffer, &message)?;
        println!("{:?}", buffer.len());
        println!("{:?}", message.size_in_words());
        println!("=====");
    }
    Ok(())
}

Output:

80
9
=====
144
17
=====
//...
7104
886
=====

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions