JesperAxelsson / rust-intmap

Specialized hashmap for u64 keys
MIT License
30 stars 10 forks source link

Swap collections instead of moving contents #38

Closed chinoto closed 2 years ago

chinoto commented 2 years ago

I was hoping this might be slightly more efficient because the internal Vecs aren't being moved and the collect knows exactly what capacity to make the new outer Vec, but the benchmark was too wibbly-wobbly to know for sure if this helps. Maybe criterion would be more accurate? At least the code is shorter.

My other idea was to drain all the entries into a single level Vec to preserve the allocations already in self.cache[ix] and minimize further allocations, but that didn't bench very well. My guess is that it is quicker to (de)allocate than copy memory around.

let vec: Vec<(u64, V)> = self.cache.iter_mut().flat_map(|v| v.drain(..)).collect();
self.cache.extend((0..new_lim - self.cache.len()).map(|_| Vec::new()));
JesperAxelsson commented 2 years ago

Yeah, I've had the same issues with unstable benchmarks. Unclear on what causes it, have tried with more test data but does not seem to do much difference. Criterion might help.

The current insert test does not exercise increase_cache as it already is has the correct capacity. Will a few more test and increase the amount of test data.