rustc-hash

repository·main·Indexed 20 days ago

https://github.com/rust-lang/rustc-hash

A high-speed, non-cryptographic hashing algorithm and collections used by the Rust compiler (rustc). It provides FxHasher and high-performance alternatives to standard library collections, including FxHashMap and FxHashSet, designed for performance-critical applications where DOS resistance is not a primary concern. Supports no_std environments, randomized hashing via FxRandomState, and deterministic hashing via FxSeededState.

Tokens
1.9K
Snippets
10
Records
13
Agent score
70%

What's inside rustc-hash

  1. Configure rustc-hash for no_std environments

    main

    By default, rustc-hash enables the std feature to provide the FxHashMap and FxHashSet collections. If you are working in a no_std environment, you must disable the default features in your Cargo.toml to avoid pulling in the standard library.

    rustc-hash = { version = "2.1", default-features = false }
  2. Use FxHashMap and FxHashSet for faster hashing

    main

    rustc-hash provides FxHashMap and FxHashSet as high-performance alternatives to the standard library's HashMap and HashSet. These collections use the FxHasher, a non-cryptographic hashing algorithm designed for speed.

    While std::collections::HashMap uses SipHash to protect against DOS attacks, FxHashMap prioritizes raw performance, making it ideal for scenarios where cryptographic security is not required (such as in compilers or internal data processing). FxHashMap and FxHashSet are type aliases for their std counterparts configured with the FxHasher.

    Note: The underlying algorithm is a polynomial hash with a single bit rotation and a wyhash-inspired compression function for strings/slices.

    use rustc_hash::FxHashMap;
    
    let mut map: FxHashMap<u32, u32> = FxHashMap::default();
    map.insert(22, 44);
  3. Use FxRandomState for randomized FxHash collections

    main

    If you need HashMap or HashSet implementations that use the FxHash algorithm but require randomized seeding to prevent hash collision attacks or to ensure different iteration orders, use FxRandomState.

    FxRandomState implements BuildHasher and provides a unique seed for every instance created via new() or Default::default(). It uses a thread-local mechanism to ensure that repeatedly creating maps on the same thread is efficient while still providing different seeds (by incrementing the cached seed) to ensure maps created on the same thread do not have the same iteration order.

    use std::collections::HashMap;
    use rustc_hash::FxRandomState;
    
    // Create a HashMap with randomized FxHash seeding
    let mut map: HashMap<String, i32, FxRandomState> = HashMap::with_hasher(FxRandomState::new());
  4. Use FxSeededState for deterministic hashing

    main

    The FxSeededState allows you to create hash maps and sets with a specific, deterministic seed. This is useful when you need consistent hash results across different runs or environments.

    When the std feature is enabled, you can use the following type aliases:

    • FxHashMapSeed
    • FxHashSetSeed
  5. Use FxHashMap and FxHashSet for fast hashing

    main

    If you are using the std feature, you can use the FxHashMap and FxHashSet type aliases. These are standard collections that use the FxBuildHasher instead of the default SipHash, providing significantly faster performance for non-cryptographic use cases where DOS resistance is not a primary concern.

    Note: These aliases require the std feature to be enabled.

    use rustc_hash::FxHashMap;
    
    let mut map: FxHashMap<u32, u32> = FxHashMap::default();
    map.insert(22, 44);
  6. Use FxHashMapRand and FxHashSetRand for randomized hashing

    main
    If you have the rand feature enabled, you can use FxHashMapRand and FxHashSetRand. These collections use FxRandomState to provide randomized hashing, which can help mitigate certain types of hash collision attacks while still benefiting from the speed of the Fx algorithm.
  7. FxHashMapSeed and FxHashSetSeed type aliases

    main

    If the std feature is enabled, rustc-hash provides convenient type aliases for hash maps and sets that use FxSeededState as their hasher state. These are useful for quickly declaring collections that use the seeded fx algorithm.

    // Requires 'std' feature
    use rustc_hash::{FxHashMapSeed, FxHashSetSeed};
    use std::collections::HashMap;
    use std::collections::HashSet;
    
    // Note: These aliases use FxSeededState, but you may still need to 
    // provide the specific seed via with_hasher if you want a non-default seed.
    let mut map: FxHashMapSeed<u32, String> = HashMap::new();
    let mut set: FxHashSetSeed<u32> = HashSet::new();
  8. Use FxSeededState to provide a fixed seed to hash maps

    main

    The FxSeededState struct implements BuildHasher and allows you to use the FxHasher algorithm with a specific, reproducible seed. This is useful when you need deterministic hashing behavior across different runs or environments. You can use it with standard collections via HashMap::with_hasher or by using the provided type aliases if the std feature is enabled.

    use std::collections::HashMap;
    use rustc_hash::FxSeededState;
    
    let mut map = HashMap::with_hasher(FxSeededState::with_seed(12));
    map.insert(15, 610);
    assert_eq!(map[&15], 610);
  9. FxRandomState::new

    main

    Constructs a new FxRandomState initialized with a random seed. It uses a thread-local seed that is incremented on each call to new() to ensure that different instances created on the same thread have different seeds.

    use rustc_hash::FxRandomState;
    
    let state = FxRandomState::new();
  10. FxHashMapRand and FxHashSetRand type aliases

    main

    The library provides convenient type aliases for standard collections pre-configured with FxRandomState:

    • FxHashMapRand<K, V>: A HashMap<K, V, FxRandomState>.
    • FxHashSetRand<V>: A HashSet<V, FxRandomState>.
    use rustc_hash::{FxHashMapRand, FxHashSetRand};
    
    let mut map: FxHashMapRand<String, i32> = FxHashMapRand::default();
    let mut set: FxHashSetRand<String> = FxHashSetRand::default();