aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-02-18 21:30:25 +0000
committergingerBill <bill@gingerbill.org>2022-02-18 21:30:25 +0000
commit71df46456a4db2592e8cebbdd4c46dc8b58b5a24 (patch)
tree4152775ac24721cd026025d1e478d2eb9a254b41 /src
parentcd89d8a3c414a6929ec058a04aeec568d99505a5 (diff)
Minimize memory usage by having an arena per thread rather than an arena per file
Diffstat (limited to 'src')
-rw-r--r--src/array.cpp4
-rw-r--r--src/checker.cpp4
-rw-r--r--src/common.cpp2
-rw-r--r--src/main.cpp2
-rw-r--r--src/parser.cpp12
-rw-r--r--src/parser.hpp6
6 files changed, 15 insertions, 15 deletions
diff --git a/src/array.cpp b/src/array.cpp
index ac3727978..d08bd647f 100644
--- a/src/array.cpp
+++ b/src/array.cpp
@@ -89,7 +89,9 @@ template <typename T>
void slice_init(Slice<T> *s, gbAllocator const &allocator, isize count) {
GB_ASSERT(count >= 0);
s->data = gb_alloc_array(allocator, T, count);
- GB_ASSERT(s->data != nullptr);
+ if (count > 0) {
+ GB_ASSERT(s->data != nullptr);
+ }
s->count = count;
}
diff --git a/src/checker.cpp b/src/checker.cpp
index f8aa8d45b..f440b7c9a 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, permanent_allocator(), init_elements_capacity);
- ptr_set_init(&s->imported, permanent_allocator(), 0);
+ string_map_init(&s->elements, heap_allocator(), init_elements_capacity);
+ ptr_set_init(&s->imported, heap_allocator(), 0);
mutex_init(&s->mutex);
if (parent != nullptr && parent != builtin_pkg->scope) {
diff --git a/src/common.cpp b/src/common.cpp
index ab2a46118..d3ee95b76 100644
--- a/src/common.cpp
+++ b/src/common.cpp
@@ -1021,7 +1021,7 @@ LoadedFileError load_file_32(char const *fullpath, LoadedFile *memory_mapped_fil
#endif
}
- gbFileContents fc = gb_file_read_contents(heap_allocator(), true, fullpath);
+ gbFileContents fc = gb_file_read_contents(permanent_allocator(), true, fullpath);
if (fc.size > I32_MAX) {
err = LoadedFile_FileTooLarge;
diff --git a/src/main.cpp b/src/main.cpp
index 014fbf822..291b56996 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2577,7 +2577,7 @@ int main(int arg_count, char const **arg_ptr) {
// NOTE(bill): add 'shared' directory if it is not already set
if (!find_library_collection_path(str_lit("shared"), nullptr)) {
add_library_collection(str_lit("shared"),
- get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("shared")));
+ get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("shared")));
}
diff --git a/src/parser.cpp b/src/parser.cpp
index 7309d9769..8a7ab2d20 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -57,6 +57,9 @@ isize ast_node_size(AstKind kind) {
return align_formula_isize(gb_size_of(AstCommonStuff) + ast_variant_sizes[kind], gb_align_of(void *));
}
+
+gb_global std::atomic<isize> global_total_node_memory_allocated;
+
// NOTE(bill): And this below is why is I/we need a new language! Discriminated unions are a pain in C/C++
Ast *alloc_ast_node(AstFile *f, AstKind kind) {
gbAllocator a = ast_allocator(f);
@@ -66,6 +69,9 @@ Ast *alloc_ast_node(AstFile *f, AstKind kind) {
Ast *node = cast(Ast *)gb_alloc(a, size);
node->kind = kind;
node->file_id = f ? f->id : 0;
+
+ global_total_node_memory_allocated += size;
+
return node;
}
@@ -4851,12 +4857,6 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) {
f->prev_token = f->tokens[f->prev_token_index];
f->curr_token = f->tokens[f->curr_token_index];
- isize const page_size = 4*1024;
- isize block_size = 2*f->tokens.count*gb_size_of(Ast);
- block_size = ((block_size + page_size-1)/page_size) * page_size;
- block_size = gb_clamp(block_size, page_size, DEFAULT_MINIMUM_BLOCK_SIZE);
- f->arena.minimum_block_size = block_size;
-
array_init(&f->comments, heap_allocator(), 0, 0);
array_init(&f->imports, heap_allocator(), 0, 0);
diff --git a/src/parser.hpp b/src/parser.hpp
index 83c755553..f6791a291 100644
--- a/src/parser.hpp
+++ b/src/parser.hpp
@@ -97,8 +97,6 @@ struct AstFile {
AstPackage * pkg;
Scope * scope;
- Arena arena;
-
Ast * pkg_decl;
String fullpath;
Tokenizer tokenizer;
@@ -801,10 +799,10 @@ gb_inline bool is_ast_when_stmt(Ast *node) {
return node->kind == Ast_WhenStmt;
}
-gb_global gb_thread_local Arena global_ast_arena = {};
+gb_global gb_thread_local Arena global_thread_local_ast_arena = {};
gbAllocator ast_allocator(AstFile *f) {
- Arena *arena = f ? &f->arena : &global_ast_arena;
+ Arena *arena = &global_thread_local_ast_arena;
return arena_allocator(arena);
}