aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-11-23 19:14:36 +0000
committergingerBill <bill@gingerbill.org>2020-11-23 19:14:36 +0000
commita55568b0c480b8a4ba7cf2f24ff2bb41cfc759ff (patch)
tree7f2634130ec16da3655fe292f036173abe4c64c2
parentb08ec005b29c28917cfe6ae664dcdb7b97f8f6b6 (diff)
Make hash internal key be `uintptr` rather than `u64` to reduce entry size
-rw-r--r--core/runtime/core_builtin.odin2
-rw-r--r--core/runtime/dynamic_map_internal.odin68
-rw-r--r--src/ir.cpp5
-rw-r--r--src/llvm_backend.cpp6
4 files changed, 44 insertions, 37 deletions
diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin
index fd6c7c9af..8a1be60d9 100644
--- a/core/runtime/core_builtin.odin
+++ b/core/runtime/core_builtin.odin
@@ -265,7 +265,7 @@ reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
if m != nil {
key := key;
- __dynamic_map_delete_key(__get_map_header(m), __get_map_key(&key));
+ __dynamic_map_delete_key(__get_map_header(m), __get_map_hash(&key));
}
}
diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin
index 7d93bb918..051d4073d 100644
--- a/core/runtime/dynamic_map_internal.odin
+++ b/core/runtime/dynamic_map_internal.odin
@@ -6,7 +6,7 @@ _ :: intrinsics;
INITIAL_MAP_CAP :: 16;
Map_Hash :: struct {
- hash: u64,
+ hash: uintptr,
key_ptr: rawptr, // address of Map_Entry_Header.key
}
@@ -39,6 +39,37 @@ Map_Header :: struct {
value_size: int,
}
+INITIAL_HASH_SEED :: 0xcbf29ce484222325;
+
+_fnv64a :: proc "contextless" (data: []byte, seed: u64 = INITIAL_HASH_SEED) -> u64 {
+ h: u64 = seed;
+ for b in data {
+ h = (h ~ u64(b)) * 0x100000001b3;
+ }
+ return h;
+}
+
+default_hash :: inline proc "contextless" (data: []byte) -> uintptr {
+ return uintptr(_fnv64a(data));
+}
+default_hash_string :: inline proc "contextless" (s: string) -> uintptr {
+ return default_hash(transmute([]byte)(s));
+}
+default_hash_ptr :: inline proc "contextless" (data: rawptr, size: int) -> uintptr {
+ s := Raw_Slice{data, size};
+ return default_hash(transmute([]byte)(s));
+}
+
+
+source_code_location_hash :: proc(s: Source_Code_Location) -> uintptr {
+ hash := _fnv64a(transmute([]byte)s.file_path);
+ hash = hash ~ (u64(s.line) * 0x100000001b3);
+ hash = hash ~ (u64(s.column) * 0x100000001b3);
+ return uintptr(hash);
+}
+
+
+
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
header := Map_Header{m = (^Raw_Map)(m)};
Entry :: struct {
@@ -58,10 +89,11 @@ __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
header.value_offset = uintptr(offset_of(Entry, value));
header.value_size = int(size_of(V));
+
return header;
}
-__get_map_key :: proc "contextless" (k: ^$K) -> Map_Hash {
+__get_map_hash :: proc "contextless" (k: ^$K) -> Map_Hash {
key := k;
map_hash: Map_Hash;
@@ -88,36 +120,6 @@ __get_map_key :: proc "contextless" (k: ^$K) -> Map_Hash {
return map_hash;
}
-_fnv64a :: proc "contextless" (data: []byte, seed: u64 = 0xcbf29ce484222325) -> u64 {
- h: u64 = seed;
- for b in data {
- h = (h ~ u64(b)) * 0x100000001b3;
- }
- return h;
-}
-
-
-default_hash :: inline proc "contextless" (data: []byte) -> u64 {
- return _fnv64a(data);
-}
-default_hash_string :: inline proc "contextless" (s: string) -> u64 {
- return default_hash(transmute([]byte)(s));
-}
-default_hash_ptr :: inline proc "contextless" (data: rawptr, size: int) -> u64 {
- s := Raw_Slice{data, size};
- return default_hash(transmute([]byte)(s));
-}
-
-
-source_code_location_hash :: proc(s: Source_Code_Location) -> u64 {
- hash := _fnv64a(transmute([]byte)s.file_path);
- hash = hash ~ (u64(s.line) * 0x100000001b3);
- hash = hash ~ (u64(s.column) * 0x100000001b3);
- return hash;
-}
-
-
-
__slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, loc := #caller_location) -> bool {
array := (^Raw_Slice)(array_);
@@ -275,7 +277,7 @@ __dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Hash) -> bool {
__dynamic_map_find :: proc(using h: Map_Header, hash: Map_Hash) -> Map_Find_Result #no_bounds_check {
fr := Map_Find_Result{-1, -1, -1};
- if n := u64(len(m.hashes)); n > 0 {
+ if n := uintptr(len(m.hashes)); n > 0 {
fr.hash_index = int(hash.hash % n);
fr.entry_index = m.hashes[fr.hash_index];
for fr.entry_index >= 0 {
diff --git a/src/ir.cpp b/src/ir.cpp
index e9f0b5647..b545e7ce3 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -3624,7 +3624,10 @@ irValue *ir_gen_map_hash(irProcedure *proc, irValue *key, Type *key_type) {
ExactValue ev = str->Constant.value;
GB_ASSERT(ev.kind == ExactValue_String);
u64 hs = fnv64a(ev.value_string.text, ev.value_string.len);
- hashed_str = ir_value_constant(t_u64, exact_value_u64(hs));
+ if (build_context.word_size == 4) {
+ hs &= 0xffffffff;
+ }
+ hashed_str = ir_value_constant(t_uintptr, exact_value_u64(hs));
} else {
auto args = array_make<irValue *>(ir_allocator(), 1);
args[0] = str;
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index f41f206b9..5bbccc18a 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -10272,7 +10272,10 @@ lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) {
if (lb_is_const(str)) {
String v = lb_get_const_string(p->module, str);
u64 hs = fnv64a(v.text, v.len);
- hashed_str = lb_const_int(p->module, t_u64, hs);
+ if (build_context.word_size == 4) {
+ hs &= 0xffffffff;
+ }
+ hashed_str = lb_const_int(p->module, t_uintptr, hs);
} else {
auto args = array_make<lbValue>(permanent_allocator(), 1);
args[0] = str;
@@ -10287,7 +10290,6 @@ lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) {
args[0] = lb_address_from_load_or_generate_local(p, key);
args[1] = lb_const_int(p->module, t_int, sz);
lbValue hash = lb_emit_runtime_call(p, "default_hash_ptr", args);
-
lb_emit_store(p, lb_emit_struct_ep(p, vp, 0), hash);
}
}