Browse Source

Improve usage of `file_id`

gingerBill 3 years ago
parent
commit
e814a3693f
9 changed files with 75 additions and 30 deletions
  1. 2 2
      src/check_builtin.cpp
  2. 2 2
      src/checker.cpp
  3. 51 12
      src/common.cpp
  4. 1 1
      src/docs_writer.cpp
  5. 6 6
      src/parser.cpp
  6. 6 2
      src/parser.hpp
  7. 3 1
      src/ptr_set.cpp
  8. 1 1
      src/thread_pool.cpp
  9. 3 3
      src/tokenizer.cpp

+ 2 - 2
src/check_builtin.cpp

@@ -446,9 +446,9 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
 				} else if (hash_kind == "fnv64") {
 					hash_value = gb_fnv64(data, file_size);
 				} else if (hash_kind == "fnv32a") {
-					hash_value = gb_fnv32a(data, file_size);
+					hash_value = fnv32a(data, file_size);
 				} else if (hash_kind == "fnv64a") {
-					hash_value = gb_fnv64a(data, file_size);
+					hash_value = fnv64a(data, file_size);
 				} else if (hash_kind == "murmur32") {
 					hash_value = gb_murmur32(data, file_size);
 				} else if (hash_kind == "murmur64") {

+ 2 - 2
src/checker.cpp

@@ -225,8 +225,8 @@ bool decl_info_has_init(DeclInfo *d) {
 Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_elements_capacity=DEFAULT_SCOPE_CAPACITY) {
 	Scope *s = gb_alloc_item(permanent_allocator(), Scope);
 	s->parent = parent;
-	string_map_init(&s->elements, heap_allocator(), init_elements_capacity);
-	ptr_set_init(&s->imported, heap_allocator(), 0);
+	string_map_init(&s->elements, permanent_allocator(), init_elements_capacity);
+	ptr_set_init(&s->imported, permanent_allocator(), 0);
 	mutex_init(&s->mutex);
 
 	if (parent != nullptr && parent != builtin_pkg->scope) {

+ 51 - 12
src/common.cpp

@@ -83,9 +83,20 @@ int i32_cmp(i32 x, i32 y) {
 u32 fnv32a(void const *data, isize len) {
 	u8 const *bytes = cast(u8 const *)data;
 	u32 h = 0x811c9dc5;
-	for (isize i = 0; i < len; i++) {
-		u32 b = cast(u32)bytes[i];
-		h = (h ^ b) * 0x01000193;
+	
+	for (; len >= 8; len -= 8, bytes += 8) {
+		h = (h ^ bytes[0]) * 0x01000193;
+		h = (h ^ bytes[1]) * 0x01000193;
+		h = (h ^ bytes[2]) * 0x01000193;
+		h = (h ^ bytes[3]) * 0x01000193;
+		h = (h ^ bytes[4]) * 0x01000193;
+		h = (h ^ bytes[5]) * 0x01000193;
+		h = (h ^ bytes[6]) * 0x01000193;
+		h = (h ^ bytes[7]) * 0x01000193;
+	}
+
+	while (len--) {
+		h = (h ^ *bytes++) * 0x01000193;
 	}
 	return h;
 }
@@ -93,20 +104,48 @@ u32 fnv32a(void const *data, isize len) {
 u64 fnv64a(void const *data, isize len) {
 	u8 const *bytes = cast(u8 const *)data;
 	u64 h = 0xcbf29ce484222325ull;
-	for (isize i = 0; i < len; i++) {
-		u64 b = cast(u64)bytes[i];
-		h = (h ^ b) * 0x100000001b3ull;
+	
+	for (; len >= 8; len -= 8, bytes += 8) {
+		h = (h ^ bytes[0]) * 0x100000001b3ull;
+		h = (h ^ bytes[1]) * 0x100000001b3ull;
+		h = (h ^ bytes[2]) * 0x100000001b3ull;
+		h = (h ^ bytes[3]) * 0x100000001b3ull;
+		h = (h ^ bytes[4]) * 0x100000001b3ull;
+		h = (h ^ bytes[5]) * 0x100000001b3ull;
+		h = (h ^ bytes[6]) * 0x100000001b3ull;
+		h = (h ^ bytes[7]) * 0x100000001b3ull;
+	}
+
+	while (len--) {
+		h = (h ^ *bytes++) * 0x100000001b3ull;
 	}
 	return h;
 }
 
 u64 u64_digit_value(Rune r) {
-	if ('0' <= r && r <= '9') {
-		return r - '0';
-	} else if ('a' <= r && r <= 'f') {
-		return r - 'a' + 10;
-	} else if ('A' <= r && r <= 'F') {
-		return r - 'A' + 10;
+	switch (r) {
+	case '0': return 0;
+	case '1': return 1;
+	case '2': return 2;
+	case '3': return 3;
+	case '4': return 4;
+	case '5': return 5;
+	case '6': return 6;
+	case '7': return 7;
+	case '8': return 8;
+	case '9': return 9;
+	case 'a': return 10;
+	case 'b': return 11;
+	case 'c': return 12;
+	case 'd': return 13;
+	case 'e': return 14;
+	case 'f': return 15;
+	case 'A': return 10;
+	case 'B': return 11;
+	case 'C': return 12;
+	case 'D': return 13;
+	case 'E': return 14;
+	case 'F': return 15;
 	}
 	return 16; // NOTE(bill): Larger than highest possible
 }

+ 1 - 1
src/docs_writer.cpp

@@ -257,7 +257,7 @@ OdinDocArray<T> odin_write_item_as_slice(OdinDocWriter *w, T data) {
 OdinDocPosition odin_doc_token_pos_cast(OdinDocWriter *w, TokenPos const &pos) {
 	OdinDocFileIndex file_index = 0;
 	if (pos.file_id != 0) {
-		AstFile *file = get_ast_file_from_id(pos.file_id);
+		AstFile *file = global_files[pos.file_id];
 		if (file != nullptr) {
 			OdinDocFileIndex *file_index_found = map_get(&w->file_cache, file);
 			GB_ASSERT(file_index_found != nullptr);

+ 6 - 6
src/parser.cpp

@@ -11,7 +11,7 @@ Token token_end_of_line(AstFile *f, Token tok) {
 }
 
 gbString get_file_line_as_string(TokenPos const &pos, i32 *offset_) {
-	AstFile *file = get_ast_file_from_id(pos.file_id);
+	AstFile *file = thread_safe_get_ast_file_from_id(pos.file_id);
 	if (file == nullptr) {
 		return nullptr;
 	}
@@ -95,7 +95,7 @@ Ast *clone_ast(Ast *node) {
 	if (node == nullptr) {
 		return nullptr;
 	}
-	AstFile *f = get_ast_file_from_id(node->file_id);
+	AstFile *f = node->thread_safe_file();
 	Ast *n = alloc_ast_node(f, node->kind);
 	gb_memmove(n, node, ast_node_size(node->kind));
 
@@ -401,7 +401,7 @@ void error(Ast *node, char const *fmt, ...) {
 	error_va(token.pos, end_pos, fmt, va);
 	va_end(va);
 	if (node != nullptr && node->file_id != 0) {
-		AstFile *f = get_ast_file_from_id(node->file_id);
+		AstFile *f = node->thread_safe_file();
 		f->error_count += 1;
 	}
 }
@@ -416,7 +416,7 @@ void error_no_newline(Ast *node, char const *fmt, ...) {
 	error_no_newline_va(token.pos, fmt, va);
 	va_end(va);
 	if (node != nullptr && node->file_id != 0) {
-		AstFile *f = get_ast_file_from_id(node->file_id);
+		AstFile *f = node->thread_safe_file();
 		f->error_count += 1;
 	}
 }
@@ -446,7 +446,7 @@ void syntax_error(Ast *node, char const *fmt, ...) {
 	syntax_error_va(token.pos, end_pos, fmt, va);
 	va_end(va);
 	if (node != nullptr && node->file_id != 0) {
-		AstFile *f = get_ast_file_from_id(node->file_id);
+		AstFile *f = node->thread_safe_file();
 		f->error_count += 1;
 	}
 }
@@ -4669,7 +4669,7 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) {
 	GB_ASSERT(f != nullptr);
 	f->fullpath = string_trim_whitespace(fullpath); // Just in case
 	set_file_path_string(f->id, fullpath);
-	set_ast_file_from_id(f->id, f);
+	thread_safe_set_ast_file_from_id(f->id, f);
 	if (!string_ends_with(f->fullpath, str_lit(".odin"))) {
 		return ParseFile_WrongExtension;
 	}

+ 6 - 2
src/parser.hpp

@@ -731,8 +731,12 @@ struct Ast {
 	
 	// NOTE(bill): I know I dislike methods but this is hopefully a temporary thing 
 	// for refactoring purposes
-	AstFile *file() const {
-		return get_ast_file_from_id(this->file_id);
+	gb_inline AstFile *file() const {
+		// NOTE(bill): This doesn't need to call get_ast_file_from_id which 
+		return global_files[this->file_id];
+	}
+	gb_inline AstFile *thread_safe_file() const {
+		return thread_safe_get_ast_file_from_id(this->file_id);
 	}
 };
 

+ 3 - 1
src/ptr_set.cpp

@@ -24,7 +24,9 @@ template <typename T> void ptr_set_reserve(PtrSet<T> *h, isize cap);
 
 template <typename T>
 void ptr_set_init(PtrSet<T> *s, gbAllocator a, isize capacity) {
-	capacity = next_pow2_isize(gb_max(16, capacity));
+	if (capacity != 0) {
+		capacity = next_pow2_isize(gb_max(16, capacity));
+	}
 
 	slice_init(&s->hashes,  a, capacity);
 	array_init(&s->entries, a, 0, capacity);

+ 1 - 1
src/thread_pool.cpp

@@ -95,7 +95,7 @@ bool thread_pool_add_task(ThreadPool *pool, WorkerTaskProc *proc, void *data) {
 	thread_pool_queue_push(pool, task);
 	GB_ASSERT(pool->ready >= 0);
 	pool->ready++;
-	condition_signal(&pool->task_cond);
+	condition_broadcast(&pool->task_cond);
 	mutex_unlock(&pool->mutex);
 	return true;
 }	

+ 3 - 3
src/tokenizer.cpp

@@ -192,7 +192,7 @@ gb_global Array<String>           global_file_path_strings; // index is file id
 gb_global Array<struct AstFile *> global_files; // index is file id
 
 String   get_file_path_string(i32 index);
-struct AstFile *get_ast_file_from_id(i32 index);
+struct AstFile *thread_safe_get_ast_file_from_id(i32 index);
 
 struct TokenPos {
 	i32 file_id;
@@ -318,7 +318,7 @@ bool set_file_path_string(i32 index, String const &path) {
 	return ok;
 }
 
-bool set_ast_file_from_id(i32 index, AstFile *file) {
+bool thread_safe_set_ast_file_from_id(i32 index, AstFile *file) {
 	bool ok = false;
 	GB_ASSERT(index >= 0);
 	mutex_lock(&global_error_collector.string_mutex);
@@ -349,7 +349,7 @@ String get_file_path_string(i32 index) {
 	return path;
 }
 
-AstFile *get_ast_file_from_id(i32 index) {
+AstFile *thread_safe_get_ast_file_from_id(i32 index) {
 	GB_ASSERT(index >= 0);
 	mutex_lock(&global_error_collector.string_mutex);