17 releases

new 0.8.8 Jul 2, 2025
0.8.6 Dec 23, 2024
0.8.5 Nov 17, 2024
0.8.2 Sep 26, 2023
0.4.1 Jun 2, 2017

#88 in Data structures

Download history 6838/week @ 2025-03-14 5297/week @ 2025-03-21 4772/week @ 2025-03-28 5567/week @ 2025-04-04 5883/week @ 2025-04-11 6919/week @ 2025-04-18 5998/week @ 2025-04-25 6180/week @ 2025-05-02 5661/week @ 2025-05-09 6300/week @ 2025-05-16 5399/week @ 2025-05-23 5979/week @ 2025-05-30 5582/week @ 2025-06-06 4999/week @ 2025-06-13 8082/week @ 2025-06-20 4943/week @ 2025-06-27

24,503 downloads per month
Used in 58 crates (5 directly)

MIT license

40KB
673 lines

SmallBox

CI Status crates.io Documentation MSRV 1.80.0 License

A space-efficient alternative to Box<T> that stores small values on the stack and falls back to heap allocation for larger values. This optimization can significantly reduce memory allocations and improve performance for applications working with many small objects.

Quick Start

Add SmallBox to your Cargo.toml:

[dependencies]
smallbox = "0.8"

Basic Usage

use smallbox::SmallBox;
use smallbox::space::S4;

// Small values are stored on the stack
let small: SmallBox<[u32; 2], S4> = SmallBox::new([1, 2]);
assert!(!small.is_heap());

// Large values automatically use heap allocation  
let large: SmallBox<[u32; 32], S4> = SmallBox::new([0; 32]);
assert!(large.is_heap());

// Use like a regular Box
println!("Small: {:?}, Large: {:?}", *small, large.len());

Benchmark

The test platform is Ubuntu 2204 on AMD Ryzen 9 7950X3D 16-Core Processor.

compare                             fastest       │ slowest       │ median        │ mean          │ samples │ iters
├─ box_large_item                   13.96 ns      │ 14.44 ns      │ 14.2 ns       │ 14.16 ns      │ 10012800
├─ box_small_item                   7.313 ns      │ 7.512 ns      │ 7.391 ns      │ 7.392 ns      │ 10025600
├─ smallbox_large_item_large_space  14.13 ns      │ 49.42 ns      │ 14.9 ns       │ 15.07 ns      │ 10012800
├─ smallbox_large_item_small_space  23.91 ns      │ 26.09 ns      │ 25 ns         │ 24.94 ns      │ 1006400
├─ smallbox_small_item_large_space  0.995 ns      │ 1.025 ns      │ 1.005 ns      │ 1.003 ns      │ 100102400
╰─ smallbox_small_item_small_space  0.985 ns      │ 1.015 ns      │ 0.995 ns      │ 0.996 ns      │ 100102400

License

Licensed under either of:

at your option.

No runtime deps