From 3dc61ce2cb05f5aa5c8a996d2f841568a26141b8 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 16 Sep 2025 22:28:20 +0800 Subject: [PATCH] Have a hash64 function for all hash-related maps --- ds/map/map_fnv/del.ha | 6 +----- ds/map/map_fnv/get.ha | 6 +----- ds/map/map_fnv/internal.ha | 8 ++++++++ ds/map/map_fnv/set.ha | 6 +----- ds/map/map_siphash/del.ha | 5 +---- ds/map/map_siphash/get.ha | 5 +---- ds/map/map_siphash/internal.ha | 9 +++++++++ ds/map/map_siphash/set.ha | 5 +---- ds/map/map_swiss_siphash/del.ha | 2 +- ds/map/map_swiss_siphash/get.ha | 2 +- ds/map/map_swiss_siphash/internal.ha | 6 +++--- diff --git a/ds/map/map_fnv/del.ha b/ds/map/map_fnv/del.ha index 95cad53e907a3a38cfd7e254df10d360b8c95ee8..fca01b176fca8d080c9bad231482c2c2eaecce04 100644 --- a/ds/map/map_fnv/del.ha +++ b/ds/map/map_fnv/del.ha @@ -2,14 +2,10 @@ // SPDX-License-Identifier: MPL-2.0 // SPDX-FileCopyrightText: 2024 Drew DeVault // SPDX-FileCopyrightText: 2025 Runxi Yu -use hash; -use hash::fnv; use ds::map; // Deletes an item from a [[map]]. export fn del(m: *map, key: []u8) (*opaque | void) = { - let h = fnv::fnv64a(); - hash::write(&h, key); - let b = m.buckets[fnv::sum64(&h): size % m.n]; + let b = m.buckets[hash64(m, key) % m.n]; return map::del(b, key); }; diff --git a/ds/map/map_fnv/get.ha b/ds/map/map_fnv/get.ha index 9437e7126f440821d7fcf8ca16f109618d028f41..097c3b764a6223e9d27b8716ac4d73a4a7c7e897 100644 --- a/ds/map/map_fnv/get.ha +++ b/ds/map/map_fnv/get.ha @@ -2,14 +2,10 @@ // SPDX-License-Identifier: MPL-2.0 // SPDX-FileCopyrightText: 2024 Drew DeVault // SPDX-FileCopyrightText: 2025 Runxi Yu -use hash; -use hash::fnv; use ds::map; // Gets an item from a [[map]] by key, returning void if not found. export fn get(m: *map, key: []u8) (*opaque | void) = { - let h = fnv::fnv64a(); - hash::write(&h, key); - let b = m.buckets[fnv::sum64(&h): size % m.n]; + let b = m.buckets[hash64(m, key) % m.n]; return map::get(b, key); }; diff --git a/ds/map/map_fnv/internal.ha b/ds/map/map_fnv/internal.ha new file mode 100644 index 0000000000000000000000000000000000000000..aff1fd56de99c7878a66500db903da55ff7fb0ac --- /dev/null +++ b/ds/map/map_fnv/internal.ha @@ -0,0 +1,8 @@ +use hash; +use hash::fnv; + +fn hash64(m: *map, key: []u8) size = { + let h = fnv::fnv64a(); + hash::write(&h, key); + return fnv::sum64(&h): size; +}; diff --git a/ds/map/map_fnv/set.ha b/ds/map/map_fnv/set.ha index 918db1f9de7e831d8dc6a8d0d3b490d534313505..1c4dcd57fe87ee18b82a3a06a6b2315aa59c56ad 100644 --- a/ds/map/map_fnv/set.ha +++ b/ds/map/map_fnv/set.ha @@ -2,14 +2,10 @@ // SPDX-License-Identifier: MPL-2.0 // SPDX-FileCopyrightText: 2024 Drew DeVault // SPDX-FileCopyrightText: 2025 Runxi Yu -use hash; -use hash::fnv; use ds::map; // Sets an item in a [[map]], replacing any existing item with the same key. export fn set(m: *map, key: []u8, value: *opaque) (void | nomem) = { - let h = fnv::fnv64a(); - hash::write(&h, key); - let b = m.buckets[fnv::sum64(&h): size % m.n]; + let b = m.buckets[hash64(m, key) % m.n]; return map::set(b, key, value); }; diff --git a/ds/map/map_siphash/del.ha b/ds/map/map_siphash/del.ha index 4847c67dadfb714a6d185760c3aa6266515f868f..b3c24b78ded3a6ff5af0b9b8cc50dddb20b3c1d3 100644 --- a/ds/map/map_siphash/del.ha +++ b/ds/map/map_siphash/del.ha @@ -9,9 +9,6 @@ use ds::map; // Deletes an item from a [[map]]. export fn del(m: *map, key: []u8) (*opaque | void) = { - let h = siphash::siphash(2, 4, &m.siphash_key); - defer hash::close(&h); - hash::write(&h, key); - let b = m.buckets[siphash::sum(&h): size % m.n]; + let b = m.buckets[hash64(m, key) % m.n]; return map::del(b, key); }; diff --git a/ds/map/map_siphash/get.ha b/ds/map/map_siphash/get.ha index ad306bdf69ec900456135204442966c39cda975c..4a554b20720ee1fcbed21c8eae003b9bba5a3423 100644 --- a/ds/map/map_siphash/get.ha +++ b/ds/map/map_siphash/get.ha @@ -9,9 +9,6 @@ use ds::map; // Gets an item from a [[map]] by key, returning void if not found. export fn get(m: *map, key: []u8) (*opaque | void) = { - let h = siphash::siphash(2, 4, &m.siphash_key); - defer hash::close(&h); - hash::write(&h, key); - let b = m.buckets[siphash::sum(&h): size % m.n]; + let b = m.buckets[hash64(m, key) % m.n]; return map::get(b, key); }; diff --git a/ds/map/map_siphash/internal.ha b/ds/map/map_siphash/internal.ha new file mode 100644 index 0000000000000000000000000000000000000000..63b4bc83156c1c1c982473770f28a5bef8271793 --- /dev/null +++ b/ds/map/map_siphash/internal.ha @@ -0,0 +1,9 @@ +use hash; +use hash::siphash; + +fn hash64(m: *map, key: []u8) size = { + let h = siphash::siphash(2, 4, &m.siphash_key); + defer hash::close(&h); + hash::write(&h, key); + return siphash::sum(&h): size; +}; diff --git a/ds/map/map_siphash/set.ha b/ds/map/map_siphash/set.ha index 9b5bc767fe67f338df318db1ac6a1e14833ef703..903f2869a72df1869273236e9044065a226b3a79 100644 --- a/ds/map/map_siphash/set.ha +++ b/ds/map/map_siphash/set.ha @@ -9,9 +9,6 @@ use ds::map; // Sets an item in a [[map]], replacing any existing item with the same key. export fn set(m: *map, key: []u8, value: *opaque) (void | nomem) = { - let h = siphash::siphash(2, 4, &m.siphash_key); - defer hash::close(&h); - hash::write(&h, key); - let b = m.buckets[siphash::sum(&h): size % m.n]; + let b = m.buckets[hash64(m, key) % m.n]; return map::set(b, key, value); }; diff --git a/ds/map/map_swiss_siphash/del.ha b/ds/map/map_swiss_siphash/del.ha index 6be3062c7393342d00db2451f8853027872ba0cf..24d45ff8a9dafddfe70a290663e49471b3f58128 100644 --- a/ds/map/map_swiss_siphash/del.ha +++ b/ds/map/map_swiss_siphash/del.ha @@ -7,7 +7,7 @@ // Deletes an item from a [[map]]. Returns the removed value or void. export fn del(m: *map, key: []u8) (*opaque | void) = { if (len(m.groups) == 0) return; - let hv = hash64(m, key); + let hv = hash64(m, key): u64; let t = h2(hv); let mask = m.group_mask; let off: size = (h1(hv): size) & mask; diff --git a/ds/map/map_swiss_siphash/get.ha b/ds/map/map_swiss_siphash/get.ha index daa9e0b3aa7c13ceebebbd614ec769b7cf200e5b..e44a8dbcbb51da74b5f87f660c5f9c7109a30ac8 100644 --- a/ds/map/map_swiss_siphash/get.ha +++ b/ds/map/map_swiss_siphash/get.ha @@ -7,7 +7,7 @@ // Gets an item from a [[map]] by key, returning void if not found. export fn get(m: *map, key: []u8) (*opaque | void) = { if (len(m.groups) == 0) return; - let hv = hash64(m, key); + let hv = hash64(m, key): u64; let t = h2(hv); let mask = m.group_mask; let off: size = (h1(hv): size) & mask; diff --git a/ds/map/map_swiss_siphash/internal.ha b/ds/map/map_swiss_siphash/internal.ha index 09fd5e9970e03c96ee3527bfbedaa39f80e3944b..13338f93cf912317adfa569c8246e0fe8ff6b175 100644 --- a/ds/map/map_swiss_siphash/internal.ha +++ b/ds/map/map_swiss_siphash/internal.ha @@ -26,11 +26,11 @@ }; fn is_full_ctrl(c: u8) bool = (c & 0x80) == 0 && c != CTRL_DELETED; -fn hash64(m: *map, key: []u8) u64 = { +fn hash64(m: *map, key: []u8) size = { let h = siphash::siphash(2, 4, &m.siphash_key); defer hash::close(&h); hash::write(&h, key); - return siphash::sum(&h); + return siphash::sum(&h): size; }; fn h1(h: u64) u64 = h >> 7u64; @@ -111,7 +111,7 @@ }; }; fn unchecked_put(m: *map, key: []u8, val: nullable *opaque) void = { - let hv = hash64(m, key); + let hv = hash64(m, key): u64; let t = h2(hv); let mask = m.group_mask; let off: size = (h1(hv): size) & mask; -- 2.48.1