Draft
Conversation
Member
Author
|
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=30nsIn the first iterations, vector reuse may seem slow, but in subsequent iterations, the reuse-based implementation wins. alloc=27814ns vs reuse=30ns |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
optimize heap