mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
perf(cchash): only hash prefix of string/bytes
This commit is contained in:
parent
e7dad1b54a
commit
40133ee511
1 changed files with 8 additions and 5 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue