[stack;vec]

A rust crate to use stack-allocated vectors (to improve performance and/or when there is no std)

Repository Latest version Documentation

Travis-CI Status Test code coverage License

⚠️⚠️ Warning: unsafe is used ⚠️⚠️

However, it should be noted that special attention has been paid to array indices arithmetic (overflow/underflow), since they are, after all, pointers, and to memory management (no memory leaks or double frees).

The only remaining issue is Exception safety; as of v0.1.0, a non fatal panic while dropping (very rare), should "only" cause memory leaks (preferable to dangling pointers).

It may thus be ill-suited for production. Use at your own risk.

Motivation

Rust stack/inline arrays don't implement 2 very useful iterator-related interfaces:

  1. IntoIterator<Item = T> for [T; n]

  2. FromIterator for [T; n]

The reason for that is that both interfaces need a structure being able to hold the partially iterated state: i.e., incomplete arrays. Those have (statically-allocated) memory that might not be initialized: so they are, in a way, like [Vec]tors (except for the fact that their (initial) capacity is fixed and cannot be changed)

That's why having those nice iterator interfaces require writing down a cell-accurate memory ownership management logic very similar to [Vec]'s : hence the [StackVec].

Bonus

By exposing the underlying [StackVec] needed by the aformentioned interfaces, we get full access to a stack-allocated [Vec], which can also be useful on its own, since it avoids heap allocation:

Disclaimer

The performance gain (from using [StackVec] instead of [Vec]) is not always guaranteed, since:

  1. [Vec] is the cornerstone of Rust's std library collection and has extremely efficient code written so that LLVM can easily optimize its usage

  2. Rust's allocator is also incredibly well optimised so the performance penalties from bins management and system allocations (and the locks in a multi-threaded environment) are quite well amortized on average.

[Vec] vs [StackVec] basic benchmark

```sh $ cargo +nightly bench --features nightly

test vecextend ... bench: 64,129 ns/iter (+/- 3,069) test vecfromiter ... bench: 65,569 ns/iter (+/- 3,761) test arrayfromiter ... bench: 358,993 ns/iter (+/- 6,916) test stackvecextend ... bench: 360,105 ns/iter (+/- 17,489) test stackvecfromiter ... bench: 369,585 ns/iter (+/- 40,894) test stackvecextendbyref ... bench: 374,226 ns/iter (+/- 11,686) test vecextendbyref ... bench: 863,362 ns/iter (+/- 32,483) ```

Usage

Examples

See the source files for the examples

You can run each example (example_name.rs) with: sh $ cargo run --example example_name

WIP

  1. [Documentation]

  2. [no_std] support

  3. More [Vec]-like methods