Browse Source

Increase usage of `PtrMap`

gingerBill 3 years ago
parent
commit
6646348e1a
9 changed files with 51 additions and 61 deletions
  1. 1 1
      src/build_settings.cpp
  2. 2 2
      src/check_expr.cpp
  3. 3 3
      src/check_type.cpp
  4. 18 22
      src/checker.cpp
  5. 2 2
      src/checker.hpp
  6. 17 27
      src/docs_writer.cpp
  7. 1 1
      src/llvm_backend_stmt.cpp
  8. 1 1
      src/main.cpp
  9. 6 2
      src/ptr_map.cpp

+ 1 - 1
src/build_settings.cpp

@@ -244,7 +244,7 @@ struct BuildContext {
 	gbAffinity affinity;
 	isize      thread_count;
 
-	Map<ExactValue> defined_values; // Key:
+	PtrMap<char const *, ExactValue> defined_values;
 };
 
 

+ 2 - 2
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<Entity *>(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) {

+ 3 - 3
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<Entity *>(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);
 }

+ 18 - 22
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<EntityGraphNode *> *M,
 }
 
 Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInfo *info, gbAllocator allocator) {
-	Map<EntityGraphNode *> M = {}; // Key: Entity *
+	PtrMap<Entity *, EntityGraphNode *> M = {};
 	map_init(&M, allocator, info->entities.count);
 	defer (map_destroy(&M));
 	for_array(i, info->entities) {
@@ -2207,7 +2207,7 @@ Array<EntityGraphNode *> 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<EntityGraphNode *> 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<EntityGraphNode *> 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<ImportGraphNode *> *M) {
+void add_import_dependency_node(Checker *c, Ast *decl, PtrMap<AstPackage *, ImportGraphNode *> *M) {
 	AstPackage *parent_pkg = decl->file->pkg;
 
 	switch (decl->kind) {
@@ -3752,11 +3750,11 @@ void add_import_dependency_node(Checker *c, Ast *decl, Map<ImportGraphNode *> *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<ImportGraphNode *> *M
 }
 
 Array<ImportGraphNode *> generate_import_dependency_graph(Checker *c) {
-	Map<ImportGraphNode *> M = {}; // Key: AstPackage *
+	PtrMap<AstPackage *, ImportGraphNode *> 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<Entity *> find_entity_path(Entity *start, Entity *end, Map<Entity *> *visited = nullptr);
+Array<Entity *> find_entity_path(Entity *start, Entity *end, PtrSet<Entity *> *visited = nullptr);
 
-bool find_entity_path_tuple(Type *tuple, Entity *end, Map<Entity *> *visited, Array<Entity *> *path_) {
+bool find_entity_path_tuple(Type *tuple, Entity *end, PtrSet<Entity *> *visited, Array<Entity *> *path_) {
 	GB_ASSERT(path_ != nullptr);
 	if (tuple == nullptr) {
 		return false;
@@ -4541,26 +4539,24 @@ bool find_entity_path_tuple(Type *tuple, Entity *end, Map<Entity *> *visited, Ar
 	return false;
 }
 
-Array<Entity *> find_entity_path(Entity *start, Entity *end, Map<Entity *> *visited) {
-	Map<Entity *> visited_ = {};
+Array<Entity *> find_entity_path(Entity *start, Entity *end, PtrSet<Entity *> *visited) {
+	PtrSet<Entity *> 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<Entity *> 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) {

+ 2 - 2
src/checker.hpp

@@ -316,8 +316,8 @@ struct CheckerInfo {
 
 	RecursiveMutex gen_procs_mutex;
 	RecursiveMutex gen_types_mutex;
-	Map<Array<Entity *> > gen_procs; // Key: Ast * | Identifier -> Entity
-	Map<Array<Entity *> > gen_types; // Key: Type *
+	PtrMap<Ast *, Array<Entity *> > gen_procs; // Key: Ast * | Identifier -> Entity
+	PtrMap<Type *, Array<Entity *> > gen_types; 
 
 	BlockingMutex type_info_mutex; // NOT recursive
 	Array<Type *> type_info_types;

+ 17 - 27
src/docs_writer.cpp

@@ -26,12 +26,10 @@ struct OdinDocWriter {
 
 	StringMap<OdinDocString> string_cache;
 
-	Map<OdinDocFileIndex>   file_cache;      // Key: AstFile *
-	Map<OdinDocPkgIndex>    pkg_cache;       // Key: AstPackage *
-	Map<OdinDocEntityIndex> entity_cache;    // Key: Entity *
-	Map<Entity *>           entity_id_cache; // Key: OdinDocEntityIndex
-	Map<OdinDocTypeIndex>   type_cache;      // Key: Type *
-	Map<Type *>             type_id_cache;   // Key: OdinDocTypeIndex
+	PtrMap<AstFile *,    OdinDocFileIndex>   file_cache;
+	PtrMap<AstPackage *, OdinDocPkgIndex>    pkg_cache;
+	PtrMap<Entity *,     OdinDocEntityIndex> entity_cache;
+	PtrMap<Type *,       OdinDocTypeIndex>   type_cache;
 
 	OdinDocWriterItemTracker<OdinDocFile>   files;
 	OdinDocWriterItemTracker<OdinDocPkg>    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<OdinDocEntityIndex> 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<OdinDocFileIndex>(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);
 		}
 

+ 1 - 1
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) {

+ 1 - 1
src/main.cpp

@@ -1109,7 +1109,7 @@ bool parse_build_flags(Array<String> 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));

+ 6 - 2
src/ptr_map.cpp

@@ -18,8 +18,12 @@ struct PtrMap {
 };
 
 
-template <typename K>
-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));
 }