From 28d17f00015e1193c46a1ac5029b8f87116ad159 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 15 Jun 2026 11:44:00 +0100 Subject: [PATCH 1/2] doc writer: Use string interning and minimize the size of the compound literal generation when it is HUGE --- src/docs_writer.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index e0b0a05c1..6b700a3bf 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -370,7 +370,7 @@ gb_internal OdinDocString odin_doc_pkg_doc_string(OdinDocWriter *w, AstPackage * if (pkg == nullptr) { return {}; } - auto buf = array_make(permanent_allocator(), 0, 0); // Minor leak + auto buf = array_make(heap_allocator(), 0, 0); for_array(i, pkg->files) { AstFile *f = pkg->files[i]; @@ -380,31 +380,37 @@ gb_internal OdinDocString odin_doc_pkg_doc_string(OdinDocWriter *w, AstPackage * } } - return odin_doc_write_string_without_cache(w, make_string(buf.data, buf.count)); + String str = string_intern_string(make_string(buf.data, buf.count)); + array_free(&buf); + return odin_doc_write_string_without_cache(w, str); } gb_internal OdinDocString odin_doc_comment_group_string(OdinDocWriter *w, CommentGroup *g) { if (g == nullptr) { return {}; } - auto buf = array_make(permanent_allocator(), 0, 0); // Minor leak + auto buf = array_make(heap_allocator(), 0, 0); odin_doc_append_comment_group_string(&buf, g); - return odin_doc_write_string_without_cache(w, make_string(buf.data, buf.count)); + String str = string_intern_string(make_string(buf.data, buf.count)); + array_free(&buf); + return odin_doc_write_string_without_cache(w, str); } -gb_internal OdinDocString odin_doc_expr_string(OdinDocWriter *w, Ast *expr) { +gb_internal OdinDocString odin_doc_expr_string(OdinDocWriter *w, Ast *expr, bool use_shorthand=false) { if (expr == nullptr) { return {}; } - gbString s = write_expr_to_string( // Minor leak - gb_string_make(permanent_allocator(), ""), + gbString s = write_expr_to_string( + gb_string_make(heap_allocator(), ""), expr, - build_context.cmd_doc_flags & CmdDocFlag_Short + use_shorthand || (build_context.cmd_doc_flags & CmdDocFlag_Short) ); + String str = string_intern_string(make_string(cast(u8 *)s, gb_string_length(s))); + gb_string_free(s); - return odin_doc_write_string(w, make_string(cast(u8 *)s, gb_string_length(s))); + return odin_doc_write_string(w, str); } gb_internal OdinDocArray odin_doc_attributes(OdinDocWriter *w, Array const &attributes) { @@ -920,7 +926,16 @@ gb_internal OdinDocEntityIndex odin_doc_add_entity(OdinDocWriter *w, Entity *e) OdinDocString init_string = {}; if (init_expr) { - init_string = odin_doc_expr_string(w, init_expr); + bool use_shorthand = false; + if (e->kind == Entity_Variable) { + Ast *expr = init_expr; + if (expr->kind == Ast_CompoundLit) { + if (expr->CompoundLit.elems.count > 512) { + use_shorthand = true; + } + } + } + init_string = odin_doc_expr_string(w, init_expr, use_shorthand); } else { if (e->kind == Entity_Constant) { if (e->Constant.flags & EntityConstantFlag_ImplicitEnumValue) { From 829dd30b0bfaede9733036b435690252e9eea422 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 15 Jun 2026 11:46:26 +0100 Subject: [PATCH 2/2] doc_writer: String intern constant values expressions --- src/docs_writer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 6b700a3bf..7a12d0875 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -943,7 +943,10 @@ gb_internal OdinDocEntityIndex odin_doc_add_entity(OdinDocWriter *w, Entity *e) } else if (e->Constant.param_value.original_ast_expr) { init_string = odin_doc_expr_string(w, e->Constant.param_value.original_ast_expr); } else { - init_string = odin_doc_write_string(w, make_string_c(exact_value_to_string(e->Constant.value))); + gbString s = exact_value_to_string(e->Constant.value); + String str = string_intern_string(make_string(cast(u8 *)s, gb_string_length(s))); + gb_string_free(s); + init_string = odin_doc_write_string(w, str); } } else if (e->kind == Entity_Variable) { if (e->Variable.param_value.original_ast_expr) {