diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin index be52ff285..cef33cbeb 100644 --- a/core/runtime/dynamic_map_internal.odin +++ b/core/runtime/dynamic_map_internal.odin @@ -5,11 +5,47 @@ _ :: intrinsics; INITIAL_MAP_CAP :: 16; +// Temporary data structure for comparing hashes and keys Map_Hash :: struct { hash: uintptr, key_ptr: rawptr, // address of Map_Entry_Header.key } +__get_map_hash :: proc "contextless" (k: ^$K) -> Map_Hash { + key := k; + map_hash: Map_Hash; + + T :: intrinsics.type_core_type(K); + + map_hash.key_ptr = k; + + when intrinsics.type_is_integer(T) { + map_hash.hash = default_hash_ptr(key, size_of(T)); + } else when intrinsics.type_is_rune(T) { + map_hash.hash = default_hash_ptr(key, size_of(T)); + } else when intrinsics.type_is_pointer(T) { + map_hash.hash = default_hash_ptr(key, size_of(T)); + } else when intrinsics.type_is_float(T) { + map_hash.hash = default_hash_ptr(key, size_of(T)); + } else when intrinsics.type_is_string(T) { + #assert(T == string); + str := (^string)(key)^; + map_hash.hash = default_hash_string(str); + } else { + #panic("Unhandled map key type"); + } + + return map_hash; +} + +__get_map_hash_from_entry :: proc "contextless" (h: Map_Header, entry: ^Map_Entry_Header) -> (hash: Map_Hash) { + hash.hash = entry.hash; + hash.key_ptr = rawptr(uintptr(entry) + h.key_offset); + return; +} + + + Map_Find_Result :: struct { hash_index: int, entry_prev: int, @@ -17,7 +53,7 @@ Map_Find_Result :: struct { } Map_Entry_Header :: struct { - hash: Map_Hash, + hash: uintptr, next: int, /* key: Key_Value, @@ -73,7 +109,7 @@ source_code_location_hash :: proc(s: Source_Code_Location) -> uintptr { __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header { header := Map_Header{m = (^Raw_Map)(m)}; Entry :: struct { - hash: Map_Hash, + hash: uintptr, next: int, key: K, value: V, @@ -93,33 +129,6 @@ __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header { return header; } -__get_map_hash :: proc "contextless" (k: ^$K) -> Map_Hash { - key := k; - map_hash: Map_Hash; - - T :: intrinsics.type_core_type(K); - - map_hash.key_ptr = k; - - when intrinsics.type_is_integer(T) { - map_hash.hash = default_hash_ptr(key, size_of(T)); - } else when intrinsics.type_is_rune(T) { - map_hash.hash = default_hash_ptr(key, size_of(T)); - } else when intrinsics.type_is_pointer(T) { - map_hash.hash = default_hash_ptr(key, size_of(T)); - } else when intrinsics.type_is_float(T) { - map_hash.hash = default_hash_ptr(key, size_of(T)); - } else when intrinsics.type_is_string(T) { - #assert(T == string); - str := (^string)(key)^; - map_hash.hash = default_hash_string(str); - } else { - #panic("Unhandled map key type"); - } - - return map_hash; -} - __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, loc := #caller_location) -> bool { array := (^Raw_Slice)(array_); @@ -141,17 +150,8 @@ __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, l return true; } -__dynamic_map_fix_keys :: proc(h: Map_Header) { - e := (^Map_Entry_Header)(h.m.entries.data); - for _ in 0.. Map_Find_Resu fr.entry_index = m.hashes[fr.hash_index]; for fr.entry_index >= 0 { entry := __dynamic_map_get_entry(h, fr.entry_index); - if __dynamic_map_hash_equal(h, entry.hash, hash) { + entry_hash := __get_map_hash_from_entry(h, entry); + if __dynamic_map_hash_equal(h, entry_hash, hash) { return fr; } fr.entry_prev = fr.entry_index; @@ -307,14 +307,10 @@ __dynamic_map_add_entry :: proc(using h: Map_Header, hash: Map_Hash, loc := #cal prev := m.entries.len; prev_data := m.entries.data; c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc); - if m.entries.data != prev_data { - __dynamic_map_fix_keys(h); - } if c != prev { end := __dynamic_map_get_entry(h, c-1); - end.hash.hash = hash.hash; - end.hash.key_ptr = rawptr(uintptr(end) + key_offset); - mem_copy(end.hash.key_ptr, hash.key_ptr, key_size); + end.hash = hash.hash; + mem_copy(rawptr(uintptr(end) + key_offset), hash.key_ptr, key_size); end.next = -1; } return prev; @@ -334,7 +330,6 @@ __dynamic_map_get_entry :: proc(using h: Map_Header, index: int) -> ^Map_Entry_H __dynamic_map_copy_entry :: proc(h: Map_Header, new, old: ^Map_Entry_Header) { mem_copy(new, old, h.entry_size); - new.hash.key_ptr = rawptr(uintptr(new) + h.key_offset); } __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds_check { @@ -352,7 +347,9 @@ __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds end := __dynamic_map_get_entry(h, m.entries.len-1); __dynamic_map_copy_entry(h, old, end); - if last := __dynamic_map_find(h, old.hash); last.entry_prev >= 0 { + old_hash := __get_map_hash_from_entry(h, old); + + if last := __dynamic_map_find(h, old_hash); last.entry_prev >= 0 { last_entry := __dynamic_map_get_entry(h, last.entry_prev); last_entry.next = fr.entry_index; } else { diff --git a/src/check_type.cpp b/src/check_type.cpp index bcc6b60db..2d2a5b3f0 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2799,9 +2799,9 @@ void init_map_entry_type(Type *type) { Scope *s = create_scope(builtin_pkg->scope); auto fields = array_make(permanent_allocator(), 0, 4); - array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hash")), t_map_hash, false, cast(i32)fields.count, EntityState_Resolved)); + array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("hash")), t_uintptr, false, cast(i32)fields.count, EntityState_Resolved)); array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, cast(i32)fields.count, EntityState_Resolved)); - array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), type->Map.key, false, cast(i32)fields.count, EntityState_Resolved)); + array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), type->Map.key, false, cast(i32)fields.count, EntityState_Resolved)); array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("value")), type->Map.value, false, cast(i32)fields.count, EntityState_Resolved));