| 1 |
# hashbrown |
| 2 |
|
| 3 |
[](https://github.com/rust-lang/hashbrown/actions) |
| 4 |
[](https://crates.io/crates/hashbrown) |
| 5 |
[](https://docs.rs/hashbrown) |
| 6 |
[](https://github.com/rust-lang/hashbrown) |
| 7 |
|
| 8 |
This crate is a Rust port of Google's high-performance [SwissTable] hash map, |
| 9 |
adapted to make it a drop-in replacement for Rust's standard `HashMap` and |
| 10 |
`HashSet` types. |
| 11 |
|
| 12 |
The original C++ version of SwissTable can be found [here], and this |
| 13 |
[CppCon talk] gives an overview of how the algorithm works. |
| 14 |
|
| 15 |
Since Rust 1.36, the Rust standard library has adopted this implementation for |
| 16 |
`HashMap`, using its own default hasher (see |
| 17 |
[`std::hash::DefaultHasher`](https://doc.rust-lang.org/std/hash/struct.DefaultHasher.html)). |
| 18 |
However you may still want to use this crate instead since it works in |
| 19 |
environments without `std`, such as embedded systems and kernels. |
| 20 |
|
| 21 |
[SwissTable]: https://abseil.io/blog/20180927-swisstables |
| 22 |
[here]: https://github.com/abseil/abseil-cpp/blob/master/absl/container/internal/raw_hash_set.h |
| 23 |
[CppCon talk]: https://www.youtube.com/watch?v=ncHmEUmJZf4 |
| 24 |
|
| 25 |
## [Change log](CHANGELOG.md) |
| 26 |
|
| 27 |
## Features |
| 28 |
|
| 29 |
- Drop-in replacement for the standard library `HashMap` and `HashSet` types. |
| 30 |
- Uses [foldhash](https://github.com/orlp/foldhash) as the default hasher, which |
| 31 |
is much faster than SipHash. However, foldhash does *not provide the same |
| 32 |
level of HashDoS resistance* as SipHash, so if that is important to you, you |
| 33 |
might want to consider using a different hasher. |
| 34 |
- Around 2x faster than the previous standard library `HashMap`. |
| 35 |
- Lower memory usage: only 1 byte of overhead per entry instead of 8. |
| 36 |
- Compatible with `#[no_std]` (but requires a global allocator with the `alloc` |
| 37 |
crate). |
| 38 |
- Empty hash maps do not allocate any memory. |
| 39 |
- SIMD lookups to scan multiple hash entries in parallel. |
| 40 |
|
| 41 |
## Usage |
| 42 |
|
| 43 |
Add this to your `Cargo.toml`: |
| 44 |
|
| 45 |
```toml |
| 46 |
[dependencies] |
| 47 |
hashbrown = "0.17" |
| 48 |
``` |
| 49 |
|
| 50 |
Then: |
| 51 |
|
| 52 |
```rust |
| 53 |
use hashbrown::HashMap; |
| 54 |
|
| 55 |
let mut map = HashMap::new(); |
| 56 |
map.insert(1, "one"); |
| 57 |
``` |
| 58 |
|
| 59 |
## Flags |
| 60 |
|
| 61 |
This crate has the following Cargo features: |
| 62 |
|
| 63 |
- `nightly`: Enables nightly-only features including: `#[may_dangle]`. |
| 64 |
- `serde`: Enables serde serialization support. |
| 65 |
- `rayon`: Enables rayon parallel iterator support. |
| 66 |
- `equivalent`: Allows comparisons to be customized with the `Equivalent` trait. |
| 67 |
(enabled by default) |
| 68 |
- `raw-entry`: Enables access to the deprecated `RawEntry` API. |
| 69 |
- `inline-more`: Adds inline hints to most functions, improving run-time |
| 70 |
performance at the cost of compilation time. (enabled by default) |
| 71 |
- `default-hasher`: Compiles with foldhash as default hasher. (enabled by |
| 72 |
default) |
| 73 |
- `allocator-api2`: Enables support for allocators that support |
| 74 |
`allocator-api2`. (enabled by default) |
| 75 |
|
| 76 |
## License |
| 77 |
|
| 78 |
Licensed under either of: |
| 79 |
|
| 80 |
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 81 |
https://www.apache.org/licenses/LICENSE-2.0) |
| 82 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 83 |
https://opensource.org/license/mit) |
| 84 |
|
| 85 |
at your option. |
| 86 |
|
| 87 |
### Contribution |
| 88 |
|
| 89 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 90 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 91 |
dual licensed as above, without any additional terms or conditions. |