Browse Source

Make hash internal key be `uintptr` rather than `u64` to reduce entry size

gingerBill 4 years ago
parent
commit
a55568b0c4
4 changed files with 44 additions and 37 deletions
  1. 1 1
      core/runtime/core_builtin.odin
  2. 35 33
      core/runtime/dynamic_map_internal.odin
  3. 4 1
      src/ir.cpp
  4. 4 2
      src/llvm_backend.cpp

+ 1 - 1
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));
 	}
 }
 

+ 35 - 33
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 {

+ 4 - 1
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;

+ 4 - 2
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);
 		}
 	}