#small #vec #vector #stack #no_std

no-std smallvec

‘Small vector’ optimization: store up to a small number of items on the stack

27 releases (8 stable)

new 1.5.1 Dec 5, 2020
1.5.0 Nov 17, 2020
1.4.2 Aug 11, 2020
1.4.1 Jul 6, 2020
0.1.5 Jun 30, 2015

#1 in Data structures

Download history 304894/week @ 2020-08-15 301155/week @ 2020-08-22 293079/week @ 2020-08-29 294443/week @ 2020-09-05 303168/week @ 2020-09-12 299231/week @ 2020-09-19 307299/week @ 2020-09-26 310286/week @ 2020-10-03 319890/week @ 2020-10-10 310599/week @ 2020-10-17 322821/week @ 2020-10-24 301214/week @ 2020-10-31 298963/week @ 2020-11-07 332353/week @ 2020-11-14 317840/week @ 2020-11-21 310778/week @ 2020-11-28

1,337,470 downloads per month
Used in 6,405 crates (460 directly)

MIT/Apache

87KB
2K SLoC

rust-smallvec

Documentation

Release notes

"Small vector" optimization for Rust: store up to a small number of items on the stack

Example

use smallvec::{SmallVec, smallvec};
    
// This SmallVec can hold up to 4 items on the stack:
let mut v: SmallVec<[i32; 4]> = smallvec![1, 2, 3, 4];

// It will automatically move its contents to the heap if
// contains more than four items:
v.push(5);

// SmallVec points to a slice, so you can use normal slice
// indexing and other methods to access its contents:
v[0] = v[1] + v[2];
v.sort();

lib.rs:

Small vectors in various sizes. These store a certain number of elements inline, and fall back to the heap for larger allocations. This can be a useful optimization for improving cache locality and reducing allocator traffic for workloads that fit within the inline buffer.

no_std support

By default, smallvec does not depend on std. However, the optional write feature implements the std::io::Write trait for vectors of u8. When this feature is enabled, smallvec depends on std.

Optional features

serde

When this optional dependency is enabled, SmallVec implements the serde::Serialize and serde::Deserialize traits.

write

When this feature is enabled, SmallVec<[u8; _]> implements the std::io::Write trait. This feature is not compatible with #![no_std] programs.

union

This feature is unstable and requires a nightly build of the Rust toolchain.

When the union feature is enabled smallvec will track its state (inline or spilled) without the use of an enum tag, reducing the size of the smallvec by one machine word. This means that there is potentially no space overhead compared to Vec. Note that smallvec can still be larger than Vec if the inline buffer is larger than two machine words.

To use this feature add features = ["union"] in the smallvec section of Cargo.toml. Note that this feature requires a nightly compiler (for now).

Tracking issue: rust-lang/rust#55149

const_generics

This feature is unstable and requires a nightly build of the Rust toolchain.

When this feature is enabled, SmallVec works with any arrays of any size, not just a fixed list of sizes.

Tracking issue: rust-lang/rust#44580

specialization

This feature is unstable and requires a nightly build of the Rust toolchain.

When this feature is enabled, SmallVec::from(slice) has improved performance for slices of Copy types. (Without this feature, you can use SmallVec::from_slice to get optimal performance for Copy types.)

Tracking issue: rust-lang/rust#31844

may_dangle

This feature is unstable and requires a nightly build of the Rust toolchain.

This feature makes the Rust compiler less strict about use of vectors that contain borrowed references. For details, see the Rustonomicon.

Tracking issue: rust-lang/rust#34761

Dependencies