Hello again, I'm hoping someone can help point me to the right apis or indicate if they exists or unstable related to the title. What I'm looking to do is to produces a faster version of the crate linear_map
https://crates.io/crates/linear-map
Effectively this is just a replacement for Hashmap backed by a Vec
specifically Vec::<(K, V)>
. From what I can tell there isn't inherently anything wrong with this, but the layout seems fairly un-cache friendly since we have to stride over V
while searching for keys. So this effectively get slower the larger V
since we're going to miss cache more often.
Just to test I made what I would consider the sort of naive replacement. Literally just.
pub struct LinearMap2<K, V> {
keys: Vec<K>,
value: Vec<V>,
}
I'm using this as part of a dynamic programming language I'm working on. Specially for property and method lookups. This is part of a larger struct that using a linear_map when there a few properties on an instance but a full hashmap when we hit some threshold. I went ahead an made a minimal impl for what I had above and some initial benchmarks are really promising. In one bench there was a 30% speedup for a script in my language.
The downside with above is it's pretty space wasteful. essentially we waste 16 bytes on two duplicate len
and capacity
fields. So this gets to the title question. If I wanted to create something like
pub struct LinearMap2<K, V> {
ptr_keys: NonNull<K>,
ptr_vals: NonNull<V>,
cap: usize,
len: usize
}
Is there a way for me to allocate for the key and vals slices in stable rust?