From 541beb615b745b45f2cc82813014a3e04f1a3231 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 5 Nov 2021 17:13:07 +0000 Subject: [PATCH] Move more things to `PtrMap` --- src/check_stmt.cpp | 9 ++++----- src/common.cpp | 8 ++++---- src/exact_value.cpp | 33 ++++++++++++++------------------- src/ptr_map.cpp | 2 +- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 103ffa071..24ad0eec1 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -699,7 +699,7 @@ struct TypeAndToken { }; -void add_constant_switch_case(CheckerContext *ctx, Map *seen, Operand operand, bool use_expr = true) { +void add_constant_switch_case(CheckerContext *ctx, PtrMap *seen, Operand operand, bool use_expr = true) { if (operand.mode != Addressing_Constant) { return; } @@ -707,7 +707,7 @@ void add_constant_switch_case(CheckerContext *ctx, Map *seen, Oper return; } - HashKey key = hash_exact_value(operand.value); + uintptr key = hash_exact_value(operand.value); TypeAndToken *found = map_get(seen, key); if (found != nullptr) { isize count = multi_map_count(seen, key); @@ -964,7 +964,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { } } - Map seen = {}; // NOTE(bill): Multimap, Key: ExactValue + PtrMap seen = {}; // NOTE(bill): Multimap, Key: ExactValue map_init(&seen, heap_allocator()); defer (map_destroy(&seen)); @@ -1133,8 +1133,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { continue; } ExactValue v = f->Constant.value; - HashKey key = hash_exact_value(v); - auto found = map_get(&seen, key); + auto found = map_get(&seen, hash_exact_value(v)); if (!found) { array_add(&unhandled, f); } diff --git a/src/common.cpp b/src/common.cpp index 5d7c87b6d..f7a0653ac 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -290,13 +290,13 @@ struct StringIntern { char str[1]; }; -Map string_intern_map = {}; // Key: u64 +PtrMap string_intern_map = {}; // Key: u64 gb_global Arena string_intern_arena = {}; char const *string_intern(char const *text, isize len) { u64 hash = gb_fnv64a(text, len); - u64 key = hash ? hash : 1; - StringIntern **found = map_get(&string_intern_map, hash_integer(key)); + uintptr key = cast(uintptr)(hash ? hash : 1); + StringIntern **found = map_get(&string_intern_map, key); if (found) { for (StringIntern *it = *found; it != nullptr; it = it->next) { if (it->len == len && gb_strncmp(it->str, (char *)text, len) == 0) { @@ -310,7 +310,7 @@ char const *string_intern(char const *text, isize len) { new_intern->next = found ? *found : nullptr; gb_memmove(new_intern->str, text, len); new_intern->str[len] = 0; - map_set(&string_intern_map, hash_integer(key), new_intern); + map_set(&string_intern_map, key, new_intern); return new_intern->str; } diff --git a/src/exact_value.cpp b/src/exact_value.cpp index 363c6d863..fd90278e5 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -63,44 +63,39 @@ struct ExactValue { gb_global ExactValue const empty_exact_value = {}; -HashKey hash_exact_value(ExactValue v) { +uintptr hash_exact_value(ExactValue v) { mutex_lock(&hash_exact_value_mutex); defer (mutex_unlock(&hash_exact_value_mutex)); - HashKey empty = {}; switch (v.kind) { case ExactValue_Invalid: - return empty; + return 0; case ExactValue_Bool: - return hash_integer(u64(v.value_bool)); + return gb_fnv32a(&v.value_bool, gb_size_of(v.value_bool)); case ExactValue_String: - { - char const *str = string_intern(v.value_string); - return hash_pointer(str); - } + return ptr_map_hash_key(string_intern(v.value_string)); case ExactValue_Integer: { - HashKey key = hashing_proc(v.value_integer.dp, gb_size_of(*v.value_integer.dp) * v.value_integer.used); + u32 key = gb_fnv32a(v.value_integer.dp, gb_size_of(*v.value_integer.dp) * v.value_integer.used); u8 last = (u8)v.value_integer.sign; - key.key = (key.key ^ last) * 0x100000001b3ll; - return key; + return (key ^ last) * 0x01000193; } case ExactValue_Float: - return hash_f64(v.value_float); + return gb_fnv32a(&v.value_float, gb_size_of(v.value_float)); case ExactValue_Pointer: - return hash_integer(v.value_pointer); + return ptr_map_hash_key(v.value_pointer); case ExactValue_Complex: - return hashing_proc(v.value_complex, gb_size_of(Complex128)); + return gb_fnv32a(v.value_complex, gb_size_of(Complex128)); case ExactValue_Quaternion: - return hashing_proc(v.value_quaternion, gb_size_of(Quaternion256)); + return gb_fnv32a(v.value_quaternion, gb_size_of(Quaternion256)); case ExactValue_Compound: - return hash_pointer(v.value_compound); + return ptr_map_hash_key(v.value_compound); case ExactValue_Procedure: - return hash_pointer(v.value_procedure); + return ptr_map_hash_key(v.value_procedure); case ExactValue_Typeid: - return hash_pointer(v.value_typeid); + return ptr_map_hash_key(v.value_typeid); } - return hashing_proc(&v, gb_size_of(ExactValue)); + return gb_fnv32a(&v, gb_size_of(ExactValue)); } diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 2387a2a20..0a61d300f 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -253,7 +253,7 @@ template PtrMapEntry *multi_map_find_next(PtrMap *h, PtrMapEntry *e) { isize i = e->next; while (i != MAP_SENTINEL) { - if (hash_key_equal(h->entries.data[i].key, e->key)) { + if (h->entries.data[i].key == e->key) { return &h->entries.data[i]; } i = h->entries.data[i].next;