perf(cchash): only hash prefix of string/bytes

This commit is contained in:
Simon Cruanes 2022-06-07 16:20:14 -04:00
parent e7dad1b54a
commit 40133ee511
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -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)