smallvec

Stack-allocated 'small vector' optimization for Rust, spilling to the heap only when needed

Library
Cargo
v1.15.2
1,688stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
65/100Good
Development Activity52
Maintenance32
Community76
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
77/100Good
Architecture78
Code Quality80
Innovation76
Learning Curve75

smallvec implements the “small vector” optimization for Rust: a SmallVec<T, N> stores up to N items inline on the stack and only allocates on the heap once it grows beyond that inline capacity. It exposes largely the same API surface as the standard library’s Vec, including slice deref, so it’s usable as a near drop-in replacement wherever most collections stay small.

Originally developed for Mozilla’s Servo browser engine, smallvec is now one of the most widely depended-upon crates in the Rust ecosystem (nearly 1 billion total downloads), used anywhere avoiding heap allocation for typically-small collections matters for performance.

What You Get

  • SmallVec<T, N> with a Vec-compatible API surface, including deref to &[T]
  • The smallvec![] macro for concise literal construction
  • Optional feature flags (union, const_generics, specialization, may_dangle) for advanced memory-layout and performance tuning
  • drain_filter/drain_keep_rest for in-place filtering without extra allocation
  • no_std compatibility for use in embedded and kernel-level Rust code

Common Use Cases

  • Representing AST nodes’ children or CSS selector components (its original Servo use case) where collections are almost always small
  • Avoiding heap allocation in hot paths that build short-lived collections, such as parser or compiler internals
  • Returning small, variable-length collections from functions without forcing a heap allocation for the common case
  • Embedded or no_std Rust code that needs a Vec-like type without relying on a heap allocator for small sizes

Under The Hood

Architecture The crate’s implementation lives almost entirely in a single src/lib.rs (2625 lines), which defines SmallVec<T, N> as an enum-like union over an inline stack array and a heap-allocated buffer, switching representations transparently as elements are pushed past the inline capacity N; a separate specialization.rs module gates nightly-only trait-specialization optimizations behind a feature flag. Tech Stack Pure Rust (edition 2018), no_std-first with alloc as the only always-on dependency, and an extensive [features] table (union, const_generics, specialization, may_dangle, impl_bincode) letting consumers opt into unsafe/nightly-only layout optimizations only when needed. Code Quality src/tests.rs (1159 lines) is nearly half the size of the implementation itself, and a dedicated fuzz/ directory plus benches/ suite validate both correctness and the performance characteristics that are the crate’s entire reason for existing; the repo also tracks debug-visualizer metadata (debug_metadata/) for IDE integration. API Design SmallVec<T, N> deliberately mirrors std::vec::Vec’s method names and slice-deref behavior so it can usually replace Vec<T> with a type-signature change alone, while opt-in feature flags keep the common case simple and push more exotic, unsafe-adjacent optimizations behind explicit Cargo features.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search