map type internal reorganization

This commit is contained in:
gingerBill
2020-11-23 16:56:31 +00:00
parent 67bc35e882
commit 4762d2f2d1
7 changed files with 66 additions and 67 deletions
-42
View File
@@ -351,48 +351,6 @@ Raw_Map :: struct {
entries: Raw_Dynamic_Array, entries: Raw_Dynamic_Array,
} }
INITIAL_MAP_CAP :: 16;
Map_Key :: struct {
hash: u64,
/* NOTE(bill)
size_of(Map_Key) == 16 Bytes on 32-bit systems
size_of(Map_Key) == 24 Bytes on 64-bit systems
This does mean that an extra word is wasted for each map when a string is not used on 64-bit systems
however, this is probably not a huge problem in terms of memory usage
*/
key: struct #raw_union {
str: string,
val: u64,
},
}
Map_Find_Result :: struct {
hash_index: int,
entry_prev: int,
entry_index: int,
}
Map_Entry_Header :: struct {
key: Map_Key,
next: int,
/*
value: Value_Type,
*/
}
Map_Header :: struct {
m: ^Raw_Map,
is_key_string: bool,
entry_size: int,
entry_align: int,
value_offset: uintptr,
value_size: int,
}
///////////////////////////// /////////////////////////////
// Init Startup Procedures // // Init Startup Procedures //
///////////////////////////// /////////////////////////////
+51 -9
View File
@@ -3,10 +3,52 @@ package runtime
import "intrinsics" import "intrinsics"
_ :: intrinsics; _ :: intrinsics;
INITIAL_MAP_CAP :: 16;
Map_Hash :: struct {
hash: u64,
/* NOTE(bill)
size_of(Map_Hash) == 16 Bytes on 32-bit systems
size_of(Map_Hash) == 24 Bytes on 64-bit systems
This does mean that an extra word is wasted for each map when a string is not used on 64-bit systems
however, this is probably not a huge problem in terms of memory usage
*/
key: struct #raw_union {
str: string,
val: u64,
},
}
Map_Find_Result :: struct {
hash_index: int,
entry_prev: int,
entry_index: int,
}
Map_Entry_Header :: struct {
key: Map_Hash,
next: int,
/*
value: Value_Type,
*/
}
Map_Header :: struct {
m: ^Raw_Map,
is_key_string: bool,
entry_size: int,
entry_align: int,
value_offset: uintptr,
value_size: int,
}
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header { __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
header := Map_Header{m = (^Raw_Map)(m)}; header := Map_Header{m = (^Raw_Map)(m)};
Entry :: struct { Entry :: struct {
key: Map_Key, key: Map_Hash,
next: int, next: int,
value: V, value: V,
}; };
@@ -19,9 +61,9 @@ __get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
return header; return header;
} }
__get_map_key :: proc "contextless" (k: $K) -> Map_Key { __get_map_key :: proc "contextless" (k: $K) -> Map_Hash {
key := k; key := k;
map_key: Map_Key; map_key: Map_Hash;
T :: intrinsics.type_core_type(K); T :: intrinsics.type_core_type(K);
@@ -169,7 +211,7 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #c
header.m^ = nm; header.m^ = nm;
} }
__dynamic_map_get :: proc(h: Map_Header, key: Map_Key) -> rawptr { __dynamic_map_get :: proc(h: Map_Header, key: Map_Hash) -> rawptr {
index := __dynamic_map_find(h, key).entry_index; index := __dynamic_map_find(h, key).entry_index;
if index >= 0 { if index >= 0 {
data := uintptr(__dynamic_map_get_entry(h, index)); data := uintptr(__dynamic_map_get_entry(h, index));
@@ -178,7 +220,7 @@ __dynamic_map_get :: proc(h: Map_Header, key: Map_Key) -> rawptr {
return nil; return nil;
} }
__dynamic_map_set :: proc(h: Map_Header, key: Map_Key, value: rawptr, loc := #caller_location) #no_bounds_check { __dynamic_map_set :: proc(h: Map_Header, key: Map_Hash, value: rawptr, loc := #caller_location) #no_bounds_check {
index: int; index: int;
assert(value != nil); assert(value != nil);
@@ -223,7 +265,7 @@ __dynamic_map_full :: inline proc(using h: Map_Header) -> bool {
} }
__dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Key) -> bool { __dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Hash) -> bool {
if a.hash == b.hash { if a.hash == b.hash {
if h.is_key_string { if h.is_key_string {
return a.key.str == b.key.str; return a.key.str == b.key.str;
@@ -235,7 +277,7 @@ __dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Key) -> bool {
return false; return false;
} }
__dynamic_map_find :: proc(using h: Map_Header, key: Map_Key) -> Map_Find_Result #no_bounds_check { __dynamic_map_find :: proc(using h: Map_Header, key: Map_Hash) -> Map_Find_Result #no_bounds_check {
fr := Map_Find_Result{-1, -1, -1}; fr := Map_Find_Result{-1, -1, -1};
if n := u64(len(m.hashes)); n > 0 { if n := u64(len(m.hashes)); n > 0 {
fr.hash_index = int(key.hash % n); fr.hash_index = int(key.hash % n);
@@ -252,7 +294,7 @@ __dynamic_map_find :: proc(using h: Map_Header, key: Map_Key) -> Map_Find_Result
return fr; return fr;
} }
__dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Key, loc := #caller_location) -> int { __dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Hash, loc := #caller_location) -> int {
prev := m.entries.len; prev := m.entries.len;
c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc); c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc);
if c != prev { if c != prev {
@@ -263,7 +305,7 @@ __dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Key, loc := #calle
return prev; return prev;
} }
__dynamic_map_delete_key :: proc(using h: Map_Header, key: Map_Key) { __dynamic_map_delete_key :: proc(using h: Map_Header, key: Map_Hash) {
fr := __dynamic_map_find(h, key); fr := __dynamic_map_find(h, key);
if fr.entry_index >= 0 { if fr.entry_index >= 0 {
__dynamic_map_erase(h, fr); __dynamic_map_erase(h, fr);
+8 -9
View File
@@ -2784,24 +2784,23 @@ void init_map_entry_type(Type *type) {
if (type->Map.entry_type != nullptr) return; if (type->Map.entry_type != nullptr) return;
// NOTE(bill): The preload types may have not been set yet // NOTE(bill): The preload types may have not been set yet
GB_ASSERT(t_map_key != nullptr); GB_ASSERT(t_map_hash != nullptr);
Type *entry_type = alloc_type_struct(); Type *entry_type = alloc_type_struct();
/* /*
struct { struct {
hash: __MapKey; key: runtime.Map_Key,
next: int; next: int,
key: Key; value: Value,
value: Value;
} }
*/ */
Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid); Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
Scope *s = create_scope(builtin_pkg->scope); Scope *s = create_scope(builtin_pkg->scope);
auto fields = array_make<Entity *>(permanent_allocator(), 0, 3); auto fields = array_make<Entity *>(permanent_allocator(), 0, 4);
array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), t_map_key, false, 0, EntityState_Resolved)); array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("key")), t_map_hash, false, cast(i32)fields.count, EntityState_Resolved));
array_add(&fields, alloc_entity_field(s, make_token_ident(str_lit("next")), t_int, false, 1, 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("value")), type->Map.value, false, 2, 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));
entry_type->Struct.fields = fields; entry_type->Struct.fields = fields;
+4 -4
View File
@@ -2278,14 +2278,14 @@ void init_core_source_code_location(Checker *c) {
} }
void init_core_map_type(Checker *c) { void init_core_map_type(Checker *c) {
if (t_map_key == nullptr) { if (t_map_hash == nullptr) {
Entity *e = find_core_entity(c, str_lit("Map_Key")); Entity *e = find_core_entity(c, str_lit("Map_Hash"));
if (e->state == EntityState_Unresolved) { if (e->state == EntityState_Unresolved) {
auto ctx = c->init_ctx; auto ctx = c->init_ctx;
check_entity_decl(&ctx, e, nullptr, nullptr); check_entity_decl(&ctx, e, nullptr, nullptr);
} }
t_map_key = e->type; t_map_hash = e->type;
GB_ASSERT(t_map_key != nullptr); GB_ASSERT(t_map_hash != nullptr);
} }
if (t_map_header == nullptr) { if (t_map_header == nullptr) {
+1 -1
View File
@@ -3608,7 +3608,7 @@ irValue *ir_gen_map_header(irProcedure *proc, irValue *map_val_ptr, Type *map_ty
irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) { irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) {
Type *hash_type = t_u64; Type *hash_type = t_u64;
irValue *v = ir_add_local_generated(proc, t_map_key, true); irValue *v = ir_add_local_generated(proc, t_map_hash, true);
Type *t = base_type(ir_type(key)); Type *t = base_type(ir_type(key));
key = ir_emit_conv(proc, key, key_type); key = ir_emit_conv(proc, key, key_type);
+1 -1
View File
@@ -10259,7 +10259,7 @@ lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) { lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) {
Type *hash_type = t_u64; Type *hash_type = t_u64;
lbAddr v = lb_add_local_generated(p, t_map_key, true); lbAddr v = lb_add_local_generated(p, t_map_hash, true);
lbValue vp = lb_addr_get_ptr(p, v); lbValue vp = lb_addr_get_ptr(p, v);
Type *t = base_type(key.type); Type *t = base_type(key.type);
key = lb_emit_conv(p, key, key_type); key = lb_emit_conv(p, key, key_type);
+1 -1
View File
@@ -684,7 +684,7 @@ gb_global Type *t_context_ptr = nullptr;
gb_global Type *t_source_code_location = nullptr; gb_global Type *t_source_code_location = nullptr;
gb_global Type *t_source_code_location_ptr = nullptr; gb_global Type *t_source_code_location_ptr = nullptr;
gb_global Type *t_map_key = nullptr; gb_global Type *t_map_hash = nullptr;
gb_global Type *t_map_header = nullptr; gb_global Type *t_map_header = nullptr;
gb_global Type *t_vector_x86_mmx = nullptr; gb_global Type *t_vector_x86_mmx = nullptr;