From 69b075782bac981ceeea5eea8f544e346f0fe6b5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 2 Jan 2023 22:40:28 +0000 Subject: Use a package local mutex for `add_type_and_value` --- src/check_type.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/check_type.cpp') diff --git a/src/check_type.cpp b/src/check_type.cpp index 4634e1fbe..05fdbf4d3 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -3045,7 +3045,7 @@ gb_internal Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) #endif if (is_type_typed(type)) { - add_type_and_value(ctx->info, e, Addressing_Type, type, empty_exact_value); + add_type_and_value(ctx, e, Addressing_Type, type, empty_exact_value); } else { gbString name = type_to_string(type); error(e, "Invalid type definition of %s", name); -- cgit v1.2.3 From faa735d0c745ddc0b550e2a54f10588c873841b7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 4 Jan 2023 15:15:12 +0000 Subject: Localize gen_types mutexes --- src/check_type.cpp | 119 ++++++++++++++++++++++++++++------------------------- src/checker.hpp | 9 +++- src/path.cpp | 12 +++--- 3 files changed, 75 insertions(+), 65 deletions(-) (limited to 'src/check_type.cpp') diff --git a/src/check_type.cpp b/src/check_type.cpp index 05fdbf4d3..fd4e965d4 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -257,63 +257,67 @@ gb_internal bool check_custom_align(CheckerContext *ctx, Ast *node, i64 *align_) gb_internal Entity *find_polymorphic_record_entity(CheckerContext *ctx, Type *original_type, isize param_count, Array const &ordered_operands, bool *failure) { - mutex_lock(&ctx->info->gen_types_mutex); - defer (mutex_unlock(&ctx->info->gen_types_mutex)); + rw_mutex_shared_lock(&ctx->info->gen_types_mutex); // @@global auto *found_gen_types = map_get(&ctx->info->gen_types, original_type); - if (found_gen_types != nullptr) { - // GB_ASSERT_MSG(ordered_operands.count >= param_count, "%td >= %td", ordered_operands.count, param_count); - - for_array(i, *found_gen_types) { - Entity *e = (*found_gen_types)[i]; - Type *t = base_type(e->type); - TypeTuple *tuple = get_record_polymorphic_params(t); - GB_ASSERT(param_count == tuple->variables.count); - - bool skip = false; - - for (isize j = 0; j < param_count; j++) { - Entity *p = tuple->variables[j]; - Operand o = {}; - if (j < ordered_operands.count) { - o = ordered_operands[j]; + if (found_gen_types == nullptr) { + rw_mutex_shared_unlock(&ctx->info->gen_types_mutex); // @@global + return nullptr; + } + + rw_mutex_shared_lock(&found_gen_types->mutex); // @@local + defer (rw_mutex_shared_unlock(&found_gen_types->mutex)); // @@local + + rw_mutex_shared_unlock(&ctx->info->gen_types_mutex); // @@global + + for (Entity *e : found_gen_types->types) { + Type *t = base_type(e->type); + TypeTuple *tuple = get_record_polymorphic_params(t); + GB_ASSERT(param_count == tuple->variables.count); + + bool skip = false; + + for (isize j = 0; j < param_count; j++) { + Entity *p = tuple->variables[j]; + Operand o = {}; + if (j < ordered_operands.count) { + o = ordered_operands[j]; + } + if (o.expr == nullptr) { + continue; + } + Entity *oe = entity_of_node(o.expr); + if (p == oe) { + // NOTE(bill): This is the same type, make sure that it will be be same thing and use that + // Saves on a lot of checking too below + continue; + } + + if (p->kind == Entity_TypeName) { + if (is_type_polymorphic(o.type)) { + // NOTE(bill): Do not add polymorphic version to the gen_types + skip = true; + break; } - if (o.expr == nullptr) { - continue; + if (!are_types_identical(o.type, p->type)) { + skip = true; + break; } - Entity *oe = entity_of_node(o.expr); - if (p == oe) { - // NOTE(bill): This is the same type, make sure that it will be be same thing and use that - // Saves on a lot of checking too below - continue; + } else if (p->kind == Entity_Constant) { + if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) { + skip = true; + break; } - - if (p->kind == Entity_TypeName) { - if (is_type_polymorphic(o.type)) { - // NOTE(bill): Do not add polymorphic version to the gen_types - skip = true; - break; - } - if (!are_types_identical(o.type, p->type)) { - skip = true; - break; - } - } else if (p->kind == Entity_Constant) { - if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) { - skip = true; - break; - } - if (!are_types_identical(o.type, p->type)) { - skip = true; - break; - } - } else { - GB_PANIC("Unknown entity kind"); + if (!are_types_identical(o.type, p->type)) { + skip = true; + break; } + } else { + GB_PANIC("Unknown entity kind"); } - if (!skip) { - return e; - } + } + if (!skip) { + return e; } } return nullptr; @@ -346,16 +350,19 @@ gb_internal void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, T // TODO(bill): Is this even correct? Or should the metadata be copied? e->TypeName.objc_metadata = original_type->Named.type_name->TypeName.objc_metadata; - mutex_lock(&ctx->info->gen_types_mutex); + rw_mutex_lock(&ctx->info->gen_types_mutex); auto *found_gen_types = map_get(&ctx->info->gen_types, original_type); if (found_gen_types) { - array_add(found_gen_types, e); + rw_mutex_lock(&found_gen_types->mutex); + array_add(&found_gen_types->types, e); + rw_mutex_unlock(&found_gen_types->mutex); } else { - auto array = array_make(heap_allocator()); - array_add(&array, e); - map_set(&ctx->info->gen_types, original_type, array); + GenTypesData gen_types = {}; + gen_types.types = array_make(heap_allocator()); + array_add(&gen_types.types, e); + map_set(&ctx->info->gen_types, original_type, gen_types); } - mutex_unlock(&ctx->info->gen_types_mutex); + rw_mutex_unlock(&ctx->info->gen_types_mutex); } gb_internal Type *check_record_polymorphic_params(CheckerContext *ctx, Ast *polymorphic_params, diff --git a/src/checker.hpp b/src/checker.hpp index 821d43922..92926689e 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -327,6 +327,11 @@ struct GenProcsData { RwMutex mutex; }; +struct GenTypesData { + Array types; + RwMutex mutex; +}; + // CheckerInfo stores all the symbol information for a type-checked program struct CheckerInfo { Checker *checker; @@ -364,9 +369,9 @@ struct CheckerInfo { RecursiveMutex lazy_mutex; // Mutex required for lazy type checking of specific files BlockingMutex gen_procs_mutex; - RecursiveMutex gen_types_mutex; + RwMutex gen_types_mutex; PtrMap gen_procs; // Key: Ast * | Identifier -> Entity - PtrMap > gen_types; + PtrMap gen_types; BlockingMutex type_info_mutex; // NOT recursive Array type_info_types; diff --git a/src/path.cpp b/src/path.cpp index 500a40cc2..4b426fc87 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -222,7 +222,6 @@ gb_internal i64 get_file_size(String path) { gb_internal ReadDirectoryError read_directory(String path, Array *fi) { GB_ASSERT(fi != nullptr); - gbAllocator a = heap_allocator(); while (path.len > 0) { Rune end = path[path.len-1]; @@ -239,9 +238,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array *fi) return ReadDirectory_InvalidPath; } { - char *c_str = alloc_cstring(a, path); - defer (gb_free(a, c_str)); - + char *c_str = alloc_cstring(temporary_allocator(), path); gbFile f = {}; gbFileError file_err = gb_file_open(&f, c_str); defer (gb_file_close(&f)); @@ -258,6 +255,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array *fi) } + gbAllocator a = heap_allocator(); char *new_path = gb_alloc_array(a, char, path.len+3); defer (gb_free(a, new_path)); @@ -280,8 +278,8 @@ gb_internal ReadDirectoryError read_directory(String path, Array *fi) do { wchar_t *filename_w = file_data.cFileName; - i64 size = cast(i64)file_data.nFileSizeLow; - size |= (cast(i64)file_data.nFileSizeHigh) << 32; + u64 size = cast(u64)file_data.nFileSizeLow; + size |= (cast(u64)file_data.nFileSizeHigh) << 32; String name = string16_to_string(a, make_string16_c(filename_w)); if (name == "." || name == "..") { gb_free(a, name.text); @@ -299,7 +297,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array *fi) FileInfo info = {}; info.name = name; info.fullpath = path_to_full_path(a, filepath); - info.size = size; + info.size = cast(i64)size; info.is_dir = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; array_add(fi, info); } while (FindNextFileW(find_file, &file_data)); -- cgit v1.2.3 From 7124d541a132fc94b2c66c54bd73eb0d103ce3d3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 11 Jan 2023 18:10:27 +0000 Subject: General optimizations --- src/check_type.cpp | 3 ++- src/checker.cpp | 15 ++++++++------- src/checker.hpp | 2 +- src/ptr_map.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/types.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 99 insertions(+), 11 deletions(-) (limited to 'src/check_type.cpp') diff --git a/src/check_type.cpp b/src/check_type.cpp index fd4e965d4..0863af967 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2405,7 +2405,8 @@ gb_internal Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_e } soa_struct->Struct.soa_count = cast(i32)count; - scope = create_scope(ctx->info, ctx->scope, 8); + scope = create_scope(ctx->info, ctx->scope); + string_map_init(&scope->elements, 8); soa_struct->Struct.scope = scope; String params_xyzw[4] = { diff --git a/src/checker.cpp b/src/checker.cpp index 4e8d19016..473af7128 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -220,11 +220,9 @@ gb_internal DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) { -gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_elements_capacity=DEFAULT_SCOPE_CAPACITY) { +gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent) { Scope *s = gb_alloc_item(permanent_allocator(), Scope); s->parent = parent; - string_map_init(&s->elements, init_elements_capacity); - ptr_set_init(&s->imported, 0); if (parent != nullptr && parent != builtin_pkg->scope) { Scope *prev_head_child = parent->head_child.exchange(s, std::memory_order_acq_rel); @@ -246,7 +244,8 @@ gb_internal Scope *create_scope_from_file(CheckerInfo *info, AstFile *f) { GB_ASSERT(f->pkg->scope != nullptr); isize init_elements_capacity = gb_max(DEFAULT_SCOPE_CAPACITY, 2*f->total_file_decl_count); - Scope *s = create_scope(info, f->pkg->scope, init_elements_capacity); + Scope *s = create_scope(info, f->pkg->scope); + string_map_init(&s->elements, init_elements_capacity); s->flags |= ScopeFlag_File; @@ -265,7 +264,8 @@ gb_internal Scope *create_scope_from_package(CheckerContext *c, AstPackage *pkg) } isize init_elements_capacity = gb_max(DEFAULT_SCOPE_CAPACITY, 2*total_pkg_decl_count); - Scope *s = create_scope(c->info, builtin_pkg->scope, init_elements_capacity); + Scope *s = create_scope(c->info, builtin_pkg->scope); + string_map_init(&s->elements, init_elements_capacity); s->flags |= ScopeFlag_Pkg; s->pkg = pkg; @@ -1753,7 +1753,8 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) { add_type_info_dependency(c->info, c->decl, t); MUTEX_GUARD_BLOCK(&c->info->type_info_mutex) { - auto found = map_get(&c->info->type_info_map, t); + MapFindResult fr; + auto found = map_try_get(&c->info->type_info_map, t, &fr); if (found != nullptr) { // Types have already been added return; @@ -1777,7 +1778,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) { ti_index = c->info->type_info_types.count; array_add(&c->info->type_info_types, t); } - map_set(&c->checker->info.type_info_map, t, ti_index); + map_set_internal_from_try_get(&c->checker->info.type_info_map, t, ti_index, fr); if (prev) { // NOTE(bill): If a previous one exists already, no need to continue diff --git a/src/checker.hpp b/src/checker.hpp index 8b8819d97..806eb2e51 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -223,7 +223,7 @@ enum ScopeFlag : i32 { ScopeFlag_ContextDefined = 1<<16, }; -enum { DEFAULT_SCOPE_CAPACITY = 29 }; +enum { DEFAULT_SCOPE_CAPACITY = 32 }; struct Scope { Ast * node; diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 89d2cbf9d..598904906 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -192,6 +192,26 @@ gb_internal void map_rehash(PtrMap *h, isize new_count) { template gb_internal V *map_get(PtrMap *h, K key) { + MapIndex hash_index = MAP_SENTINEL; + MapIndex entry_prev = MAP_SENTINEL; + MapIndex entry_index = MAP_SENTINEL; + if (h->hashes.count != 0) { + u32 hash = ptr_map_hash_key(key); + hash_index = cast(MapIndex)(hash & (h->hashes.count-1)); + entry_index = h->hashes.data[hash_index]; + while (entry_index != MAP_SENTINEL) { + auto *entry = &h->entries.data[entry_index]; + if (entry->key == key) { + return &entry->value; + } + entry_prev = entry_index; + entry_index = entry->next; + } + } + return nullptr; +} +template +gb_internal V *map_try_get(PtrMap *h, K key, MapFindResult *fr_) { MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; if (h->hashes.count != 0) { u32 hash = ptr_map_hash_key(key); @@ -206,9 +226,25 @@ gb_internal V *map_get(PtrMap *h, K key) { fr.entry_index = entry->next; } } + if (h->hashes.count == 0 || map__full(h)) { + map_grow(h); + } + if (fr_) *fr_ = fr; return nullptr; } + +template +gb_internal void map_set_internal_from_try_get(PtrMap *h, K key, V const &value, MapFindResult const &fr) { + MapIndex index = map__add_entry(h, key); + if (fr.entry_prev != MAP_SENTINEL) { + h->entries.data[fr.entry_prev].next = index; + } else { + h->hashes.data[fr.hash_index] = index; + } + h->entries.data[index].value = value; +} + template gb_internal V &map_must_get(PtrMap *h, K key) { V *ptr = map_get(h, key); diff --git a/src/types.cpp b/src/types.cpp index 99f393cc5..69c1ebe68 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2528,9 +2528,58 @@ gb_internal bool lookup_subtype_polymorphic_selection(Type *dst, Type *src, Sele gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple_names); gb_internal bool are_types_identical(Type *x, Type *y) { + if (x == y) { + return true; + } + + if ((x == nullptr && y != nullptr) || + (x != nullptr && y == nullptr)) { + return false; + } + + if (x->kind == Type_Named) { + Entity *e = x->Named.type_name; + if (e->TypeName.is_type_alias) { + x = x->Named.base; + } + } + if (y->kind == Type_Named) { + Entity *e = y->Named.type_name; + if (e->TypeName.is_type_alias) { + y = y->Named.base; + } + } + if (x->kind != y->kind) { + return false; + } + return are_types_identical_internal(x, y, false); } gb_internal bool are_types_identical_unique_tuples(Type *x, Type *y) { + if (x == y) { + return true; + } + + if (!x | !y) { + return false; + } + + if (x->kind == Type_Named) { + Entity *e = x->Named.type_name; + if (e->TypeName.is_type_alias) { + x = x->Named.base; + } + } + if (y->kind == Type_Named) { + Entity *e = y->Named.type_name; + if (e->TypeName.is_type_alias) { + y = y->Named.base; + } + } + if (x->kind != y->kind) { + return false; + } + return are_types_identical_internal(x, y, true); } @@ -2540,11 +2589,11 @@ gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple return true; } - if ((x == nullptr && y != nullptr) || - (x != nullptr && y == nullptr)) { + if (!x | !y) { return false; } + #if 0 if (x->kind == Type_Named) { Entity *e = x->Named.type_name; if (e->TypeName.is_type_alias) { @@ -2560,6 +2609,7 @@ gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple if (x->kind != y->kind) { return false; } + #endif switch (x->kind) { case Type_Generic: -- cgit v1.2.3