mirror of
https://github.com/odin-lang/Odin.git
synced 2026-01-01 10:52:19 +00:00
Reduce unnecessary map gets
This commit is contained in:
@@ -296,7 +296,8 @@ clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
|
||||
@builtin
|
||||
reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) {
|
||||
if m != nil {
|
||||
__dynamic_map_reserve(__get_map_header(m), uint(capacity), loc)
|
||||
h := __get_map_header_table(T)
|
||||
__dynamic_map_reserve(m, h, uint(capacity), loc)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,17 +57,20 @@ Map_Header :: struct {
|
||||
}
|
||||
|
||||
// USED INTERNALLY BY THE COMPILER
|
||||
__dynamic_map_get :: proc "contextless" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr) -> rawptr {
|
||||
index := __dynamic_map_find(h, key_hash, key_ptr).entry_index
|
||||
if index != MAP_SENTINEL {
|
||||
data := uintptr(__dynamic_map_get_entry(h, index))
|
||||
return rawptr(data + h.value_offset)
|
||||
__dynamic_map_get :: proc "contextless" (m: rawptr, table: Map_Header_Table, key_hash: uintptr, key_ptr: rawptr) -> rawptr {
|
||||
if m != nil {
|
||||
h := Map_Header{(^Raw_Map)(m), table}
|
||||
index := __dynamic_map_find(h, key_hash, key_ptr).entry_index
|
||||
if index != MAP_SENTINEL {
|
||||
data := uintptr(__dynamic_map_get_entry(h, index))
|
||||
return rawptr(data + h.value_offset)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// USED INTERNALLY BY THE COMPILER
|
||||
__dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check {
|
||||
__dynamic_map_set :: proc "odin" (m: rawptr, table: Map_Header_Table, key_hash: uintptr, key_ptr: rawptr, value: rawptr, loc := #caller_location) -> ^Map_Entry_Header #no_bounds_check {
|
||||
add_entry :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: rawptr, loc := #caller_location) -> Map_Index {
|
||||
prev := Map_Index(h.m.entries.len)
|
||||
c := Map_Index(__dynamic_array_append_nothing(&h.m.entries, h.entry_size, h.entry_align, loc))
|
||||
@@ -79,11 +82,14 @@ __dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: raw
|
||||
}
|
||||
return prev
|
||||
}
|
||||
assert(condition = m != nil)
|
||||
|
||||
h := Map_Header{(^Raw_Map)(m), table}
|
||||
|
||||
index := MAP_SENTINEL
|
||||
|
||||
if len(h.m.hashes) == 0 {
|
||||
__dynamic_map_reserve(h, INITIAL_MAP_CAP, loc)
|
||||
__dynamic_map_reserve(m, table, INITIAL_MAP_CAP, loc)
|
||||
__dynamic_map_grow(h, loc)
|
||||
}
|
||||
|
||||
@@ -119,7 +125,11 @@ __dynamic_map_set :: proc "odin" (h: Map_Header, key_hash: uintptr, key_ptr: raw
|
||||
}
|
||||
|
||||
// USED INTERNALLY BY THE COMPILER
|
||||
__dynamic_map_reserve :: proc "odin" (h: Map_Header, cap: uint, loc := #caller_location) {
|
||||
__dynamic_map_reserve :: proc "odin" (m: rawptr, table: Map_Header_Table, cap: uint, loc := #caller_location) {
|
||||
assert(condition = m != nil)
|
||||
|
||||
h := Map_Header{(^Raw_Map)(m), table}
|
||||
|
||||
c := context
|
||||
if h.m.entries.allocator.procedure != nil {
|
||||
c.allocator = h.m.entries.allocator
|
||||
@@ -352,7 +362,7 @@ ceil_to_pow2 :: proc "contextless" (n: uint) -> uint {
|
||||
__dynamic_map_grow :: proc "odin" (h: Map_Header, loc := #caller_location) {
|
||||
new_count := max(uint(h.m.entries.cap) * 2, INITIAL_MAP_CAP)
|
||||
// Rehash through Reserve
|
||||
__dynamic_map_reserve(h, new_count, loc)
|
||||
__dynamic_map_reserve(h.m, h.table, new_count, loc)
|
||||
}
|
||||
|
||||
__dynamic_map_full :: #force_inline proc "contextless" (h: Map_Header) -> bool {
|
||||
|
||||
@@ -500,10 +500,15 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A
|
||||
return value;
|
||||
}
|
||||
|
||||
LLVMValueRef lb_gen_map_header_table_internal(lbModule *m, Type *map_type) {
|
||||
lbAddr lb_gen_map_header_table_internal(lbModule *m, Type *map_type) {
|
||||
map_type = base_type(map_type);
|
||||
GB_ASSERT(map_type->kind == Type_Map);
|
||||
|
||||
lbAddr *found = map_get(&m->map_header_table_map, map_type);
|
||||
if (found) {
|
||||
return *found;
|
||||
}
|
||||
|
||||
GB_ASSERT(map_type->Map.entry_type->kind == Type_Struct);
|
||||
i64 entry_size = type_size_of (map_type->Map.entry_type);
|
||||
i64 entry_align = type_align_of (map_type->Map.entry_type);
|
||||
@@ -531,7 +536,18 @@ LLVMValueRef lb_gen_map_header_table_internal(lbModule *m, Type *map_type) {
|
||||
const_values[5] = lb_const_int(m, t_uintptr, value_offset).value;
|
||||
const_values[6] = lb_const_int(m, t_int, value_size) .value;
|
||||
|
||||
return llvm_const_named_struct(m, t_map_header_table, const_values, gb_count_of(const_values));
|
||||
LLVMValueRef llvm_res = llvm_const_named_struct(m, t_map_header_table, const_values, gb_count_of(const_values));
|
||||
lbValue res = {llvm_res, t_map_header_table};
|
||||
|
||||
lbAddr addr = lb_add_global_generated(m, t_map_header_table, res, nullptr);
|
||||
LLVMValueRef global_data = addr.addr.value;
|
||||
|
||||
LLVMSetLinkage(global_data, LLVMPrivateLinkage);
|
||||
LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr);
|
||||
LLVMSetGlobalConstant(global_data, true);
|
||||
|
||||
map_set(&m->map_header_table_map, map_type, addr);
|
||||
return addr;
|
||||
}
|
||||
|
||||
|
||||
@@ -550,7 +566,7 @@ lbAddr lb_gen_map_header_internal(lbProcedure *p, lbValue map_val_ptr, Type *map
|
||||
Type *raw_map_ptr_type = map_header_base->Struct.fields[0]->type;
|
||||
LLVMValueRef const_values[2] = {};
|
||||
const_values[0] = LLVMConstNull(lb_type(p->module, raw_map_ptr_type));
|
||||
const_values[1] = lb_gen_map_header_table_internal(p->module, map_type);
|
||||
const_values[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, map_type)).value;
|
||||
|
||||
LLVMValueRef const_value = llvm_const_named_struct(p->module, t_map_header, const_values, gb_count_of(const_values));
|
||||
LLVMBuildStore(p->builder, const_value, h.addr.value);
|
||||
@@ -653,12 +669,30 @@ lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue
|
||||
return hashed_key;
|
||||
}
|
||||
|
||||
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type,
|
||||
lbValue map_key, lbValue map_value, Ast *node) {
|
||||
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) {
|
||||
if (p->name == "main.map_type") {
|
||||
gb_printf_err("HERE!\n");
|
||||
}
|
||||
|
||||
Type *map_type = base_type(type_deref(map_ptr.type));
|
||||
|
||||
lbValue key_ptr = {};
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 4);
|
||||
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
|
||||
args[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, map_type));
|
||||
args[2] = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr);
|
||||
args[3] = key_ptr;
|
||||
|
||||
lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args);
|
||||
|
||||
return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value));
|
||||
}
|
||||
|
||||
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr, Type *map_type,
|
||||
lbValue const &map_key, lbValue const &map_value, Ast *node) {
|
||||
map_type = base_type(map_type);
|
||||
GB_ASSERT(map_type->kind == Type_Map);
|
||||
|
||||
lbValue h = lb_gen_map_header(p, addr.addr, map_type);
|
||||
lbValue key_ptr = {};
|
||||
lbValue key_hash = lb_gen_map_key_hash(p, map_key, map_type->Map.key, &key_ptr);
|
||||
lbValue v = lb_emit_conv(p, map_value, map_type->Map.value);
|
||||
@@ -666,15 +700,32 @@ void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_
|
||||
lbAddr value_addr = lb_add_local_generated(p, v.type, false);
|
||||
lb_addr_store(p, value_addr, v);
|
||||
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 5);
|
||||
args[0] = h;
|
||||
args[1] = key_hash;
|
||||
args[2] = key_ptr;
|
||||
args[3] = lb_emit_conv(p, value_addr.addr, t_rawptr);
|
||||
args[4] = lb_emit_source_code_location(p, node);
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 6);
|
||||
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
|
||||
args[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, map_type));
|
||||
args[2] = key_hash;
|
||||
args[3] = key_ptr;
|
||||
args[4] = lb_emit_conv(p, value_addr.addr, t_rawptr);
|
||||
args[5] = lb_emit_source_code_location(p, node);
|
||||
lb_emit_runtime_call(p, "__dynamic_map_set", args);
|
||||
}
|
||||
|
||||
void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) {
|
||||
GB_ASSERT(!build_context.no_dynamic_literals);
|
||||
|
||||
String proc_name = {};
|
||||
if (p->entity) {
|
||||
proc_name = p->entity->token.string;
|
||||
}
|
||||
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 4);
|
||||
args[0] = lb_emit_conv(p, map_ptr, t_rawptr);
|
||||
args[1] = lb_addr_load(p, lb_gen_map_header_table_internal(p->module, type_deref(map_ptr.type)));
|
||||
args[2] = lb_const_int(p->module, t_int, capacity);
|
||||
args[3] = lb_emit_source_code_location(p, proc_name, pos);
|
||||
lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
|
||||
}
|
||||
|
||||
|
||||
struct lbGlobalVariable {
|
||||
lbValue var;
|
||||
|
||||
@@ -159,6 +159,8 @@ struct lbModule {
|
||||
|
||||
StringMap<lbAddr> objc_classes;
|
||||
StringMap<lbAddr> objc_selectors;
|
||||
|
||||
PtrMap<Type *, lbAddr> map_header_table_map;
|
||||
};
|
||||
|
||||
struct lbGenerator {
|
||||
@@ -446,7 +448,10 @@ lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool
|
||||
lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id);
|
||||
lbValue lb_gen_map_header(lbProcedure *p, lbValue map_val_ptr, Type *map_type);
|
||||
lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_);
|
||||
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbAddr addr, Type *map_type, lbValue map_key, lbValue map_value, Ast *node);
|
||||
|
||||
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key);
|
||||
void lb_insert_dynamic_map_key_and_value(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node);
|
||||
void lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos);
|
||||
|
||||
lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e);
|
||||
lbValue lb_find_value_from_entity(lbModule *m, Entity *e);
|
||||
|
||||
@@ -3670,16 +3670,14 @@ lbAddr lb_build_addr_index_expr(lbProcedure *p, Ast *expr) {
|
||||
|
||||
if (is_type_map(t)) {
|
||||
lbAddr map_addr = lb_build_addr(p, ie->expr);
|
||||
lbValue map_val = lb_addr_load(p, map_addr);
|
||||
if (deref) {
|
||||
map_val = lb_emit_load(p, map_val);
|
||||
}
|
||||
|
||||
lbValue key = lb_build_expr(p, ie->index);
|
||||
key = lb_emit_conv(p, key, t->Map.key);
|
||||
|
||||
Type *result_type = type_of_expr(expr);
|
||||
lbValue map_ptr = lb_address_from_load_or_generate_local(p, map_val);
|
||||
lbValue map_ptr = lb_addr_get_ptr(p, map_addr);
|
||||
if (is_type_pointer(type_deref(map_ptr.type))) {
|
||||
map_ptr = lb_emit_load(p, map_ptr);
|
||||
}
|
||||
return lb_addr_map(map_ptr, key, t, result_type);
|
||||
}
|
||||
|
||||
@@ -4130,20 +4128,16 @@ lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
|
||||
break;
|
||||
}
|
||||
GB_ASSERT(!build_context.no_dynamic_literals);
|
||||
{
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 3);
|
||||
args[0] = lb_gen_map_header(p, v.addr, type);
|
||||
args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count);
|
||||
args[2] = lb_emit_source_code_location(p, proc_name, pos);
|
||||
lb_emit_runtime_call(p, "__dynamic_map_reserve", args);
|
||||
}
|
||||
|
||||
lb_dynamic_map_reserve(p, v.addr, 2*cl->elems.count, pos);
|
||||
|
||||
for_array(field_index, cl->elems) {
|
||||
Ast *elem = cl->elems[field_index];
|
||||
ast_node(fv, FieldValue, elem);
|
||||
|
||||
lbValue key = lb_build_expr(p, fv->field);
|
||||
lbValue value = lb_build_expr(p, fv->value);
|
||||
lb_insert_dynamic_map_key_and_value(p, v, type, key, value, elem);
|
||||
lb_insert_dynamic_map_key_and_value(p, v.addr, type, key, value, elem);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -74,6 +74,9 @@ void lb_init_module(lbModule *m, Checker *c) {
|
||||
|
||||
string_map_init(&m->objc_classes, a);
|
||||
string_map_init(&m->objc_selectors, a);
|
||||
|
||||
map_init(&m->map_header_table_map, a, 0);
|
||||
|
||||
}
|
||||
|
||||
bool lb_init_generator(lbGenerator *gen, Checker *c) {
|
||||
@@ -383,21 +386,6 @@ Type *lb_addr_type(lbAddr const &addr) {
|
||||
return type_deref(addr.addr.type);
|
||||
}
|
||||
|
||||
lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) {
|
||||
Type *map_type = base_type(type_deref(map_ptr.type));
|
||||
lbValue h = lb_gen_map_header(p, map_ptr, map_type);
|
||||
|
||||
lbValue key_ptr = {};
|
||||
auto args = array_make<lbValue>(permanent_allocator(), 3);
|
||||
args[0] = h;
|
||||
args[1] = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr);
|
||||
args[2] = key_ptr;
|
||||
|
||||
lbValue ptr = lb_emit_runtime_call(p, "__dynamic_map_get", args);
|
||||
|
||||
return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value));
|
||||
}
|
||||
|
||||
lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) {
|
||||
if (addr.addr.value == nullptr) {
|
||||
GB_PANIC("Illegal addr -> nullptr");
|
||||
@@ -715,7 +703,7 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
|
||||
|
||||
return;
|
||||
} else if (addr.kind == lbAddr_Map) {
|
||||
lb_insert_dynamic_map_key_and_value(p, addr, addr.map.type, addr.map.key, value, p->curr_stmt);
|
||||
lb_insert_dynamic_map_key_and_value(p, addr.addr, addr.map.type, addr.map.key, value, p->curr_stmt);
|
||||
return;
|
||||
} else if (addr.kind == lbAddr_Context) {
|
||||
lbAddr old_addr = lb_find_or_generate_context_ptr(p);
|
||||
|
||||
@@ -889,7 +889,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> const &args,
|
||||
GB_ASSERT(param_count-1 <= args.count);
|
||||
param_count -= 1;
|
||||
} else {
|
||||
GB_ASSERT_MSG(param_count == args.count, "%td == %td", param_count, args.count);
|
||||
GB_ASSERT_MSG(param_count == args.count, "%td == %td (%s)", param_count, args.count, LLVMPrintValueToString(value.value));
|
||||
}
|
||||
|
||||
lbValue result = {};
|
||||
|
||||
Reference in New Issue
Block a user