From 211455cb398aa0a6d7b12e81941d4a062af54d8e Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Tue, 24 Mar 2026 17:38:28 -0600 Subject: [PATCH 1/2] refactor!: use split_first_chunk to help optimizer I'm not interested in performance, actually. The reason I need the optimizer to do well here is to eliminate a panic condition. I'm trying to write a no-panic hash table and tried using rustc-hash and discovered this. --- Cargo.toml | 1 + src/lib.rs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b8612d5..8a5ca72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" keywords = ["hash", "hasher", "fxhash", "rustc"] repository = "https://github.com/rust-lang/rustc-hash" edition = "2021" +rust-version = "1.77" [features] default = ["std"] diff --git a/src/lib.rs b/src/lib.rs index 0bff13e..25b7eaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -308,10 +308,10 @@ fn hash_bytes(bytes: &[u8]) -> u64 { } } else { // Handle bulk (can partially overlap with suffix). - let mut off = 0; - while off < len - 16 { - let x = u64::from_le_bytes(bytes[off..off + 8].try_into().unwrap()); - let y = u64::from_le_bytes(bytes[off + 8..off + 16].try_into().unwrap()); + let mut bulk = &bytes[0..(len - 1)]; + while let Some((chunk, rest)) = bulk.split_first_chunk::<16>() { + let x = u64::from_le_bytes((&chunk[..8]).try_into().unwrap()); + let y = u64::from_le_bytes((&chunk[8..]).try_into().unwrap()); // Replace s1 with a mix of s0, x, and y, and s0 with s1. // This ensures the compiler can unroll this loop into two @@ -322,7 +322,7 @@ fn hash_bytes(bytes: &[u8]) -> u64 { let t = multiply_mix(s0 ^ x, PREVENT_TRIVIAL_ZERO_COLLAPSE ^ y); s0 = s1; s1 = t; - off += 16; + bulk = rest; } let suffix = &bytes[len - 16..]; From f061387ad7da880a8bf5ce7e5c76aa863827520f Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Wed, 25 Mar 2026 11:16:48 -0600 Subject: [PATCH 2/2] style: use consistent range format --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 25b7eaf..b3270c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -308,7 +308,7 @@ fn hash_bytes(bytes: &[u8]) -> u64 { } } else { // Handle bulk (can partially overlap with suffix). - let mut bulk = &bytes[0..(len - 1)]; + let mut bulk = &bytes[..(len - 1)]; while let Some((chunk, rest)) = bulk.split_first_chunk::<16>() { let x = u64::from_le_bytes((&chunk[..8]).try_into().unwrap()); let y = u64::from_le_bytes((&chunk[8..]).try_into().unwrap());