aml_tz4_331012050
Merge "Revert "Update hashbrown to 0.12.0"" am: 94cdad0346 am: 589fdf95b1 am: 538884f4c1 am: 6325c20384

Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/hashbrown/+/2020795

Change-Id: I7f6e716a80b9703bbcf9983b62996c4744313bbf
tree: 9eb9b985863843fbf9332a33951797aca28db187
  1. benches/
  2. src/
  3. tests/
  4. .cargo_vcs_info.json
  5. .gitignore
  6. Android.bp
  7. Cargo.toml
  8. Cargo.toml.orig
  9. cargo2android.json
  10. CHANGELOG.md
  11. clippy.toml
  12. LICENSE-APACHE
  13. LICENSE-MIT
  14. METADATA
  15. MODULE_LICENSE_APACHE2
  16. OWNERS
  17. README.md
  18. TEST_MAPPING
README.md

hashbrown

Build Status Crates.io Documentation Rust

This crate is a Rust port of Google‘s high-performance SwissTable hash map, adapted to make it a drop-in replacement for Rust’s standard HashMap and HashSet types.

The original C++ version of SwissTable can be found here, and this CppCon talk gives an overview of how the algorithm works.

Since Rust 1.36, this is now the HashMap implementation for the Rust standard library. However you may still want to use this crate instead since it works in environments without std, such as embedded systems and kernels.

Change log

Features

  • Drop-in replacement for the standard library HashMap and HashSet types.
  • Uses AHash as the default hasher, which is much faster than SipHash. However, AHash does not provide the same level of HashDoS resistance as SipHash, so if that is important to you, you might want to consider using a different hasher.
  • Around 2x faster than the previous standard library HashMap.
  • Lower memory usage: only 1 byte of overhead per entry instead of 8.
  • Compatible with #[no_std] (but requires a global allocator with the alloc crate).
  • Empty hash maps do not allocate any memory.
  • SIMD lookups to scan multiple hash entries in parallel.

Performance

Compared to the previous implementation of std::collections::HashMap (Rust 1.35).

With the hashbrown default AHash hasher:

nameoldstdhash ns/iterhashbrown ns/iterdiff ns/iterdiff %speedup
insert_ahash_highbits18,8658,020-10,845-57.49%x 2.35
insert_ahash_random19,7118,019-11,692-59.32%x 2.46
insert_ahash_serial19,3656,463-12,902-66.63%x 3.00
insert_erase_ahash_highbits51,13617,916-33,220-64.96%x 2.85
insert_erase_ahash_random51,15717,688-33,469-65.42%x 2.89
insert_erase_ahash_serial45,47914,895-30,584-67.25%x 3.05
iter_ahash_highbits1,3991,092-307-21.94%x 1.28
iter_ahash_random1,5861,059-527-33.23%x 1.50
iter_ahash_serial3,1681,079-2,089-65.94%x 2.94
lookup_ahash_highbits32,3514,792-27,559-85.19%x 6.75
lookup_ahash_random17,4194,817-12,602-72.35%x 3.62
lookup_ahash_serial15,2543,606-11,648-76.36%x 4.23
lookup_fail_ahash_highbits21,1874,369-16,818-79.38%x 4.85
lookup_fail_ahash_random21,5504,395-17,155-79.61%x 4.90
lookup_fail_ahash_serial19,4503,176-16,274-83.67%x 6.12

With the libstd default SipHash hasher:

nameoldstdhash ns/iterhashbrown ns/iterdiff ns/iterdiff %speedup
insert_std_highbits19,21616,885-2,331-12.13%x 1.14
insert_std_random19,17917,034-2,145-11.18%x 1.13
insert_std_serial19,46217,493-1,969-10.12%x 1.11
insert_erase_std_highbits50,82535,847-14,978-29.47%x 1.42
insert_erase_std_random51,44835,392-16,056-31.21%x 1.45
insert_erase_std_serial87,71138,091-49,620-56.57%x 2.30
iter_std_highbits1,3781,159-219-15.89%x 1.19
iter_std_random1,3951,132-263-18.85%x 1.23
iter_std_serial1,7041,105-599-35.15%x 1.54
lookup_std_highbits17,19513,642-3,553-20.66%x 1.26
lookup_std_random17,18113,773-3,408-19.84%x 1.25
lookup_std_serial15,48313,651-1,832-11.83%x 1.13
lookup_fail_std_highbits20,92613,474-7,452-35.61%x 1.55
lookup_fail_std_random21,76613,505-8,261-37.95%x 1.61
lookup_fail_std_serial19,33613,519-5,817-30.08%x 1.43

Usage

Add this to your Cargo.toml:

[dependencies]
hashbrown = "0.11"

Then:

use hashbrown::HashMap;

let mut map = HashMap::new();
map.insert(1, "one");

Flags

This crate has the following Cargo features:

  • nightly: Enables nightly-only features including: #[may_dangle].
  • serde: Enables serde serialization support.
  • rayon: Enables rayon parallel iterator support.
  • raw: Enables access to the experimental and unsafe RawTable API.
  • inline-more: Adds inline hints to most functions, improving run-time performance at the cost of compilation time. (enabled by default)
  • bumpalo: Provides a BumpWrapper type which allows bumpalo to be used for memory allocation.
  • ahash: Compiles with ahash as default hasher. (enabled by default)
  • ahash-compile-time-rng: Activates the compile-time-rng feature of ahash. For targets with no random number generator this pre-generates seeds at compile time and embeds them as constants. See aHash's documentation (disabled by default)

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.