diff --git a/src/build_settings.cpp b/src/build_settings.cpp index fa8922f61..182975d7b 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -244,7 +244,7 @@ struct BuildContext { gbAffinity affinity; isize thread_count; - Map defined_values; // Key: + PtrMap defined_values; }; diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 2ad3cc4a6..7a0273805 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -314,7 +314,7 @@ bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, Entity *base_ return false; } - auto *found_gen_procs = map_get(&info->gen_procs, hash_pointer(base_entity->identifier)); + auto *found_gen_procs = map_get(&info->gen_procs, base_entity->identifier.load()); if (found_gen_procs) { auto procs = *found_gen_procs; for_array(i, procs) { @@ -423,7 +423,7 @@ bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, Entity *base_ } else { auto array = array_make(heap_allocator()); array_add(&array, entity); - map_set(&info->gen_procs, hash_pointer(base_entity->identifier), array); + map_set(&info->gen_procs, base_entity->identifier.load(), array); } if (poly_proc_data) { diff --git a/src/check_type.cpp b/src/check_type.cpp index 05d5c674a..398967af8 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -228,7 +228,7 @@ Entity *find_polymorphic_record_entity(CheckerContext *ctx, Type *original_type, mutex_lock(&ctx->info->gen_types_mutex); defer (mutex_unlock(&ctx->info->gen_types_mutex)); - auto *found_gen_types = map_get(&ctx->info->gen_types, hash_pointer(original_type)); + 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); @@ -311,13 +311,13 @@ void add_polymorphic_record_entity(CheckerContext *ctx, Ast *node, Type *named_t named_type->Named.type_name = e; mutex_lock(&ctx->info->gen_types_mutex); - auto *found_gen_types = map_get(&ctx->info->gen_types, hash_pointer(original_type)); + auto *found_gen_types = map_get(&ctx->info->gen_types, original_type); if (found_gen_types) { array_add(found_gen_types, e); } else { auto array = array_make(heap_allocator()); array_add(&array, e); - map_set(&ctx->info->gen_types, hash_pointer(original_type), array); + map_set(&ctx->info->gen_types, original_type, array); } mutex_unlock(&ctx->info->gen_types_mutex); } diff --git a/src/checker.cpp b/src/checker.cpp index 044342760..16e699ee3 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -810,7 +810,7 @@ void init_universal(void) { bool defined_values_double_declaration = false; for_array(i, bc->defined_values.entries) { - char const *name = cast(char const *)cast(uintptr)bc->defined_values.entries[i].key.key; + char const *name = bc->defined_values.entries[i].key; ExactValue value = bc->defined_values.entries[i].value; GB_ASSERT(value.kind != ExactValue_Invalid); @@ -2199,7 +2199,7 @@ void add_entity_dependency_from_procedure_parameters(Map *M, } Array generate_entity_dependency_graph(CheckerInfo *info, gbAllocator allocator) { - Map M = {}; // Key: Entity * + PtrMap M = {}; map_init(&M, allocator, info->entities.count); defer (map_destroy(&M)); for_array(i, info->entities) { @@ -2207,7 +2207,7 @@ Array generate_entity_dependency_graph(CheckerInfo *info, gbA if (is_entity_a_dependency(e)) { EntityGraphNode *n = gb_alloc_item(allocator, EntityGraphNode); n->entity = e; - map_set(&M, hash_pointer(e), n); + map_set(&M, e, n); } } @@ -2227,9 +2227,7 @@ Array generate_entity_dependency_graph(CheckerInfo *info, gbA } GB_ASSERT(dep != nullptr); if (is_entity_a_dependency(dep)) { - EntityGraphNode **m_ = map_get(&M, hash_pointer(dep)); - GB_ASSERT(m_ != nullptr); - EntityGraphNode *m = *m_; + EntityGraphNode *m = map_must_get(&M, dep); entity_graph_node_set_add(&n->succ, m); entity_graph_node_set_add(&m->pred, n); } @@ -2244,7 +2242,7 @@ Array generate_entity_dependency_graph(CheckerInfo *info, gbA for_array(i, M.entries) { auto *entry = &M.entries[i]; - auto *e = cast(Entity *)cast(uintptr)entry->key.key; + auto *e = entry->key; EntityGraphNode *n = entry->value; if (e->kind == Entity_Procedure) { @@ -3728,7 +3726,7 @@ String path_to_entity_name(String name, String fullpath, bool strip_extension=tr #if 1 -void add_import_dependency_node(Checker *c, Ast *decl, Map *M) { +void add_import_dependency_node(Checker *c, Ast *decl, PtrMap *M) { AstPackage *parent_pkg = decl->file->pkg; switch (decl->kind) { @@ -3752,11 +3750,11 @@ void add_import_dependency_node(Checker *c, Ast *decl, Map *M ImportGraphNode *m = nullptr; ImportGraphNode *n = nullptr; - found_node = map_get(M, hash_pointer(pkg)); + found_node = map_get(M, pkg); GB_ASSERT(found_node != nullptr); m = *found_node; - found_node = map_get(M, hash_pointer(parent_pkg)); + found_node = map_get(M, parent_pkg); GB_ASSERT(found_node != nullptr); n = *found_node; @@ -3794,14 +3792,14 @@ void add_import_dependency_node(Checker *c, Ast *decl, Map *M } Array generate_import_dependency_graph(Checker *c) { - Map M = {}; // Key: AstPackage * + PtrMap M = {}; map_init(&M, heap_allocator(), 2*c->parser->packages.count); defer (map_destroy(&M)); for_array(i, c->parser->packages) { AstPackage *pkg = c->parser->packages[i]; ImportGraphNode *n = import_graph_node_create(heap_allocator(), pkg); - map_set(&M, hash_pointer(pkg), n); + map_set(&M, pkg, n); } // Calculate edges for graph M @@ -4507,9 +4505,9 @@ void check_import_entities(Checker *c) { } -Array find_entity_path(Entity *start, Entity *end, Map *visited = nullptr); +Array find_entity_path(Entity *start, Entity *end, PtrSet *visited = nullptr); -bool find_entity_path_tuple(Type *tuple, Entity *end, Map *visited, Array *path_) { +bool find_entity_path_tuple(Type *tuple, Entity *end, PtrSet *visited, Array *path_) { GB_ASSERT(path_ != nullptr); if (tuple == nullptr) { return false; @@ -4541,26 +4539,24 @@ bool find_entity_path_tuple(Type *tuple, Entity *end, Map *visited, Ar return false; } -Array find_entity_path(Entity *start, Entity *end, Map *visited) { - Map visited_ = {}; +Array find_entity_path(Entity *start, Entity *end, PtrSet *visited) { + PtrSet visited_ = {}; bool made_visited = false; if (visited == nullptr) { made_visited = true; - map_init(&visited_, heap_allocator()); + ptr_set_init(&visited_, heap_allocator()); visited = &visited_; } defer (if (made_visited) { - map_destroy(&visited_); + ptr_set_destroy(&visited_); }); Array empty_path = {}; - HashKey key = hash_pointer(start); - - if (map_get(visited, key) != nullptr) { + if (ptr_set_exists(visited, start)) { return empty_path; } - map_set(visited, key, start); + ptr_set_add(visited, start); DeclInfo *decl = start->decl_info; if (decl) { diff --git a/src/checker.hpp b/src/checker.hpp index 700cd7e54..ab8fce3d8 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -316,8 +316,8 @@ struct CheckerInfo { RecursiveMutex gen_procs_mutex; RecursiveMutex gen_types_mutex; - Map > gen_procs; // Key: Ast * | Identifier -> Entity - Map > gen_types; // Key: Type * + PtrMap > gen_procs; // Key: Ast * | Identifier -> Entity + PtrMap > gen_types; BlockingMutex type_info_mutex; // NOT recursive Array type_info_types; diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 1603ca22c..e7254e16b 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -26,12 +26,10 @@ struct OdinDocWriter { StringMap string_cache; - Map file_cache; // Key: AstFile * - Map pkg_cache; // Key: AstPackage * - Map entity_cache; // Key: Entity * - Map entity_id_cache; // Key: OdinDocEntityIndex - Map type_cache; // Key: Type * - Map type_id_cache; // Key: OdinDocTypeIndex + PtrMap file_cache; + PtrMap pkg_cache; + PtrMap entity_cache; + PtrMap type_cache; OdinDocWriterItemTracker files; OdinDocWriterItemTracker pkgs; @@ -61,9 +59,7 @@ void odin_doc_writer_prepare(OdinDocWriter *w) { map_init(&w->file_cache, a); map_init(&w->pkg_cache, a); map_init(&w->entity_cache, a); - map_init(&w->entity_id_cache, a); map_init(&w->type_cache, a); - map_init(&w->type_id_cache, a); odin_doc_writer_item_tracker_init(&w->files, 1); odin_doc_writer_item_tracker_init(&w->pkgs, 1); @@ -81,9 +77,7 @@ void odin_doc_writer_destroy(OdinDocWriter *w) { map_destroy(&w->file_cache); map_destroy(&w->pkg_cache); map_destroy(&w->entity_cache); - map_destroy(&w->entity_id_cache); map_destroy(&w->type_cache); - map_destroy(&w->type_id_cache); } @@ -115,9 +109,7 @@ void odin_doc_writer_start_writing(OdinDocWriter *w) { map_clear(&w->file_cache); map_clear(&w->pkg_cache); map_clear(&w->entity_cache); - map_clear(&w->entity_id_cache); map_clear(&w->type_cache); - map_clear(&w->type_id_cache); isize total_size = odin_doc_writer_calc_total_size(w); total_size = align_formula_isize(total_size, 8); @@ -267,7 +259,7 @@ OdinDocPosition odin_doc_token_pos_cast(OdinDocWriter *w, TokenPos const &pos) { if (pos.file_id != 0) { AstFile *file = get_ast_file_from_id(pos.file_id); if (file != nullptr) { - OdinDocFileIndex *file_index_found = map_get(&w->file_cache, hash_pointer(file)); + OdinDocFileIndex *file_index_found = map_get(&w->file_cache, file); GB_ASSERT(file_index_found != nullptr); file_index = *file_index_found; } @@ -483,16 +475,16 @@ OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { if (type == nullptr) { return 0; } - OdinDocTypeIndex *found = map_get(&w->type_cache, hash_pointer(type)); + OdinDocTypeIndex *found = map_get(&w->type_cache, type); if (found) { return *found; } for_array(i, w->type_cache.entries) { // NOTE(bill): THIS IS SLOW - Type *other = cast(Type *)cast(uintptr)w->type_cache.entries[i].key.key; + Type *other = w->type_cache.entries[i].key; if (are_types_identical(type, other)) { OdinDocTypeIndex index = w->type_cache.entries[i].value; - map_set(&w->type_cache, hash_pointer(type), index); + map_set(&w->type_cache, type, index); return index; } } @@ -502,8 +494,7 @@ OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { OdinDocType doc_type = {}; OdinDocTypeIndex type_index = 0; type_index = odin_doc_write_item(w, &w->types, &doc_type, &dst); - map_set(&w->type_cache, hash_pointer(type), type_index); - map_set(&w->type_id_cache, hash_integer(type_index), type); + map_set(&w->type_cache, type, type_index); switch (type->kind) { case Type_Basic: @@ -776,12 +767,12 @@ OdinDocEntityIndex odin_doc_add_entity(OdinDocWriter *w, Entity *e) { return 0; } - OdinDocEntityIndex *prev_index = map_get(&w->entity_cache, hash_pointer(e)); + OdinDocEntityIndex *prev_index = map_get(&w->entity_cache, e); if (prev_index) { return *prev_index; } - if (e->pkg != nullptr && map_get(&w->pkg_cache, hash_pointer(e->pkg)) == nullptr) { + if (e->pkg != nullptr && map_get(&w->pkg_cache, e->pkg) == nullptr) { return 0; } @@ -789,8 +780,7 @@ OdinDocEntityIndex odin_doc_add_entity(OdinDocWriter *w, Entity *e) { OdinDocEntity* dst = nullptr; OdinDocEntityIndex doc_entity_index = odin_doc_write_item(w, &w->entities, &doc_entity, &dst); - map_set(&w->entity_cache, hash_pointer(e), doc_entity_index); - map_set(&w->entity_id_cache, hash_integer(doc_entity_index), e); + map_set(&w->entity_cache, e, doc_entity_index); Ast *type_expr = nullptr; @@ -901,7 +891,7 @@ void odin_doc_update_entities(OdinDocWriter *w) { defer (array_free(&entities)); for_array(i, w->entity_cache.entries) { - Entity *e = cast(Entity *)cast(uintptr)w->entity_cache.entries[i].key.key; + Entity *e = w->entity_cache.entries[i].key; entities[i] = e; } for_array(i, entities) { @@ -912,7 +902,7 @@ void odin_doc_update_entities(OdinDocWriter *w) { } for_array(i, w->entity_cache.entries) { - Entity *e = cast(Entity *)cast(uintptr)w->entity_cache.entries[i].key.key; + Entity *e = w->entity_cache.entries[i].key; OdinDocEntityIndex entity_index = w->entity_cache.entries[i].value; OdinDocTypeIndex type_index = odin_doc_type(w, e->type); @@ -955,7 +945,7 @@ OdinDocArray odin_doc_add_pkg_entities(OdinDocWriter *w, Ast if (pkg->scope == nullptr) { return {}; } - if (map_get(&w->pkg_cache, hash_pointer(pkg)) == nullptr) { + if (map_get(&w->pkg_cache, pkg) == nullptr) { return {}; } @@ -1056,7 +1046,7 @@ void odin_doc_write_docs(OdinDocWriter *w) { OdinDocPkg *dst = nullptr; OdinDocPkgIndex pkg_index = odin_doc_write_item(w, &w->pkgs, &doc_pkg, &dst); - map_set(&w->pkg_cache, hash_pointer(pkg), pkg_index); + map_set(&w->pkg_cache, pkg, pkg_index); auto file_indices = array_make(heap_allocator(), 0, pkg->files.count); defer (array_free(&file_indices)); @@ -1067,7 +1057,7 @@ void odin_doc_write_docs(OdinDocWriter *w) { doc_file.pkg = pkg_index; doc_file.name = odin_doc_write_string(w, file->fullpath); OdinDocFileIndex file_index = odin_doc_write_item(w, &w->files, &doc_file); - map_set(&w->file_cache, hash_pointer(file), file_index); + map_set(&w->file_cache, file, file_index); array_add(&file_indices, file_index); } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 4f57dbfa6..9d3d2c949 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -83,7 +83,7 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { DeclInfo *decl = decl_info_of_entity(e); ast_node(pl, ProcLit, decl->proc_lit); if (pl->body != nullptr) { - auto *found = map_get(&info->gen_procs, hash_pointer(ident)); + auto *found = map_get(&info->gen_procs, ident); if (found) { auto procs = *found; for_array(i, procs) { diff --git a/src/main.cpp b/src/main.cpp index e79e7203d..3fed50c80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1109,7 +1109,7 @@ bool parse_build_flags(Array args) { break; } - HashKey key = hash_pointer(string_intern(name)); + char const *key = string_intern(name); if (map_get(&build_context.defined_values, key) != nullptr) { gb_printf_err("Defined constant '%.*s' already exists\n", LIT(name)); diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 4659fcf9b..2387a2a20 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -18,8 +18,12 @@ struct PtrMap { }; -template -u32 ptr_map_hash_key(K key) { +u32 ptr_map_hash_key(void const *key) { + // TODO(bill): Improve ptr_map_hash_key + return gb_fnv32a(&key, gb_size_of(key)); +} +u32 ptr_map_hash_key(uintptr key) { + // TODO(bill): Improve ptr_map_hash_key return gb_fnv32a(&key, gb_size_of(key)); }