Make 16 simple hasher cases for small types

This commit is contained in:
gingerBill
2020-11-29 18:17:21 +00:00
parent 9e13416312
commit f06f33872c
4 changed files with 55 additions and 59 deletions

View File

@@ -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)^;

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -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;