You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
=====
The text was updated successfully, but these errors were encountered:
gtsui
changed the title
Capnp message growing unbounded
Capnp message growing unbounded when reusing Builder to save on allocation
Oct 7, 2024
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?
Output:
The text was updated successfully, but these errors were encountered: