From a6b6d1df69e7a33ab163405ed79e6176b1d83d2a Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Sun, 19 Jul 2026 13:42:36 -0300 Subject: [PATCH] Simplify Sha1 shard key extraction and drop its unwrap The Sha1 sharder built a hex string of the whole digest just to parse its tail back into an integer, which introduced a fallible i64::from_str_radix and an .unwrap() that could panic on malformed input. Since a SHA1 digest is already raw bytes, the same value can be read directly. Read the shard key from the last four bytes of the digest with u32::from_be_bytes instead of formatting to hex and parsing the tail with i64::from_str_radix. This removes the allocation, the fallible parse, and the unwrap. The same bytes are selected (the previous code took the last eight hex characters), so shard assignments are unchanged. --- src/sharding.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/sharding.rs b/src/sharding.rs index 990f967b0..e599ebdd6 100644 --- a/src/sharding.rs +++ b/src/sharding.rs @@ -66,13 +66,9 @@ impl Sharder { hasher.update(key.to_string().as_bytes()); - let result = hasher.finalize(); + let digest = hasher.finalize(); - // Convert the SHA1 hash into hex so we can parse it as a large integer. - let hex = format!("{:x}", result); - - // Parse the last 8 bytes as an integer (8 bytes = bigint). - let key = i64::from_str_radix(&hex[hex.len() - 8..], 16).unwrap() as usize; + let key = u32::from_be_bytes([digest[16], digest[17], digest[18], digest[19]]) as usize; key % self.shards }