### Install Dependencies and Setup Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Installs necessary tools like git, a C++ compiler, Rust, and uv, then clones the Rapidhash repository. ```shell # install git and a C++ compiler sudo yum install -y git gcc gcc-c++ # install rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh . "$HOME/.cargo/env" # install uv curl -LsSf https://astral.sh/uv/install.sh | sh # install cargo-criterion for benchmarks cargo install cargo-criterion # clone rapidhash git clone https://github.com/hoxxep/rapidhash.git cd rapidhash ``` -------------------------------- ### Install and Preview Documentation Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Installs `cargo-docs` and then previews the project's documentation with specific `rustdoc` flags. ```shell # Install cargo-docs cargo install cargo-docs # Preview the documentation RUSTDOCFLAGS="--cfg docsrs" cargo +nightly docs -- --all-features ``` -------------------------------- ### Run CLI Example from File Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Executes the `cli` example, taking input data from a specified file. ```shell # From file cargo run --example cli -- example.txt ``` -------------------------------- ### Run CLI Example from Stdin Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Executes the `cli` example, piping input data from standard input. ```shell # From stdin echo "example" | cargo run --example cli ``` -------------------------------- ### Install Rapidhash CLI Source: https://github.com/hoxxep/rapidhash/blob/master/README.md Install the rapidhash command-line tool using cargo. This tool can hash files or standard input. ```shell cargo install rapidhash ``` -------------------------------- ### Install and Use Rapidhash CLI Source: https://github.com/hoxxep/rapidhash/blob/master/rapidhash/README.md Instructions for installing the rapidhash CLI tool and using it to hash files or standard input with the V3 algorithm. ```shell # install cargo install rapidhash # hash a file (output: 8543579700415218186) rapidhash --v3 example.txt # hash stdin (output: 8543579700415218186) echo "example" | rapidhash --v3 ``` -------------------------------- ### Example Usage of RapidHashMap Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Illustrates a complete example of creating, populating, and iterating over a RapidHashMap, showing practical application of its methods. ```rust use rapidhash::RapidHashMap; fn main() { let mut map = RapidHashMap::with_capacity(10); map.insert("Alice", 30); map.insert("Bob", 25); map.insert("Charlie", 35); if let Some(age) = map.get("Alice") { println!("Alice is {} years old", age); } for (name, age) in &map { println!("{}: {}", name, age); } } ``` -------------------------------- ### Example Usage of RapidHashSet Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Illustrates a complete example of creating, populating, checking for elements, and iterating over a RapidHashSet, showing practical application of its methods. ```rust use rapidhash::RapidHashSet; fn main() { let mut colors = RapidHashSet::with_capacity(10); colors.insert("red"); colors.insert("blue"); colors.insert("green"); if colors.contains("red") { println!("Red is in the set"); } for color in &colors { println!("{}", color); } } ``` -------------------------------- ### Enable Doctests Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md Run this command to enable and execute all documentation tests, ensuring examples are up-to-date. ```bash cargo test --doc ``` -------------------------------- ### Compile-Time Hashing Example Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/types.md Shows an example of using `const` functions to perform hashing at compile time, utilizing `RapidSecrets` and `rapidhash_v3_inline`. ```rust const SECRETS: RapidSecrets = RapidSecrets::seed(0x123456); const fn hash_at_compile_time() -> u64 { rapidhash::v3::rapidhash_v3_inline::(b"test", &SECRETS) } ``` -------------------------------- ### Example .cargo/config.toml for nightly feature Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md This configuration snippet for .cargo/config.toml is used in conjunction with the 'nightly' feature to enable native CPU tuning for potentially better performance. ```toml [build] rustflags = ["-Z", "tune-cpu=native"] ``` -------------------------------- ### Run All Benchmarks Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Executes all benchmarks within the `rapidhash-bench` crate using `cargo-criterion`. Assumes `cargo-criterion` is installed. ```shell # Run in the bench crate cd rapidhash-bench # Run all benchmarks (assumes cargo-criterion is installed) RUSTFLAGS="-C target-cpu=native" cargo criterion --plotting-backend disabled --bench bench --features bench ``` -------------------------------- ### No-Std Compatibility Example Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md Demonstrates basic `no_std` usage by disabling default features and using `rapidhash_v3`. Requires `core::alloc` if `RapidSecrets::random()` is used. ```rust #![no_std] extern crate alloc; use rapidhash::v3::rapidhash_v3; let hash = rapidhash_v3(b"test"); ``` -------------------------------- ### Create and Use RapidHasher (Fast) Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md Demonstrates creating a new RapidHasher with a custom seed, writing bytes, and finishing to get the hash value. This is for the speed-optimized variant. ```rust use rapidhash::fast::RapidHasher; use std::hash::Hasher; let mut hasher = RapidHasher::new(0); hasher.write(b"hello world"); let hash = hasher.finish(); println!("{}", hash); ``` -------------------------------- ### Example usage of RapidRng with rng feature Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md Demonstrates how to use RapidRng for random number generation when the 'rng' feature is enabled. This example seeds the RNG and generates a random value within a range. ```rust use rapidhash::rng::RapidRng; use rand::Rng; fn main() { let mut rng = RapidRng::seed_from_u64(42); let value: u32 = rng.gen_range(0..100); } ``` -------------------------------- ### Run IAI-Callgrind Benchmarks Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Compares instruction counts and L1 cache misses using `iai-callgrind`. Requires `valgrind` to be installed. ```shell # Run iai-callgrind to compare instruction counts and L1 cache misses # Requires: valgrind RUSTFLAGS="-C target-cpu=native" cargo bench --bench iai-callgrind --features bench ``` -------------------------------- ### Fuzz Raw Rapidhash Method Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Fuzzes the raw `rapidhash` method using `cargo-fuzz`. Assumes `cargo-fuzz` is installed. ```shell # fuzz the raw rapidhash method. (assumes cargo-fuzz is installed) cargo +nightly fuzz run rapidhash ``` -------------------------------- ### Customizing RapidHasher with Compile-Time Parameters Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md This example shows how to define a custom hasher type by enabling specific compile-time protections using const generics. This is useful for tailoring the hasher's quality and performance characteristics. ```rust use rapidhash::inner::RapidHasher; // Custom hasher with all protections enabled type ProtectedHasher<'s> = RapidHasher<'s, true, true, false, true>; ``` -------------------------------- ### Standard (Default) configuration Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md This is the recommended configuration for most applications, as it includes the standard library support, all collection types, and file I/O functions by default. ```toml [dependencies] rapidhash = "4.4" ``` -------------------------------- ### Create and Use RandomState for HashMap Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md Shows how to create a RandomState instance and use it to initialize a HashMap. This is for the fast build hasher. ```rust use rapidhash::fast::RandomState; use std::collections::HashMap; let state = RandomState::new(); let map: HashMap = HashMap::with_hasher(state); ``` -------------------------------- ### Fuzz with AFL Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Performs fuzzing using AFL (American Fuzzy Lop). Assumes `cargo-afl` is installed. ```shell # use AFL fuzzing. (assumes cargo-afl is installed) cargo afl fuzz -i in -o out target/debug/afl_rapidhash ``` -------------------------------- ### Use Fast HashMap Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/00-START-HERE.md Demonstrates how to create and insert elements into a `RapidHashMap` for fast in-memory hashing. ```rust use rapidhash::RapidHashMap; let mut map = RapidHashMap::new(); map.insert("key", "value"); ``` -------------------------------- ### Hashing a File with rapidhash_v3_file Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Demonstrates how to hash the contents of a file using the rapidhash_v3_file function. Requires opening the file first. ```rust use rapidhash::v3::{rapidhash_v3_file, RapidSecrets}; let mut file = File::open("data.bin")?; let hash = rapidhash_v3_file(&mut file)?; ``` -------------------------------- ### Generate Fast Deterministic Sequence Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/rng.md Create a vector of `u64` random numbers using `rapidrng_fast` in a deterministic sequence, starting from a seed of 0. ```rust use rapidhash::rng::rapidrng_fast; fn generate_sequence(count: usize) -> Vec { let mut seed = 0u64; (0..count) .map(|_| rapidrng_fast(&mut seed)) .collect() } ``` -------------------------------- ### Binary Size Optimization for Release Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md Optimize for binary size in release builds by setting `opt-level = "z"` and enabling `strip`. This may reduce performance due to less inlining. ```toml [profile.release] opt-level = "z" # Optimize for size lto = "fat" codegen-units = 1 strip = true ``` -------------------------------- ### Hash Bytes with rapidhash_v3 Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/portable-hashing.md Hash a byte slice using the default rapidhash V3 algorithm. This is the simplest way to get a reproducible hash. ```rust use rapidhash::v3::rapidhash_v3; let hash = rapidhash_v3(b"hello world"); println!("Hash: {}", hash); ``` -------------------------------- ### RapidHashSet Construction Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Demonstrates the different ways to construct a `RapidHashSet`. ```APIDOC ## RapidHashSet Construction ### Default Constructor ```rust use rapidhash::RapidHashSet; let mut set: RapidHashSet = RapidHashSet::default(); set.insert("item".to_string()); ``` **Returns:** Empty `RapidHashSet` with default capacity ### New Constructor ```rust use rapidhash::RapidHashSet; let mut set = RapidHashSet::new(); set.insert("item"); ``` **Returns:** Empty `RapidHashSet` with default capacity ### With Capacity ```rust use rapidhash::RapidHashSet; let mut set = RapidHashSet::with_capacity(50); set.insert("item"); ``` **Parameters:** | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | capacity | usize | Yes | Initial capacity | **Returns:** Empty `RapidHashSet` with specified capacity ``` -------------------------------- ### Direct In-Memory Hashing with SeedableState Source: https://github.com/hoxxep/rapidhash/blob/master/README.md Utilize SeedableState to directly use the RapidHasher for in-memory hashing. This example demonstrates hashing a byte slice and asserting the output. ```rust use std::hash::BuildHasher; use rapidhash::quality::SeedableState; // Using the RapidHasher directly for in-memory hashing. let hasher = SeedableState::fixed(); assert_eq!(hasher.hash_one(b"hello world"), 3348275917668072623); ``` -------------------------------- ### Importing Rapidhash Types Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/types.md Demonstrates how to import various types from different modules within the Rapidhash library for common usage patterns. ```rust use rapidhash::fast::{RapidHasher, RandomState, GlobalState}; use rapidhash::quality::{RapidHasher as QualityHasher, RandomState as QualityRandomState}; use rapidhash::v3::{rapidhash_v3, RapidSecrets, RapidStreamHasherV3}; use rapidhash::{RapidHashMap, RapidHashSet}; use rapidhash::rng::RapidRng; ``` -------------------------------- ### Generate Random Numbers with RapidRng Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/INDEX.md Use RapidRng for generating random numbers. It provides a default implementation that can be used for various randomization needs. Call next() to get a random value. ```rust use rapidhash::rng::RapidRng; let mut rng = RapidRng::default(); let value = rng.next(); ``` -------------------------------- ### RapidHashMap Construction Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Demonstrates the different ways to construct a `RapidHashMap`. ```APIDOC ## RapidHashMap Construction ### Default Constructor ```rust use rapidhash::RapidHashMap; let mut map: RapidHashMap = RapidHashMap::default(); map.insert("key".to_string(), 42); ``` **Returns:** Empty `RapidHashMap` with default capacity ### New Constructor ```rust use rapidhash::RapidHashMap; let mut map = RapidHashMap::new(); map.insert("key", 42); ``` **Returns:** Empty `RapidHashMap` with default capacity ### With Capacity ```rust use rapidhash::RapidHashMap; let mut map = RapidHashMap::with_capacity(100); map.insert("key", 42); ``` **Parameters:** | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | capacity | usize | Yes | Initial capacity | **Returns:** Empty `RapidHashMap` with specified capacity ``` -------------------------------- ### High-Performance Caching with RapidHashMap Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Demonstrates using `RapidHashMap` for high-performance caching. Pre-allocating capacity with `with_capacity` can improve performance for large caches. ```rust use rapidhash::RapidHashMap; fn cache_results() { let mut cache = RapidHashMap::with_capacity(1000); for key in 0..10000 { if !cache.contains_key(&key) { let value = compute_expensive_result(key); // Assume this function exists cache.insert(key, value); } } } ``` -------------------------------- ### Generate Benchmark Charts Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Uses a Python script executed via `uv run` to generate charts for the `--bench bench` data. ```shell # generate the --bench bench charts uv run generate_charts.py ``` -------------------------------- ### Recommended Release Profile Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md Use this TOML configuration for optimal release builds, balancing performance and compile times. It enables full LTO and single codegen units. ```toml [profile.release] opt-level = 3 lto = "fat" codegen-units = 1 ``` -------------------------------- ### Run Benchmarks with Nightly Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Runs all benchmarks using the nightly Rust toolchain, including the `nightly` feature. ```shell # Run all benchmarks, with nightly RUSTFLAGS="-C target-cpu=native" cargo +nightly criterion --plotting-backend disabled --bench bench --features bench,nightly ``` -------------------------------- ### Architecture-Specific Tuning for Release Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md Configure release builds for native CPU optimizations by setting `target-cpu=native`. This disables portability and is best for single-machine deployments. ```toml [profile.release] opt-level = 3 lto = "fat" codegen-units = 1 [build] rustflags = ["-C", "target-cpu=native"] ``` -------------------------------- ### Migrate from Rapidhash V2 to V3 Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/portable-hashing.md Demonstrates the migration from `rapidhash_v2_2_seeded` to the equivalent `rapidhash_v3_seeded` function. ```rust use rapidhash::v2::rapidhash_v2_2_seeded; let hash = rapidhash_v2_2_seeded(b"test", &secrets); // New use rapidhash::v3::rapidhash_v3_seeded; let hash = rapidhash_v3_seeded(b"test", &secrets); ``` -------------------------------- ### Fast Hashing with RapidHasher and HashMap Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Demonstrates direct use of RapidHasher for speed-optimized hashing and its integration with HashMap using RandomState. ```rust use rapidhash::fast::{RapidHasher, RandomState}; use std::collections::HashMap; use std::hash::Hasher; // Direct hasher let mut hasher = RapidHasher::default(); hasher.write(b"test"); let hash = hasher.finish(); // With HashMap let mut map: HashMap = HashMap::new(); map.insert("key".to_string(), 42); ``` -------------------------------- ### Quality Hashing with RapidHasher Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Shows how to use RapidHasher for quality-optimized hashing, suitable for algorithms benefiting from high-quality hash distributions. ```rust use rapidhash::quality::RapidHasher; use std::hash::Hasher; let mut hasher = RapidHasher::default(); hasher.write(b"test"); let hash = hasher.finish(); ``` -------------------------------- ### RandomState::new (Fast) Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md Creates a new `RandomState` instance, which acts as a `std::hash::BuildHasher`. It initializes `RapidHasher` with a random seed for each new instance, providing minimal DoS resistance. ```APIDOC ## RandomState::new (Fast) ### Description Creates a new `RandomState` instance, which acts as a `std::hash::BuildHasher`. It initializes `RapidHasher` with a random seed for each new instance, providing minimal DoS resistance. ### Method Signature `pub fn new() -> Self` ### Parameters #### Path Parameters * None #### Query Parameters * None #### Request Body * None ### Parameters * None ### Request Example ```rust use rapidhash::fast::RandomState; use std::collections::HashMap; let state = RandomState::new(); let map: HashMap = HashMap::with_hasher(state); ``` ### Response #### Success Response (200) * **Return Value**: `RandomState` - The newly created random state instance. #### Response Example ```rust // Example output (internal state will be randomized) let state = RandomState::new(); ``` ``` -------------------------------- ### Portable Hashing with Rapidhash V3 Source: https://github.com/hoxxep/rapidhash/blob/master/rapidhash/README.md Illustrates portable hashing using rapidhash V3 algorithms for bulk, streamed, and file-based data. Requires setting global hashing secrets. ```rust use rapidhash::v3::{rapidhash_v3_seeded, rapidhash_v3_file_seeded, RapidSecrets, RapidStreamHasherV3}; /// Set your global hashing secrets. /// - For HashDoS resistance, choose a randomized secret. /// - For C++ compatibility, use the `seed_cpp` method or `DEFAULT_RAPID_SECRETS`. const SECRETS: RapidSecrets = RapidSecrets::seed(0x123456); // Bulk: hash a complete byte slice. let bulk = rapidhash_v3_seeded(b"hello world", &SECRETS); // Stream: write chunks of any size, same output regardless of chunk boundaries. let mut hasher = RapidStreamHasherV3::new(&SECRETS); hasher.write(b"hello "); hasher.write(b"world"); let stream = hasher.finish(); // Read: hash from any `Read` source (files, cursors, etc.). let read = rapidhash_v3_file_seeded(std::io::Cursor::new(b"hello world"), &SECRETS).unwrap(); assert_eq!(bulk, stream); assert_eq!(bulk, read); ``` -------------------------------- ### Create RapidHasher with Default Seed Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md Shows how to create a RapidHasher using its default seed and then compute a hash. This is for the speed-optimized variant. ```rust use rapidhash::fast::RapidHasher; use std::hash::Hasher; let mut hasher = RapidHasher::default(); hasher.write(b"test"); let hash = hasher.finish(); ``` -------------------------------- ### Generate Benchmark Table Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Uses a Python script executed via `uv run` to generate a table for the `--bench realworld` data. ```shell # generate the --bench realworld table uv run generate_table.py ``` -------------------------------- ### Run Quality Benchmarks Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Executes quality tests across various hash functions using `cargo bench`. ```shell # Run quality tests across various hash functions RUSTFLAGS="-C target-cpu=native" cargo bench --bench quality --features bench ``` -------------------------------- ### RapidHashSet::new Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Creates a new RapidHashSet with default capacity. This is useful for initializing an empty set. ```APIDOC ## RapidHashSet::new ### Description Creates a new `RapidHashSet` with default capacity. ### Method Associated Function (constructor) ### Signature `fn new() -> Self` ### Returns An empty `RapidHashSet` with default capacity. ### Example ```rust use rapidhash::{RapidHashSet, HashSetExt}; let set = RapidHashSet::::new(); ``` ``` -------------------------------- ### Run Realworld Benchmark Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Executes the `realworld` benchmark, which is a modified version of the foldhash benchmarks. ```shell # Run the realworld benchmark, which is a modification of the foldhash benchmarks RUSTFLAGS="-C target-cpu=native" cargo criterion --plotting-backend disabled --bench realworld --features bench ``` -------------------------------- ### Minimal (no_std) configuration Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md This configuration is for embedded systems that do not use the standard library. It disables default features, making only portable hashing and specific hasher types available. ```toml [dependencies] rapidhash = { version = "4.4", default-features = false } ``` -------------------------------- ### Run All Tests Source: https://github.com/hoxxep/rapidhash/blob/master/docs/CHEATSHEET.md Executes all tests for the project, including those with all features enabled. ```shell # Run tests cargo +nightly test --all-features ``` -------------------------------- ### HashMap with Custom RandomState Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Illustrates creating a standard HashMap with a custom RandomState from the rapidhash::fast module for optimized hashing. ```rust use rapidhash::fast::RandomState; use std::collections::HashMap; let mut map: HashMap = HashMap::new(); ``` -------------------------------- ### Hash File with Rapidhash CLI Source: https://github.com/hoxxep/rapidhash/blob/master/README.md Hash a file using the rapidhash CLI tool with the --v3 flag. The output is the decimal u64 hash value. ```shell rapidhash --v3 example.txt ``` -------------------------------- ### RapidHashSet Methods Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Illustrates common operations available for `RapidHashSet`. ```APIDOC ## RapidHashSet Methods All standard `HashSet` methods are available. Common operations: ```rust use rapidhash::RapidHashSet; let mut set = RapidHashSet::new(); // Insert set.insert("apple"); // Contains let exists = set.contains("apple"); // Remove let removed = set.remove("apple"); // Iteration for item in &set { println!("{}", item); } // Length let size = set.len(); // Clear set.clear(); // Set operations let set1: RapidHashSet<_> = [1, 2, 3].iter().cloned().collect(); let set2: RapidHashSet<_> = [2, 3, 4].iter().cloned().collect(); let union = set1.union(&set2).cloned().collect::>(); let intersection = set1.intersection(&set2).cloned().collect::>(); let difference = set1.difference(&set2).cloned().collect::>(); ``` ``` -------------------------------- ### Portable Hashing Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/00-START-HERE.md Shows how to use the `rapidhash_v3` function to generate a stable hash that is consistent across different platforms. ```rust use rapidhash::v3::rapidhash_v3; let hash = rapidhash_v3(b"data"); // Same on x86, ARM, etc. ``` -------------------------------- ### Migrate from Rapidhash V1 to V3 Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/portable-hashing.md Shows how to update code from using `rapidhash_v1` to the newer `rapidhash_v3` function. ```rust use rapidhash::v1::rapidhash_v1; let hash = rapidhash_v1(b"test"); // New use rapidhash::v3::rapidhash_v3; let hash = rapidhash_v3(b"test"); ``` -------------------------------- ### Bench Profile Configuration Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md TOML configuration for the bench profile, optimized for accurate benchmarking with full LTO and single codegen units. ```toml [profile.bench] codegen-units = 1 lto = "fat" opt-level = 3 debug = true debug-assertions = false ``` -------------------------------- ### Portable Hashing with Rapidhash V3 Source: https://github.com/hoxxep/rapidhash/blob/master/README.md Demonstrates portable hashing using rapidhash V3 algorithms, including bulk, stream, and read-based hashing. Ensure to manage secrets appropriately for HashDoS resistance or C++ compatibility. ```rust use rapidhash::v3::{rapidhash_v3_seeded, rapidhash_v3_file_seeded, RapidSecrets, RapidStreamHasherV3}; /// Set your global hashing secrets. /// - For HashDoS resistance, choose a randomized secret. /// - For C++ compatibility, use the `seed_cpp` method or `DEFAULT_RAPID_SECRETS`. const SECRETS: RapidSecrets = RapidSecrets::seed(0x123456); // Bulk: hash a complete byte slice. let bulk = rapidhash_v3_seeded(b"hello world", &SECRETS); // Stream: write chunks of any size, same output regardless of chunk boundaries. let mut hasher = RapidStreamHasherV3::new(&SECRETS); hasher.write(b"hello "); hasher.write(b"world"); let stream = hasher.finish(); // Read: hash from any `Read` source (files, cursors, etc.). let read = rapidhash_v3_file_seeded(std::io::Cursor::new(b"hello world"), &SECRETS).unwrap(); assert_eq!(bulk, stream); assert_eq!(bulk, read); ``` -------------------------------- ### Custom Inner Hasher Configuration Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Illustrates creating a custom hasher configuration using the inner RapidHasher with specific compile-time parameters for quality and compactness. ```rust use rapidhash::inner::RapidHasher; use std::hash::Hasher; // Custom configuration: quality + compact type CustomHasher<'s> = RapidHasher<'s, true, true, true, false>; let mut hasher = CustomHasher::default(); hasher.write(b"test"); let hash = hasher.finish(); ``` -------------------------------- ### Using BuildHasher Trait with RandomState Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md Demonstrates how to use the `BuildHasher` trait with `RandomState` to create a hasher, write data to it, and finish the hashing process. It also shows the convenience of the `hash_one` method. ```rust use std::hash::{BuildHasher, Hasher}; use rapidhash::fast::RandomState; let state = RandomState::default(); let mut hasher = state.build_hasher(); hasher.write(b"test"); let hash = hasher.finish(); // or use hash_one convenience method let hash = state.hash_one(b"test"); ``` -------------------------------- ### RapidHashMap::new Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Creates a new RapidHashMap with default capacity. This is a convenient way to initialize an empty hash map. ```APIDOC ## RapidHashMap::new ### Description Creates a new `RapidHashMap` with default capacity. ### Method Associated Function (constructor) ### Signature `fn new() -> Self` ### Returns An empty `RapidHashMap` with default capacity. ### Example ```rust use rapidhash::{RapidHashMap, HashMapExt}; let map = RapidHashMap::::new(); ``` ``` -------------------------------- ### Construct RapidHashSet with New Constructor Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Creates an empty RapidHashSet with default capacity. Suitable for general use when no specific initial capacity is needed. ```rust use rapidhash::RapidHashSet; let mut set = RapidHashSet::new(); set.insert("item"); ``` -------------------------------- ### RapidHashMap and RapidHashSet with Capacity Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Shows how to create a RapidHashMap and RapidHashSet with a specified initial capacity using helper type aliases and traits. ```rust use rapidhash::{RapidHashMap, RapidHashSet, HashMapExt, HashSetExt}; let mut map = RapidHashMap::with_capacity(100); let mut set = RapidHashSet::with_capacity(50); ``` -------------------------------- ### aarch64 AWS Graviton3 Benchmark Results (target-cpu=native) Source: https://github.com/hoxxep/rapidhash/blob/master/README.md Benchmark results for rapidhash on aarch64 AWS Graviton3 with target-cpu=native optimization. Includes comparisons with gxhash, fxhash, ahash, and siphash. ```text ┌────────────────┬─────────────┬─────────────┬────────────┬────────────┬────────┬────────┬───────┬─────────┐ │ metric ┆ rapidhash-f ┆ rapidhash-q ┆ foldhash-f ┆ foldhash-q ┆ gxhash ┆ fxhash ┆ ahash ┆ siphash │ ╞════════════════╪═════════════╪═════════════╪════════════╪════════════╪════════╪════════╪═══════╪═════════╡ │ avg_rank ┆ 2.59 ┆ 4.20 ┆ 3.38 ┆ 5.28 ┆ 4.09 ┆ 2.50 ┆ 5.98 ┆ 7.97 │ │ geometric_mean ┆ 7.84 ┆ 8.97 ┆ 8.56 ┆ 9.68 ┆ 8.59 ┆ 8.15 ┆ 11.16 ┆ 32.59 │ └────────────────┴─────────────┴─────────────┴────────────┴────────────┴────────┴────────┴───────┴─────────┘ ``` -------------------------------- ### Verify Hashing Equivalence Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/stream-hasher.md Demonstrates that bulk hashing, single-write streaming, and multi-write streaming produce identical hash results for the same data. ```rust use rapidhash::v3::{rapidhash_v3, RapidStreamHasherV3, DEFAULT_RAPID_SECRETS}; let data = b"hello world"; // Bulk hashing let bulk_hash = rapidhash_v3(data); // Streaming hashing - single write let mut hasher = RapidStreamHasherV3::new(&DEFAULT_RAPID_SECRETS); hasher.write(data); let stream_hash = hasher.finish(); assert_eq!(bulk_hash, stream_hash); // Streaming hashing - multiple writes let mut hasher = RapidStreamHasherV3::new(&DEFAULT_RAPID_SECRETS); hasher.write(&data[..6]); hasher.write(&data[6..]); let stream_hash2 = hasher.finish(); assert_eq!(bulk_hash, stream_hash2); ``` -------------------------------- ### Stream/File Hashing Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/00-START-HERE.md Illustrates how to hash data from streams or files using `RapidStreamHasherV3`. This involves initializing the hasher, writing data chunks, and finalizing the hash. ```rust use rapidhash::v3::RapidStreamHasherV3; let mut hasher = RapidStreamHasherV3::new(&secrets); hasher.write(&chunk1); hasher.write(&chunk2); let hash = hasher.finish(); ``` -------------------------------- ### Write Multiple Byte Slices to RapidHasher Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/in-memory-hasher.md Demonstrates writing multiple byte slices sequentially to a RapidHasher. This is for the speed-optimized variant. ```rust use rapidhash::fast::RapidHasher; use std::hash::Hasher; let mut hasher = RapidHasher::default(); hasher.write(b"hello"); hasher.write(b"world"); ``` -------------------------------- ### File Hashing with RapidHash Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/README.md Hash the contents of a file using the `rapidhash_v3_file` function. Ensure the file is opened successfully and handle potential errors. ```rust use rapidhash::v3::rapidhash_v3_file; use std::fs::File; let mut file = File::open("data.bin")?; let hash = rapidhash_v3_file(&mut file)?; ``` -------------------------------- ### Frequency Counting with RapidHashMap Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Shows how to use `RapidHashMap` for frequency counting of words in a text. The `entry` API is used for efficient insertion and updates. ```rust use rapidhash::RapidHashMap; fn count_words(text: &str) -> RapidHashMap<&str, u32> { let mut counts = RapidHashMap::new(); for word in text.split_whitespace() { *counts.entry(word).or_insert(0) += 1; } counts } ``` -------------------------------- ### Maximum Performance configuration Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/configuration.md This configuration targets throughput-critical applications by enabling 'std', 'unsafe', and 'nightly' features. It also includes release profile settings for optimal performance. ```toml [dependencies] rapidhash = { version = "4.4", features = ["std", "unsafe", "nightly"] } [profile.release] lto = "fat" codegen-units = 1 opt-level = 3 ``` -------------------------------- ### Construct RapidHashSet with Default Constructor Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/api-reference/collections.md Creates an empty RapidHashSet with default capacity. Use when the initial size is unknown or not critical. ```rust use rapidhash::RapidHashSet; let mut set: RapidHashSet = RapidHashSet::default(); set.insert("item".to_string()); ``` -------------------------------- ### Streaming Data Hashing with RapidStreamHasherV3 Source: https://github.com/hoxxep/rapidhash/blob/master/_autodocs/MODULES.md Shows how to hash data in chunks using RapidStreamHasherV3. Initialize with default secrets and finish after writing all chunks. ```rust use rapidhash::v3::{RapidStreamHasherV3, DEFAULT_RAPID_SECRETS}; let mut hasher = RapidStreamHasherV3::new(&DEFAULT_RAPID_SECRETS); hasher.write(chunk1); hasher.write(chunk2); let hash = hasher.finish(); ``` -------------------------------- ### aarch64 Apple M1 Max Benchmark Results (target-cpu=native) Source: https://github.com/hoxxep/rapidhash/blob/master/README.md Benchmark results for rapidhash on aarch64 Apple M1 Max with target-cpu=native optimization. Compares rapidhash against gxhash, fxhash, ahash, and siphash. ```text ┌────────────────┬─────────────┬─────────────┬────────────┬────────────┬────────┬────────┬───────┬─────────┐ │ metric ┆ rapidhash-f ┆ rapidhash-q ┆ foldhash-f ┆ foldhash-q ┆ gxhash ┆ fxhash ┆ ahash ┆ siphash │ ╞════════════════╪═════════════╪═════════════╪════════════╪════════════╪════════╪════════╪═══════╪═════════╡ │ avg_rank ┆ 2.23 ┆ 3.94 ┆ 3.30 ┆ 5.08 ┆ 4.69 ┆ 3.16 ┆ 5.64 ┆ 7.97 │ │ geometric_mean ┆ 4.25 ┆ 4.79 ┆ 4.79 ┆ 5.19 ┆ 4.93 ┆ 5.48 ┆ 5.91 ┆ 21.99 │ └────────────────┴─────────────┴─────────────┴────────────┴────────────┴────────┴────────┴───────┴─────────┘ ``` -------------------------------- ### Use AFL for Fuzzing Source: https://github.com/hoxxep/rapidhash/blob/master/rapidhash/fuzz/README.md Integrate with AFL for fuzzing. This command assumes 'in' and 'out' directories are set up for input and output. ```shell # use AFL fuzzing. cargo afl fuzz -i in -o out target/debug/afl_rapidhash ```