Skip to content

Conversation

@JKearnsl
Copy link
Member

optimize heap

@JKearnsl
Copy link
Member Author

JKearnsl commented Jun 10, 2025

The following test demonstrates the problem well

#[test]
fn buffer_reuse() {
    let pool = BufferPool::new(Some(214748));
    let mut results = vec![(0u128, 0u128); 214748];
    let mut buffers= Vec::new();
    for i in 0..214748 {
        let mut buf = pool.alloc().unwrap();
        buf.write(&[i as u8; 9000]).unwrap();
        buffers.push(buf);
    }
    for i in buffers.into_iter() {
        pool.release(i);
    }
    
    let mut start = std::time::Instant::now();
    for i in 0..214748 {
        start = std::time::Instant::now();
        let mut buf = pool.alloc().unwrap();
        results[i] = (results[i].0, start.elapsed().as_nanos());
        pool.release(buf);
    }

    for i in 0..214748 {
        start = std::time::Instant::now();
        let buf = Vec::<u8>::with_capacity(9000);
        results[i] = (start.elapsed().as_nanos(), results[i].1);
    }
    for (alloc_time, reuse_time) in results {
        if alloc_time > 1000 || reuse_time > 1000 {
            panic!("Allocation or reuse took too long: alloc={}ns, reuse={}ns", alloc_time, reuse_time);
        }
    }
}

Panic:

warning: `client` (lib test) generated 3 warnings (run `cargo fix --lib -p client --tests` to apply 1 suggestion)
    Finished `release` profile [optimized] target(s) in 0.08s
     Running unittests src/lib.rs (target/release/deps/client-ffaab629ddbc9824)

Allocation or reuse took too long: alloc=27814ns, reuse=30ns
thread 'runtime::buffer::tests::buffer_reuse' panicked at crates/client/src/runtime/buffer.rs:152:17:
Allocation or reuse took too long: alloc=27814ns, reuse=30ns

In the first iterations, vector reuse may seem slow, but in subsequent iterations, the reuse-based implementation wins.

alloc=27814ns vs reuse=30ns

@JKearnsl JKearnsl marked this pull request as draft September 13, 2025 15:31
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