My new "memory optimised" code is far from complete yet, but I thought I would do some measuring before going further. This is a cherry-picked test, but I think the results are still quite interesting. The test is insert 2,000 16-byte entries into a BTreeMap (evens then odds) and check how much memory was used.
#[test]
fn exp_mem_test()
{
const N: usize = 51;
const M: usize = N + 1;
let n = 1000;
let mut map = BTreeMap::<u64, u64, N, M>::new();
for i in 0..n {
map.insert(i*2, i*2);
}
for i in 0..n {
map.insert(i*2+1, i*2+1);
}
crate::print_memory();
println!("Required memory: {} bytes", n * 32 );
println!("size of Leaf={}", std::mem::size_of::<Leaf<u64,u64,N>>() );
}
#[test]
fn std_mem_test()
{
let n = 1000;
let mut map = std::collections::BTreeMap::<u64, u64>::new();
for i in 0..n {
map.insert(i*2, i*2);
}
for i in 0..n {
map.insert(i*2+1, i*2+1);
}
crate::print_memory();
}
Results:
Memory allocated: 72916 bytes
test lessmem::std_mem_test ... ok
Memory allocated: 37300 bytes
Required memory: 32000 bytes
size of Leaf=816
test lessmem::exp_mem_test ... ok
So std is using nearly twice as much memory for this test, which I think shows there is at least some interesting scope for saving memory here. What I don't yet understand is WHY.