aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-11-29 18:17:21 +0000
committergingerBill <bill@gingerbill.org>2020-11-29 18:17:21 +0000
commitf06f33872ca22ca9afd1787b130ee3fb5fb2d723 (patch)
tree230c4f3acdd3a4ce31e3fff7e5c1abb397a77ad8
parent9e1341631246e752b7be703a6ef3e4398205b726 (diff)
Make 16 simple hasher cases for small types
-rw-r--r--core/runtime/dynamic_map_internal.odin16
-rw-r--r--src/check_type.cpp42
-rw-r--r--src/ir.cpp42
-rw-r--r--src/llvm_backend.cpp14
4 files changed, 55 insertions, 59 deletions
diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin
index 7d5ff017b..667a56575 100644
--- a/core/runtime/dynamic_map_internal.odin
+++ b/core/runtime/dynamic_map_internal.odin
@@ -77,7 +77,7 @@ default_hash_ptr :: inline proc "contextless" (data: rawptr, size: int) -> uintp
}
@(private)
-_default_hasher_const :: inline proc "contextless" (data: rawptr, seed: uintptr, $N: uint) -> uintptr {
+_default_hasher_const :: inline proc "contextless" (data: rawptr, seed: uintptr, $N: uint) -> uintptr where N <= 16 {
h := u64(seed) + 0xcbf29ce484222325;
p := uintptr(data);
inline for _ in 0..<N {
@@ -99,11 +99,25 @@ default_hasher_n :: inline proc "contextless" (data: rawptr, seed: uintptr, N: i
return uintptr(h);
}
+// NOTE(bill): There are loads of predefined ones to improve optimizations for small types
+
default_hasher1 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 1); }
default_hasher2 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 2); }
+default_hasher3 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 3); }
default_hasher4 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 4); }
+default_hasher5 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 5); }
+default_hasher6 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 6); }
+default_hasher7 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 7); }
default_hasher8 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 8); }
+default_hasher9 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 9); }
+default_hasher10 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 10); }
+default_hasher11 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 11); }
+default_hasher12 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 12); }
+default_hasher13 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 13); }
+default_hasher14 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 14); }
+default_hasher15 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 15); }
default_hasher16 :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr { return inline _default_hasher_const(data, seed, 16); }
+
default_hasher_string :: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr {
h := u64(seed) + 0xcbf29ce484222325;
str := (^[]byte)(data)^;
diff --git a/src/check_type.cpp b/src/check_type.cpp
index 9c8308757..ab69c89bc 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -2860,37 +2860,31 @@ void add_map_key_type_dependencies(CheckerContext *ctx, Type *key) {
return;
}
+ if (is_type_simple_compare(key)) {
+ i64 sz = type_size_of(key);
+ if (1 <= sz && sz <= 16) {
+ char buf[20] = {};
+ gb_snprintf(buf, 20, "default_hasher%d", cast(i32)sz);
+ add_package_dependency(ctx, "runtime", buf);
+ return;
+ } else {
+ add_package_dependency(ctx, "runtime", "default_hasher_n");
+ return;
+ }
+ }
+
if (key->kind == Type_Struct) {
add_package_dependency(ctx, "runtime", "default_hasher_n");
- if (!is_type_simple_compare(key)) {
- for_array(i, key->Struct.fields) {
- Entity *field = key->Struct.fields[i];
- add_map_key_type_dependencies(ctx, field->type);
- }
+ for_array(i, key->Struct.fields) {
+ Entity *field = key->Struct.fields[i];
+ add_map_key_type_dependencies(ctx, field->type);
}
} else if (key->kind == Type_EnumeratedArray) {
add_package_dependency(ctx, "runtime", "default_hasher_n");
- if (!is_type_simple_compare(key->EnumeratedArray.elem)) {
- add_map_key_type_dependencies(ctx, key->EnumeratedArray.elem);
- }
+ add_map_key_type_dependencies(ctx, key->EnumeratedArray.elem);
} else if (key->kind == Type_Array) {
add_package_dependency(ctx, "runtime", "default_hasher_n");
- if (!is_type_simple_compare(key->Array.elem)) {
- add_map_key_type_dependencies(ctx, key->Array.elem);
- }
- } else {
- if (!is_type_simple_compare(key)) {
- GB_PANIC("HERE");
- }
-
- i64 sz = type_size_of(key);
- switch (sz) {
- case 1: add_package_dependency(ctx, "runtime", "default_hasher1"); break;
- case 2: add_package_dependency(ctx, "runtime", "default_hasher2"); break;
- case 4: add_package_dependency(ctx, "runtime", "default_hasher4"); break;
- case 8: add_package_dependency(ctx, "runtime", "default_hasher8"); break;
- case 16: add_package_dependency(ctx, "runtime", "default_hasher16"); break;
- }
+ add_map_key_type_dependencies(ctx, key->Array.elem);
}
}
}
diff --git a/src/ir.cpp b/src/ir.cpp
index 06d88ae5b..0d9292a50 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -4969,15 +4969,10 @@ irValue *ir_simple_compare_hash(irProcedure *p, Type *type, irValue *data, irVal
GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
i64 sz = type_size_of(type);
- char const *name = nullptr;
- switch (sz) {
- case 1: name = "default_hasher1"; break;
- case 2: name = "default_hasher2"; break;
- case 4: name = "default_hasher4"; break;
- case 8: name = "default_hasher8"; break;
- case 16: name = "default_hasher16"; break;
- }
- if (name != nullptr) {
+ if (1 <= sz && sz <= 16) {
+ char name[20] = {};
+ gb_snprintf(name, 20, "default_hasher%d", cast(i32)sz);
+
auto args = array_make<irValue *>(permanent_allocator(), 2);
args[0] = data;
args[1] = seed;
@@ -5038,7 +5033,19 @@ irValue *ir_get_hasher_proc_for_type(irModule *m, Type *type) {
return p;
}
- if (type->kind == Type_Struct) {
+ if (is_type_cstring(type)) {
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ irValue *res = ir_emit_runtime_call(proc, "default_hasher_cstring", args);
+ ir_emit(proc, ir_instr_return(proc, res));
+ } else if (is_type_string(type)) {
+ auto args = array_make<irValue *>(permanent_allocator(), 2);
+ args[0] = data;
+ args[1] = seed;
+ irValue *res = ir_emit_runtime_call(proc, "default_hasher_string", args);
+ ir_emit(proc, ir_instr_return(proc, res));
+ } else if (type->kind == Type_Struct) {
type_set_offsets(type);
data = ir_emit_conv(proc, data, t_u8_ptr);
@@ -5096,25 +5103,10 @@ irValue *ir_get_hasher_proc_for_type(irModule *m, Type *type) {
irValue *res = ir_emit_load(proc, pres);
ir_emit(proc, ir_instr_return(proc, res));
- } else if (is_type_cstring(type)) {
- auto args = array_make<irValue *>(permanent_allocator(), 2);
- args[0] = data;
- args[1] = seed;
- irValue *res = ir_emit_runtime_call(proc, "default_hasher_cstring", args);
- ir_emit(proc, ir_instr_return(proc, res));
- } else if (is_type_string(type)) {
- auto args = array_make<irValue *>(permanent_allocator(), 2);
- args[0] = data;
- args[1] = seed;
- irValue *res = ir_emit_runtime_call(proc, "default_hasher_string", args);
- ir_emit(proc, ir_instr_return(proc, res));
} else {
GB_PANIC("Unhandled type for hasher: %s", type_to_string(type));
}
-
- ir_end_procedure_body(proc);
-
return p;
}
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index b37744b20..001a44895 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -9253,15 +9253,11 @@ lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue
GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type));
i64 sz = type_size_of(type);
- char const *name = nullptr;
- switch (sz) {
- case 1: name = "default_hasher1"; break;
- case 2: name = "default_hasher2"; break;
- case 4: name = "default_hasher4"; break;
- case 8: name = "default_hasher8"; break;
- case 16: name = "default_hasher16"; break;
- }
- if (name != nullptr) {
+
+ if (1 <= sz && sz <= 16) {
+ char name[20] = {};
+ gb_snprintf(name, 20, "default_hasher%d", cast(i32)sz);
+
auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = data;
args[1] = seed;