smallvec
Stack-allocated 'small vector' optimization for Rust, spilling to the heap only when needed
Repository Health
Technical Analysis
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 aVec-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_restfor in-place filtering without extra allocationno_stdcompatibility 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_stdRust code that needs aVec-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.
Used by 5 apps in this directory
Arroyo
Data Engineering · Analytics
A distributed stream processing engine written in Rust that lets you write SQL to run stateful, real-time computations over data streams with subsecond results.
Cap
Team Chat · Video Conferencing
Open source Loom alternative with GPU-accelerated recording, instant share links, AI summaries, and full self-hosting via Docker Compose.
Meilisearch
Search
Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.
monty
AI Development · Developer Tools
Run LLM-generated Python code safely inside your agent—no containers, no CPython, no compromise—with sub-microsecond startup.
Spacedrive
File Storage · Collaboration
One file manager for all your devices and clouds — powered by a Virtual Distributed File System built in Rust.