aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-11-15 17:26:01 +0000
committergingerBill <bill@gingerbill.org>2021-11-15 17:26:01 +0000
commite814a3693f9efbdd00113de4dd5937acd97bc486 (patch)
treeff0534f9609fb8b5726c0933027db9963bb469e4 /src
parentf55fc4cd08dba25730fa1c74d31611cf27ef2155 (diff)
Improve usage of `file_id`
Diffstat (limited to 'src')
-rw-r--r--src/check_builtin.cpp4
-rw-r--r--src/checker.cpp4
-rw-r--r--src/common.cpp63
-rw-r--r--src/docs_writer.cpp2
-rw-r--r--src/parser.cpp12
-rw-r--r--src/parser.hpp8
-rw-r--r--src/ptr_set.cpp4
-rw-r--r--src/thread_pool.cpp2
-rw-r--r--src/tokenizer.cpp6
9 files changed, 75 insertions, 30 deletions
diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp
index d910314fb..f93cf9886 100644
--- a/src/check_builtin.cpp
+++ b/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") {
diff --git a/src/checker.cpp b/src/checker.cpp
index 3caed256a..fa74c23ed 100644
--- a/src/checker.cpp
+++ b/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) {
diff --git a/src/common.cpp b/src/common.cpp
index cca478421..ab2a46118 100644
--- a/src/common.cpp
+++ b/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
}
diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp
index e7254e16b..430e26782 100644
--- a/src/docs_writer.cpp
+++ b/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);
diff --git a/src/parser.cpp b/src/parser.cpp
index ba5df1f27..cbd4d61d5 100644
--- a/src/parser.cpp
+++ b/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;
}
diff --git a/src/parser.hpp b/src/parser.hpp
index b1518533e..b83822cbf 100644
--- a/src/parser.hpp
+++ b/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);
}
};
diff --git a/src/ptr_set.cpp b/src/ptr_set.cpp
index 8dd3cb4dc..ca7df3b53 100644
--- a/src/ptr_set.cpp
+++ b/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);
diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp
index fc65110fe..727cdcdda 100644
--- a/src/thread_pool.cpp
+++ b/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;
}
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
index 10b4494d7..b7ade9d89 100644
--- a/src/tokenizer.cpp
+++ b/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);