Rust Smallvec Optimization

What is a smallvec?

Smallvec is a Rust crate that provides a small vector optimization that stores up to a bounded number of items on the stack. Smallvec preallocates the memory onto the stack and therefore is not fit for applications that do not know the maximum number of items that they may store.

Why would I use smallvec?

If you ever find yourself frequently allocating and deallocating many small, probably numeric, vectors in some code, then you should consider using smallvec. It is always recommended to profile your code also, to confirm that vector allocating is indeed your bottleneck. Sometimes heap vector allocation may create a bottleneck, but only in very performance sensitive code. For those cases it may help to allocate all vectors into the "hottest" area of memory, which would be the stack. In this fashion you can not only benefit from the reduced allocation/deallocation complexity, but also from some more cache hits as well.