From 40133ee5113245bcbfe23a927140a2765e22faf5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 7 Jun 2022 16:20:14 -0400 Subject: [PATCH] perf(cchash): only hash prefix of string/bytes --- src/core/CCHash.ml | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/core/CCHash.ml b/src/core/CCHash.ml index 181bf2c1..bf6edc45 100644 --- a/src/core/CCHash.ml +++ b/src/core/CCHash.ml @@ -86,13 +86,16 @@ let int32 (x:int32) = Hashtbl.hash x (* TODO: FNV *) let int64 (x:int64) = Hashtbl.hash x (* TODO: FNV *) let nativeint (x:nativeint) = Hashtbl.hash x +(* do not hash more than 128 bytes in strings/bytes *) +let max_len_b_ = 128 + let bytes (x:bytes) = let h = ref fnv_offset_basis in - Bytes.iter (fun c -> - h := Int64.(mul !h fnv_prime); - let byte = Char.code c in - h := Int64.(logxor !h (of_int byte)); - ) x; + for i = 0 to min max_len_b_ (Bytes.length x) do + h := Int64.(mul !h fnv_prime); + let byte = Char.code (Bytes.unsafe_get x i) in + h := Int64.(logxor !h (of_int byte)); + done; Int64.to_int !h land max_int let string (x:string) = bytes (Bytes.unsafe_of_string x)