Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ An inline SIMD accelerated hashmap designed for small amount of data.

## Usage

### SmallMap

```rust
use small_map::FxSmallMap;
// Don't worry about the 32 here, it's the inline size, not the limitation.
Expand All @@ -26,6 +28,22 @@ Usually you can use this map for short lifetime and small key/values, for exampl

You can use it like other normal hashmaps without worrying about its size.

### SmallSet

```rust
use small_map::FxSmallSet;
// Similar to SmallMap, but for sets (collection of unique values)
type MySmallSet<T> = FxSmallSet<32, T>;

let mut set = MySmallSet::new();
set.insert(1_u8);
set.insert(2_u8);
assert!(set.contains(&1));
assert_eq!(set.len(), 2);
```

SmallSet is implemented as a wrapper around SmallMap where the value is `()`, providing the same performance benefits for small collections of unique values.

## Choosing N

The choice of `N` involves a trade-off between inline storage benefits and struct size overhead:
Expand Down
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ use inline::Inline;
mod macros;
mod inline;
mod raw;
mod set;

/// Re-exported from [`hashbrown`] for custom key equivalence lookups.
///
/// Allows using a different type for lookups than the key type stored in the map.
pub use hashbrown::Equivalent;
pub use set::SmallSet;

#[cfg(feature = "serde")]
mod serde;
Expand All @@ -70,6 +72,28 @@ pub type FxSmallMap<const N: usize, K, V> = SmallMap<N, K, V, rustc_hash::FxBuil
pub type ASmallMap<const N: usize, K, V> =
SmallMap<N, K, V, core::hash::BuildHasherDefault<ahash::AHasher>>;

/// Type alias for [`SmallSet`] using [`rapidhash`](https://docs.rs/rapidhash) as the heap hasher.
///
/// Since rapidhash is a fast hasher, `LINEAR_THRESHOLD` is set to 8 to prefer SIMD search earlier.
///
/// Requires the `rapidhash` feature.
#[cfg(feature = "rapidhash")]
pub type RapidSmallSet<const N: usize, T> =
SmallSet<N, T, rapidhash::fast::RandomState, DefaultInlineHasher, 8>;

/// Type alias for [`SmallSet`] using [`rustc_hash::FxBuildHasher`] as the heap hasher.
///
/// Requires the `fxhash` feature.
#[cfg(feature = "fxhash")]
pub type FxSmallSet<const N: usize, T> = SmallSet<N, T, rustc_hash::FxBuildHasher>;

/// Type alias for [`SmallSet`] using [`ahash::AHasher`] as the heap hasher.
///
/// Requires the `ahash` feature.
#[cfg(feature = "ahash")]
pub type ASmallSet<const N: usize, T> =
SmallSet<N, T, core::hash::BuildHasherDefault<ahash::AHasher>>;

#[cfg(feature = "rapidhash")]
type DefaultInlineHasher = rapidhash::fast::RandomState;
#[cfg(all(not(feature = "rapidhash"), feature = "fxhash"))]
Expand Down Expand Up @@ -694,6 +718,15 @@ pub struct Keys<'a, const N: usize, K, V> {
inner: Iter<'a, N, K, V>,
}

impl<'a, const N: usize, K, V> Clone for Keys<'a, N, K, V> {
#[inline]
fn clone(&self) -> Self {
Keys {
inner: self.inner.clone(),
}
}
}

impl<'a, const N: usize, K, V> Iterator for Keys<'a, N, K, V> {
type Item = &'a K;

Expand Down
Loading