Hello everyone,
What's the most efficient way (mostly in terms of access speed, but it can't take up an excessive amount of memory, either) to store a small vector (less than 16 elements)? I've heard of smallvec, but this early in the project, I don't want to use an external crate. Right now, I'm just representing it like this:
struct<T : Clone> NanoVec<T> {
data : [T; 16],
len : usize,
}
But T isn't always Copy, so to initialize the list, I have to do something like this (it gets very inconvenient):
But seriously, I actually want to try implementing it on my own (get more experienced in Rust and just for fun). Any starter ideas (that's what I'm asking for - my current approach, using a fixed size array, is really awkward (especially for non copy types), and probably not the most memory of time efficient)? But yeah, Cargo makes using external crates very painless in Rust, so I see why it's good practice to not reinvent the wheel.
For educational purposes you can study arrayvec. It uses smaller types for small length. Also check how it uses uninitialised memory – it needs special care in case of panics.
You'll also find lots of hacks in C (that can be adapted to Rust) if you search for Small String Optimization.
I found simply making a regular vector and then doing .reserve(N) directly after the definition outperformed SmallVec by a significant margin. Maybe I was doing something wrong but I benchmarked it extensively.
Like this:
let mut y_segs: Vec<u64> = Vec::new();
y_segs.reserve(100);