Change internal layout of map[K]V

This commit is contained in:
gingerBill
2020-11-23 18:25:01 +00:00
parent 4762d2f2d1
commit 91758656f6
6 changed files with 109 additions and 121 deletions
+3 -7
View File
@@ -1707,16 +1707,12 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
data := uintptr(entries.data) + uintptr(i*entry_size);
header := cast(^runtime.Map_Entry_Header)data;
if reflect.is_string(info.key) {
strings.write_string(fi.buf, header.key.key.str);
} else {
fi := Info{buf = fi.buf};
fmt_arg(&fi, any{rawptr(&header.key.key.val), info.key.id}, 'v');
}
key := data + entry_type.offsets[2];
fmt_arg(&Info{buf = fi.buf}, any{rawptr(key), info.key.id}, 'v');
strings.write_string(fi.buf, "=");
value := data + entry_type.offsets[2];
value := data + entry_type.offsets[3];
fmt_arg(fi, any{rawptr(value), info.value.id}, 'v');
}
}
+2 -1
View File
@@ -264,7 +264,8 @@ reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int) {
@builtin
delete_key :: proc(m: ^$T/map[$K]$V, key: K) {
if m != nil {
__dynamic_map_delete_key(__get_map_header(m), __get_map_key(key));
key := key;
__dynamic_map_delete_key(__get_map_header(m), __get_map_key(&key));
}
}
+59 -61
View File
@@ -7,17 +7,7 @@ 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,
},
key_ptr: rawptr, // address of Map_Entry_Header.key
}
Map_Find_Result :: struct {
@@ -27,9 +17,10 @@ Map_Find_Result :: struct {
}
Map_Entry_Header :: struct {
key: Map_Hash,
hash: Map_Hash,
next: int,
/*
key: Key_Value,
value: Value_Type,
*/
}
@@ -41,6 +32,9 @@ Map_Header :: struct {
entry_size: int,
entry_align: int,
key_offset: uintptr,
key_size: int,
value_offset: uintptr,
value_size: int,
}
@@ -48,57 +42,50 @@ Map_Header :: struct {
__get_map_header :: proc "contextless" (m: ^$T/map[$K]$V) -> Map_Header {
header := Map_Header{m = (^Raw_Map)(m)};
Entry :: struct {
key: Map_Hash,
hash: Map_Hash,
next: int,
key: K,
value: V,
};
header.is_key_string = intrinsics.type_is_string(K);
header.entry_size = int(size_of(Entry));
header.entry_align = int(align_of(Entry));
header.key_offset = uintptr(offset_of(Entry, key));
header.key_size = int(size_of(K));
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_key :: proc "contextless" (k: ^$K) -> Map_Hash {
key := k;
map_key: Map_Hash;
map_hash: Map_Hash;
T :: intrinsics.type_core_type(K);
map_hash.key_ptr = k;
when intrinsics.type_is_integer(T) {
map_key.hash = default_hash_ptr(&key, size_of(T));
sz :: 8*size_of(T);
when sz == 8 { map_key.key.val = u64(( ^u8)(&key)^); }
else when sz == 16 { map_key.key.val = u64((^u16)(&key)^); }
else when sz == 32 { map_key.key.val = u64((^u32)(&key)^); }
else when sz == 64 { map_key.key.val = u64((^u64)(&key)^); }
else { #panic("Unhandled integer size"); }
map_hash.hash = default_hash_ptr(key, size_of(T));
} else when intrinsics.type_is_rune(T) {
map_key.hash = default_hash_ptr(&key, size_of(T));
map_key.key.val = u64((^rune)(&key)^);
map_hash.hash = default_hash_ptr(key, size_of(T));
} else when intrinsics.type_is_pointer(T) {
map_key.hash = default_hash_ptr(&key, size_of(T));
map_key.key.val = u64(uintptr((^rawptr)(&key)^));
map_hash.hash = default_hash_ptr(key, size_of(T));
} else when intrinsics.type_is_float(T) {
map_key.hash = default_hash_ptr(&key, size_of(T));
sz :: 8*size_of(T);
when sz == 32 { map_key.key.val = u64((^u32)(&key)^); }
else when sz == 64 { map_key.key.val = u64((^u64)(&key)^); }
else { #panic("Unhandled float size"); }
map_hash.hash = default_hash_ptr(key, size_of(T));
} else when intrinsics.type_is_string(T) {
#assert(T == string);
str := (^string)(&key)^;
map_key.hash = default_hash_string(str);
map_key.key.str = str;
str := (^string)(key)^;
map_hash.hash = default_hash_string(str);
} else {
#panic("Unhandled map key type");
}
return map_key;
return map_hash;
}
_fnv64a :: proc "contextless" (data: []byte, seed: u64 = 0xcbf29ce484222325) -> u64 {
@@ -188,8 +175,8 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #c
entry_header := __dynamic_map_get_entry(header, i);
data := uintptr(entry_header);
fr := __dynamic_map_find(new_header, entry_header.key);
j := __dynamic_map_add_entry(new_header, entry_header.key, loc);
fr := __dynamic_map_find(new_header, entry_header.hash);
j := __dynamic_map_add_entry(new_header, entry_header.hash, loc);
if fr.entry_prev < 0 {
nm.hashes[fr.hash_index] = j;
} else {
@@ -199,8 +186,7 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #c
e := __dynamic_map_get_entry(new_header, j);
e.next = fr.entry_index;
ndata := uintptr(e);
mem_copy(rawptr(ndata+value_offset), rawptr(data+value_offset), value_size);
mem_copy(rawptr(uintptr(e)+value_offset), rawptr(data+value_offset), value_size);
if __dynamic_map_full(new_header) {
__dynamic_map_grow(new_header, loc);
@@ -211,8 +197,8 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int, loc := #c
header.m^ = nm;
}
__dynamic_map_get :: proc(h: Map_Header, key: Map_Hash) -> rawptr {
index := __dynamic_map_find(h, key).entry_index;
__dynamic_map_get :: proc(h: Map_Header, hash: Map_Hash) -> rawptr {
index := __dynamic_map_find(h, hash).entry_index;
if index >= 0 {
data := uintptr(__dynamic_map_get_entry(h, index));
return rawptr(data + h.value_offset);
@@ -220,7 +206,7 @@ __dynamic_map_get :: proc(h: Map_Header, key: Map_Hash) -> rawptr {
return nil;
}
__dynamic_map_set :: proc(h: Map_Header, key: Map_Hash, value: rawptr, loc := #caller_location) #no_bounds_check {
__dynamic_map_set :: proc(h: Map_Header, hash: Map_Hash, value: rawptr, loc := #caller_location) #no_bounds_check {
index: int;
assert(value != nil);
@@ -229,11 +215,11 @@ __dynamic_map_set :: proc(h: Map_Header, key: Map_Hash, value: rawptr, loc := #c
__dynamic_map_grow(h, loc);
}
fr := __dynamic_map_find(h, key);
fr := __dynamic_map_find(h, hash);
if fr.entry_index >= 0 {
index = fr.entry_index;
} else {
index = __dynamic_map_add_entry(h, key, loc);
index = __dynamic_map_add_entry(h, hash, loc);
if fr.entry_prev >= 0 {
entry := __dynamic_map_get_entry(h, fr.entry_prev);
entry.next = index;
@@ -243,9 +229,15 @@ __dynamic_map_set :: proc(h: Map_Header, key: Map_Hash, value: rawptr, loc := #c
}
{
e := __dynamic_map_get_entry(h, index);
e.key = key;
val := (^byte)(uintptr(e) + h.value_offset);
key := rawptr(uintptr(e) + h.key_offset);
mem_copy(key, hash.key_ptr, h.key_size);
val := rawptr(uintptr(e) + h.value_offset);
mem_copy(val, value, h.value_size);
e.hash.hash = hash.hash;
e.hash.key_ptr = key;
}
if __dynamic_map_full(h) {
@@ -267,24 +259,28 @@ __dynamic_map_full :: inline proc(using h: Map_Header) -> bool {
__dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Hash) -> bool {
if a.hash == b.hash {
if h.is_key_string {
return a.key.str == b.key.str;
} else {
return a.key.val == b.key.val;
if a.key_ptr == b.key_ptr {
return true;
}
return true;
if a.key_ptr == nil || b.key_ptr == nil {
return false;
}
if h.is_key_string {
return (^string)(a.key_ptr)^ == (^string)(b.key_ptr)^;
}
return memory_equal(a.key_ptr, b.key_ptr, h.key_size);
}
return false;
}
__dynamic_map_find :: proc(using h: Map_Header, key: Map_Hash) -> Map_Find_Result #no_bounds_check {
__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 {
fr.hash_index = int(key.hash % n);
fr.hash_index = int(hash.hash % n);
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.key, key) {
if __dynamic_map_hash_equal(h, entry.hash, hash) {
return fr;
}
fr.entry_prev = fr.entry_index;
@@ -294,19 +290,21 @@ __dynamic_map_find :: proc(using h: Map_Header, key: Map_Hash) -> Map_Find_Resul
return fr;
}
__dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Hash, loc := #caller_location) -> int {
__dynamic_map_add_entry :: proc(using h: Map_Header, hash: Map_Hash, loc := #caller_location) -> int {
prev := m.entries.len;
c := __dynamic_array_append_nothing(&m.entries, entry_size, entry_align, loc);
if c != prev {
end := __dynamic_map_get_entry(h, c-1);
end.key = key;
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.next = -1;
}
return prev;
}
__dynamic_map_delete_key :: proc(using h: Map_Header, key: Map_Hash) {
fr := __dynamic_map_find(h, key);
__dynamic_map_delete_key :: proc(using h: Map_Header, hash: Map_Hash) {
fr := __dynamic_map_find(h, hash);
if fr.entry_index >= 0 {
__dynamic_map_erase(h, fr);
}
@@ -332,7 +330,7 @@ __dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) #no_bounds
end := __dynamic_map_get_entry(h, m.entries.len-1);
mem_copy(old, end, entry_size);
if last := __dynamic_map_find(h, old.key); last.entry_prev >= 0 {
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 {
+8 -6
View File
@@ -2789,18 +2789,20 @@ void init_map_entry_type(Type *type) {
/*
struct {
key: runtime.Map_Key,
next: int,
value: Value,
hash: runtime.Map_Hash,
next: int,
key: Key,
value: Value,
}
*/
Ast *dummy_node = alloc_ast_node(nullptr, Ast_Invalid);
Scope *s = create_scope(builtin_pkg->scope);
auto fields = array_make<Entity *>(permanent_allocator(), 0, 4);
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, 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));
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("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("value")), type->Map.value, false, cast(i32)fields.count, EntityState_Resolved));
entry_type->Struct.fields = fields;
+20 -24
View File
@@ -3595,18 +3595,22 @@ irValue *ir_gen_map_header(irProcedure *proc, irValue *map_val_ptr, Type *map_ty
i64 entry_size = type_size_of (map_type->Map.entry_type);
i64 entry_align = type_align_of (map_type->Map.entry_type);
i64 value_offset = type_offset_of(map_type->Map.entry_type, 2);
i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
i64 key_size = type_size_of (map_type->Map.key);
i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
i64 value_size = type_size_of (map_type->Map.value);
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 2), ir_const_int(entry_size));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 3), ir_const_int(entry_align));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 4), ir_const_uintptr(value_offset));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 5), ir_const_int(value_size));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 4), ir_const_uintptr(key_offset));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 5), ir_const_int(key_size));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 6), ir_const_uintptr(value_offset));
ir_emit_store(proc, ir_emit_struct_ep(proc, h, 7), ir_const_int(value_size));
return ir_emit_load(proc, h);
}
irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) {
irValue *ir_gen_map_hash(irProcedure *proc, irValue *key, Type *key_type) {
Type *hash_type = t_u64;
irValue *v = ir_add_local_generated(proc, t_map_hash, true);
Type *t = base_type(ir_type(key));
@@ -3627,10 +3631,6 @@ irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) {
hashed_str = ir_emit_runtime_call(proc, "default_hash_string", args);
}
ir_emit_store(proc, ir_emit_struct_ep(proc, v, 0), hashed_str);
irValue *key_data = ir_emit_struct_ep(proc, v, 1);
key_data = ir_emit_conv(proc, key_data, alloc_type_pointer(key_type));
ir_emit_store(proc, key_data, str);
} else {
i64 sz = type_size_of(t);
GB_ASSERT(sz <= 8);
@@ -3640,16 +3640,17 @@ irValue *ir_gen_map_key(irProcedure *proc, irValue *key, Type *key_type) {
args[1] = ir_const_int(sz);
irValue *hash = ir_emit_runtime_call(proc, "default_hash_ptr", args);
irValue *hash_ptr = ir_emit_struct_ep(proc, v, 0);
irValue *key_data = ir_emit_struct_ep(proc, v, 1);
key_data = ir_emit_conv(proc, key_data, alloc_type_pointer(key_type));
ir_emit_store(proc, hash_ptr, hash);
ir_emit_store(proc, key_data, key);
}
}
irValue *key_ptr = ir_address_from_load_or_generate_local(proc, key);
key_ptr = ir_emit_conv(proc, key_ptr, t_rawptr);
irValue *key_data = ir_emit_struct_ep(proc, v, 1);
ir_emit_store(proc, key_data, key_ptr);
return ir_emit_load(proc, v);
}
@@ -3705,7 +3706,7 @@ irValue *ir_insert_dynamic_map_key_and_value(irProcedure *proc, irValue *addr, T
map_type = base_type(map_type);
irValue *h = ir_gen_map_header(proc, addr, map_type);
irValue *key = ir_gen_map_key(proc, map_key, map_type->Map.key);
irValue *key = ir_gen_map_hash(proc, map_key, map_type->Map.key);
irValue *v = ir_emit_conv(proc, map_value, map_type->Map.value);
irValue *ptr = ir_add_local_generated(proc, ir_type(v), false);
@@ -4062,7 +4063,7 @@ irValue *ir_addr_load(irProcedure *proc, irAddr const &addr) {
Type *map_type = base_type(addr.map_type);
irValue *v = ir_add_local_generated(proc, map_type->Map.lookup_result_type, true);
irValue *h = ir_gen_map_header(proc, addr.addr, map_type);
irValue *key = ir_gen_map_key(proc, addr.map_key, map_type->Map.key);
irValue *key = ir_gen_map_hash(proc, addr.map_key, map_type->Map.key);
auto args = array_make<irValue *>(ir_allocator(), 2);
args[0] = h;
@@ -4230,7 +4231,7 @@ irValue *ir_addr_get_ptr(irProcedure *proc, irAddr const &addr, bool allow_refer
if (allow_reference) {
Type *map_type = base_type(addr.map_type);
irValue *h = ir_gen_map_header(proc, addr.addr, map_type);
irValue *key = ir_gen_map_key(proc, addr.map_key, map_type->Map.key);
irValue *key = ir_gen_map_hash(proc, addr.map_key, map_type->Map.key);
auto args = array_make<irValue *>(ir_allocator(), 2);
args[0] = h;
@@ -8320,7 +8321,7 @@ irValue *ir_build_expr_internal(irProcedure *proc, Ast *expr) {
irValue *addr = ir_address_from_load_or_generate_local(proc, right);
irValue *h = ir_gen_map_header(proc, addr, rt);
irValue *key = ir_gen_map_key(proc, left, rt->Map.key);
irValue *key = ir_gen_map_hash(proc, left, rt->Map.key);
auto args = array_make<irValue *>(ir_allocator(), 2);
args[0] = h;
@@ -9954,13 +9955,8 @@ void ir_build_range_indexed(irProcedure *proc, irValue *expr, Type *val_type, ir
elem = ir_emit_load(proc, elem);
irValue *entry = ir_emit_ptr_offset(proc, elem, idx);
val = ir_emit_load(proc, ir_emit_struct_ep(proc, entry, 2));
irValue *key_raw = ir_emit_struct_ep(proc, entry, 0);
key_raw = ir_emit_struct_ep(proc, key_raw, 1);
irValue *key = ir_emit_conv(proc, key_raw, alloc_type_pointer(expr_type->Map.key));
idx = ir_emit_load(proc, key);
idx = ir_emit_load(proc, ir_emit_struct_ep(proc, entry, 2));
val = ir_emit_load(proc, ir_emit_struct_ep(proc, entry, 3));
break;
}
+17 -22
View File
@@ -3361,13 +3361,8 @@ void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_type, lbValu
elem = lb_emit_load(p, elem);
lbValue entry = lb_emit_ptr_offset(p, elem, idx);
val = lb_emit_load(p, lb_emit_struct_ep(p, entry, 2));
lbValue key_raw = lb_emit_struct_ep(p, entry, 0);
key_raw = lb_emit_struct_ep(p, key_raw, 1);
lbValue key = lb_emit_conv(p, key_raw, alloc_type_pointer(expr_type->Map.key));
idx = lb_emit_load(p, key);
idx = lb_emit_load(p, lb_emit_struct_ep(p, entry, 2));
val = lb_emit_load(p, lb_emit_struct_ep(p, entry, 3));
break;
}
@@ -10246,13 +10241,19 @@ lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type) {
i64 entry_size = type_size_of (map_type->Map.entry_type);
i64 entry_align = type_align_of (map_type->Map.entry_type);
i64 value_offset = type_offset_of(map_type->Map.entry_type, 2);
i64 key_offset = type_offset_of(map_type->Map.entry_type, 2);
i64 key_size = type_size_of (map_type->Map.key);
i64 value_offset = type_offset_of(map_type->Map.entry_type, 3);
i64 value_size = type_size_of (map_type->Map.value);
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 2), lb_const_int(p->module, t_int, entry_size));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 3), lb_const_int(p->module, t_int, entry_align));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 4), lb_const_int(p->module, t_uintptr, value_offset));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 5), lb_const_int(p->module, t_int, value_size));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 4), lb_const_int(p->module, t_uintptr, key_offset));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 5), lb_const_int(p->module, t_int, key_size));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 6), lb_const_int(p->module, t_uintptr, value_offset));
lb_emit_store(p, lb_emit_struct_ep(p, h.addr, 7), lb_const_int(p->module, t_int, value_size));
return lb_addr_load(p, h);
}
@@ -10278,10 +10279,6 @@ lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) {
hashed_str = lb_emit_runtime_call(p, "default_hash_string", args);
}
lb_emit_store(p, lb_emit_struct_ep(p, vp, 0), hashed_str);
lbValue key_data = lb_emit_struct_ep(p, vp, 1);
key_data = lb_emit_conv(p, key_data, alloc_type_pointer(key_type));
lb_emit_store(p, key_data, str);
} else {
i64 sz = type_size_of(t);
GB_ASSERT(sz <= 8);
@@ -10291,16 +10288,14 @@ lbValue lb_gen_map_key(lbProcedure *p, lbValue key, Type *key_type) {
args[1] = lb_const_int(p->module, t_int, sz);
lbValue hash = lb_emit_runtime_call(p, "default_hash_ptr", args);
lbValue hash_ptr = lb_emit_struct_ep(p, vp, 0);
lbValue key_data = lb_emit_struct_ep(p, vp, 1);
key_data = lb_emit_conv(p, key_data, alloc_type_pointer(key_type));
lb_emit_store(p, hash_ptr, hash);
lb_emit_store(p, key_data, key);
lb_emit_store(p, lb_emit_struct_ep(p, vp, 0), hash);
}
}
lbValue key_ptr = lb_address_from_load_or_generate_local(p, key);
key_ptr = lb_emit_conv(p, key_ptr, t_rawptr);
lb_emit_store(p, lb_emit_struct_ep(p, vp, 1), key_ptr);
return lb_addr_load(p, v);
}
@@ -12202,7 +12197,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da
lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count);
vals[9] = soa_kind.value;
vals[1] = soa_type.value;
vals[10] = soa_type.value;
vals[11] = soa_len.value;
}
}