pub enum SmallMap<const N: usize, K, V, SH = RandomState, SI = RandomState, const LINEAR_THRESHOLD: usize = DEFAULT_LINEAR_THRESHOLD> {
Heap(HashMap<K, V, SH>),
Inline(Inline<N, K, V, SH, SI, LINEAR_THRESHOLD>),
}Expand description
A hybrid map that stores data inline when small, and spills to heap when it grows.
§Type Parameters
-
N: Maximum number of elements to store inline (must be > 0). When the map exceeds this size, it automatically spills to a heap-allocatedHashMap. -
K: Key type. Must implementEq + Hashfor most operations. -
V: Value type. -
SH: Hasher for heap storage. Default:RandomState. This hasher is used when the map spills to heap. Note: The standard library’sRandomStateprovides HashDoS resistance but is not the fastest option. Consider your security vs performance requirements - for non-adversarial workloads, faster alternatives likerapidhashorfxhashcan improve performance significantly (though they are not cryptographically secure). -
SI: Hasher for inline storage. Default:DefaultInlineHasher(rapidhash if available, otherwise fxhash, ahash, or RandomState based on features). This hasher is used for SIMD-accelerated lookups in inline mode. Since inline storage only handles a small number of elements, HashDoS resistance is generally not a concern here - performance is the priority. We recommend using the default value and enabling therapidhashorfxhashfeature for best performance. -
LINEAR_THRESHOLD: Threshold for switching between linear and SIMD search. Default:DEFAULT_LINEAR_THRESHOLD(equal to SIMD group width, typically 16 on SSE2). WhenN < LINEAR_THRESHOLDorlen < LINEAR_THRESHOLD, linear search is used; otherwise, SIMD-accelerated hash search is used.How to choose this value: The optimal threshold depends on the tradeoff between hash computation cost and key comparison cost:
- Linear search:
lenkey comparisons (direct equality checks). - SIMD search: 1 hash computation +
len / GROUP_WIDTHSIMD operations + a few key comparisons.
If key comparison is cheap (e.g., integers, short strings), a higher threshold favors linear search which avoids hash overhead. If key comparison is expensive (e.g., long strings, complex types), a lower threshold makes SIMD search more attractive.
Recommendation: Values above 16 are generally not recommended. The default value works well for most use cases. Set to
0to disable linear search entirely. - Linear search:
§Examples
use small_map::SmallMap;
// Basic usage with defaults
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
// Custom hasher
use std::collections::hash_map::RandomState;
let mut map: SmallMap<8, &str, i32, RandomState> = SmallMap::new();
// Custom linear threshold (force SIMD search even for small maps)
let mut map: SmallMap<8, &str, i32, RandomState, RandomState, 4> = SmallMap::new();Variants§
Heap(HashMap<K, V, SH>)
Heap-allocated storage using hashbrown::HashMap.
Inline(Inline<N, K, V, SH, SI, LINEAR_THRESHOLD>)
Inline storage with SIMD-accelerated lookups.
Implementations§
Source§impl<const N: usize, K, V, SH: Default, SI: Default, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH: Default, SI: Default, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty SmallMap with the specified capacity.
The hash map will be able to hold at least capacity elements without
reallocating. If capacity is smaller than N, the hash map will not allocate.
Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Sourcepub fn with_hasher(hash_builder: SH) -> Selfwhere
SI: Default,
pub fn with_hasher(hash_builder: SH) -> Selfwhere
SI: Default,
Creates an empty SmallMap which will use the given hash builder to hash
keys. It will be allocated with the given allocator.
The hash map is initially created with a capacity of N, so it will not allocate until it its size bigger than inline size N.
§Examples
use std::hash::{BuildHasherDefault, DefaultHasher};
use small_map::SmallMap;
let s = BuildHasherDefault::<DefaultHasher>::default();
let mut map = SmallMap::<8, _, _, _>::with_hasher(s);
map.insert(1, 2);Sourcepub const fn with_hashers(heap_hasher: SH, inline_hasher: SI) -> Self
pub const fn with_hashers(heap_hasher: SH, inline_hasher: SI) -> Self
Creates an empty SmallMap which will use the given hash builders to hash
keys. It will be allocated with the given allocator.
§Examples
use std::collections::hash_map::RandomState;
use small_map::SmallMap;
let heap_hasher = RandomState::new();
let inline_hasher = RandomState::new();
let mut map = SmallMap::<8, _, _, _, _>::with_hashers(heap_hasher, inline_hasher);
map.insert(1, 2);Sourcepub fn with_capacity_and_hasher(capacity: usize, heap_hasher: SH) -> Selfwhere
SI: Default,
pub fn with_capacity_and_hasher(capacity: usize, heap_hasher: SH) -> Selfwhere
SI: Default,
Creates an empty SmallMap with the specified capacity, using heap_hasher
to hash the keys.
The hash map will be able to hold at least capacity elements without
reallocating. If capacity is smaller than or eq to N, the hash map will not allocate.
Sourcepub fn with_capacity_and_hashers(
capacity: usize,
heap_hasher: SH,
inline_hasher: SI,
) -> Self
pub fn with_capacity_and_hashers( capacity: usize, heap_hasher: SH, inline_hasher: SI, ) -> Self
Creates an empty SmallMap with the specified capacity, using heap_hasher
to hash the keys for heap storage, and inline_hasher for inline storage.
§Examples
use std::collections::hash_map::RandomState;
use small_map::SmallMap;
let heap_hasher = RandomState::new();
let inline_hasher = RandomState::new();
let mut map =
SmallMap::<8, _, _, _, _>::with_capacity_and_hashers(16, heap_hasher, inline_hasher);
map.insert(1, 2);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the SmallMap<N, K, V> might be able to hold
more, but is guaranteed to be able to hold at least this many.
§Examples
use small_map::SmallMap;
let map: SmallMap<8, i32, i32> = SmallMap::with_capacity(100);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 100);
let map: SmallMap<8, i32, i32> = SmallMap::with_capacity(2);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 8);Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Returns a mutable reference to the value corresponding to the key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
map.insert(1, 10);
if let Some(v) = map.get_mut(&1) {
*v = 20;
}
assert_eq!(map.get(&1), Some(&20));Sourcepub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true if the map contains a value for the specified key.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert!(map.contains_key(&1));
assert!(!map.contains_key(&2));Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
assert_eq!(map.insert(1, "a"), None);
assert_eq!(map.insert(1, "b"), Some("a"));
assert_eq!(map.get(&1), Some(&"b"));Sourcepub unsafe fn insert_unique_unchecked(
&mut self,
key: K,
value: V,
) -> (&K, &mut V)
pub unsafe fn insert_unique_unchecked( &mut self, key: K, value: V, ) -> (&K, &mut V)
Inserts a key-value pair into the map without checking if the key already exists.
Returns a reference to the key and a mutable reference to the value.
§Safety
The caller must ensure that the key does not already exist in the map. Inserting a duplicate key will result in undefined behavior (e.g., memory leaks, incorrect iteration, or other inconsistencies).
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
// SAFETY: We know the key doesn't exist because the map is empty.
unsafe { map.insert_unique_unchecked(1, "a") };
assert_eq!(map.get(&1), Some(&"a"));Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes a key from the map, returning the value at the key if the key was previously in the map.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);Sourcepub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
Removes a key from the map, returning the stored key and value if the key was previously in the map.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, &str> = SmallMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove_entry(&1), None);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
for i in 0..8 {
map.insert(i, i * 10);
}
map.retain(|&k, _| k % 2 == 0);
assert_eq!(map.len(), 4);Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map.
This method clears the map and keeps the allocated memory for reuse.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, ()> = SmallMap::new();
for i in 0..16 {
map.insert(i, ());
}
assert!(!map.is_inline());
assert_eq!(map.len(), 16);
map.clear();
assert!(!map.is_inline());
assert_eq!(map.len(), 0);Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
assert_eq!(map.len(), 0);
map.insert(1, 10);
assert_eq!(map.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, i32, i32> = SmallMap::new();
assert!(map.is_empty());
map.insert(1, 10);
assert!(!map.is_empty());Sourcepub fn iter(&self) -> Iter<'_, N, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, N, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
for (key, val) in map.iter() {
println!("key: {key} val: {val}");
}Sourcepub fn keys(&self) -> Keys<'_, N, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, N, K, V> ⓘ
An iterator visiting all keys in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
for key in map.keys() {
println!("{key}");
}Sourcepub fn values(&self) -> Values<'_, N, K, V> ⓘ
pub fn values(&self) -> Values<'_, N, K, V> ⓘ
An iterator visiting all values in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
for val in map.values() {
println!("{val}");
}Sourcepub fn into_keys(self) -> IntoKeys<N, K, V> ⓘ
pub fn into_keys(self) -> IntoKeys<N, K, V> ⓘ
Creates a consuming iterator visiting all the keys in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
let keys: Vec<_> = map.into_keys().collect();Sourcepub fn into_values(self) -> IntoValues<N, K, V> ⓘ
pub fn into_values(self) -> IntoValues<N, K, V> ⓘ
Creates a consuming iterator visiting all the values in arbitrary order.
§Examples
use small_map::SmallMap;
let mut map: SmallMap<8, &str, i32> = SmallMap::new();
map.insert("a", 1);
map.insert("b", 2);
let values: Vec<_> = map.into_values().collect();Trait Implementations§
Source§impl<const N: usize, K: Clone, V: Clone, SH: Clone, SI: Clone, const LINEAR_THRESHOLD: usize> Clone for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K: Clone, V: Clone, SH: Clone, SI: Clone, const LINEAR_THRESHOLD: usize> Clone for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Debug for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Debug for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§impl<const N: usize, K, V, SH: Default, SI: Default, const LINEAR_THRESHOLD: usize> Default for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH: Default, SI: Default, const LINEAR_THRESHOLD: usize> Default for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§fn default() -> Self
fn default() -> Self
Creates an empty SmallMap<N, K, V, SH, SI>, with the Default value for the hasher.
§Examples
use std::collections::hash_map::RandomState;
use small_map::SmallMap;
// You can specify all types of SmallMap, including N and hasher.
// Created map is empty and don't allocate memory
let map: SmallMap<8, u32, String> = SmallMap::default();
assert_eq!(map.capacity(), 8);
let map: SmallMap<8, u32, String, RandomState> = SmallMap::default();
assert_eq!(map.capacity(), 8);Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Extend<(K, V)> for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Extend<(K, V)> for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> FromIterator<(K, V)> for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> FromIterator<(K, V)> for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize, Q> Index<&Q> for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize, Q> Index<&Q> for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§impl<'a, const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> IntoIterator for &'a SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<'a, const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> IntoIterator for &'a SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> IntoIterator for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> IntoIterator for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Source§impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> PartialEq for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> PartialEq for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Eq for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Auto Trait Implementations§
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Freeze for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> RefUnwindSafe for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Send for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Sync for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> Unpin for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
impl<const N: usize, K, V, SH, SI, const LINEAR_THRESHOLD: usize> UnwindSafe for SmallMap<N, K, V, SH, SI, LINEAR_THRESHOLD>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.