aboutsummaryrefslogtreecommitdiff
path: root/hashmap.ha
diff options
context:
space:
mode:
authorPolesznyák Márk <contact@pml68.dev>2026-04-14 10:45:32 +0200
committerPolesznyák Márk <contact@pml68.dev>2026-04-14 23:33:36 +0200
commit8a4664ac4c0e3c68a6a32bf451d35452c9409f2b (patch)
treebd83026cca79d388ff072cbdc0a8cee89def56f8 /hashmap.ha
parentfeat: Hare updates (rt -> sys), fix sorting and parsing (diff)
downloadhare-1brc-8a4664ac4c0e3c68a6a32bf451d35452c9409f2b.tar.gz
feat: mmap + multithreading, down to 7s on i5 9400f
Diffstat (limited to 'hashmap.ha')
-rw-r--r--hashmap.ha39
1 files changed, 20 insertions, 19 deletions
diff --git a/hashmap.ha b/hashmap.ha
index 41235ea..81c9f94 100644
--- a/hashmap.ha
+++ b/hashmap.ha
@@ -1,28 +1,29 @@
-def BUCKETS: size = 1 << 17;
+def HASHMAP_SIZE: size = 1 << 14;
-type hashmap = [BUCKETS][](size, data);
+type hashmap = [HASHMAP_SIZE](size, stat);
-fn getitem(map: *hashmap, hash: size) data = {
- let bucket = &map[hash & (BUCKETS - 1)];
- for (let i = 0z; i < len(bucket); i += 1) {
- if (bucket[i].0 == hash)
- return bucket[i].1;
+fn getitem(map: *hashmap, hash: size) stat = {
+ let i = hash & (HASHMAP_SIZE - 1);
+ for (true) {
+ if (map[i].0 == 0 && map[i].1.count == 0)
+ break;
+ if (map[i].0 == hash)
+ return map[i].1;
+ i = (i + 1) & (HASHMAP_SIZE - 1);
};
- return data { ... };
+ return stat { ... };
};
-fn setitem(map: *hashmap, hash: size, value: data) void = {
- let bucket = &map[hash & (BUCKETS - 1)];
- for (let i = 0z; i < len(bucket); i += 1) {
- if (bucket[i].0 == hash) {
- bucket[i].1 = value;
+fn setitem(map: *hashmap, hash: size, value: stat) void = {
+ let i = hash & (HASHMAP_SIZE - 1);
+ for (true) {
+ if (map[i].0 == 0 && map[i].1.count == 0)
+ break;
+ if (map[i].0 == hash) {
+ map[i].1 = value;
return;
};
+ i = (i + 1) & (HASHMAP_SIZE - 1);
};
- append(bucket, (hash, value))!;
-};
-
-fn finish(map: *hashmap) void = {
- for (let i = 0z; i < len(map); i += 1)
- free(map[i]);
+ map[i] = (hash, value);
};