From cc87d0349f584ba1d9f5d79727f1b9d7df1c1eec Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Mon, 20 Jul 2026 10:45:54 -0300 Subject: [PATCH] Compute SHA1 shard key from raw digest bytes The SHA1 sharding hash formatted the full 20-byte digest as a hex string, then parsed the last 8 hex characters back into an integer on every call. That allocated a string and ran a radix parse to recover bytes already sitting in the digest. Read the last four bytes directly and assemble the key with u32::from_be_bytes. The last 8 hex characters map to bytes 16 through 19, so the shard assignment is unchanged; only the work to extract it goes away. This also drops the unwrap on the parse. --- pgdog/src/frontend/router/sharding/hasher.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pgdog/src/frontend/router/sharding/hasher.rs b/pgdog/src/frontend/router/sharding/hasher.rs index 78bc4183a..4121fefc2 100644 --- a/pgdog/src/frontend/router/sharding/hasher.rs +++ b/pgdog/src/frontend/router/sharding/hasher.rs @@ -38,10 +38,7 @@ impl Hasher { hasher.update(bytes); let hash = hasher.finalize(); - let hex = format!("{:x}", hash); - let key = i64::from_str_radix(&hex[hex.len() - 8..], 16).unwrap(); - - key as u64 + u32::from_be_bytes([hash[16], hash[17], hash[18], hash[19]]) as u64 } }