diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-06-27 01:20:37 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-27 01:20:37 +0200 |
| commit | 2e83e221419b480160dbf11dd09edf8b5fa97f32 (patch) | |
| tree | bc5e666006bf43931bc793ac921061b5630db4d4 /src/docs_writer.cpp | |
| parent | c51df72f1a8a21dd0064086dfc1186a3dd79e2ef (diff) | |
| parent | f72b2b153057e1d629c85af2ea7c54f7928198d5 (diff) | |
Merge branch 'master' into args-leak
Diffstat (limited to 'src/docs_writer.cpp')
| -rw-r--r-- | src/docs_writer.cpp | 85 |
1 files changed, 32 insertions, 53 deletions
diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 341b3fa6b..1f2325980 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -16,6 +16,8 @@ gb_global char const* OdinDocWriterState_strings[] { "writing ", }; +gb_global std::atomic<bool> g_in_doc_writer; + struct OdinDocWriter { CheckerInfo *info; OdinDocWriterState state; @@ -26,11 +28,10 @@ struct OdinDocWriter { StringMap<OdinDocString> string_cache; - OrderedInsertPtrMap<AstFile *, OdinDocFileIndex> file_cache; - OrderedInsertPtrMap<AstPackage *, OdinDocPkgIndex> pkg_cache; - OrderedInsertPtrMap<Entity *, OdinDocEntityIndex> entity_cache; - OrderedInsertPtrMap<Type *, OdinDocTypeIndex> type_cache; - OrderedInsertPtrMap<Type *, Type *> stable_type_cache; + OrderedInsertPtrMap<AstFile *, OdinDocFileIndex> file_cache; + OrderedInsertPtrMap<AstPackage *, OdinDocPkgIndex> pkg_cache; + OrderedInsertPtrMap<Entity *, OdinDocEntityIndex> entity_cache; + OrderedInsertPtrMap<u64/*type hash*/, OdinDocTypeIndex> type_cache; OdinDocWriterItemTracker<OdinDocFile> files; OdinDocWriterItemTracker<OdinDocPkg> pkgs; @@ -42,7 +43,7 @@ struct OdinDocWriter { }; gb_internal OdinDocEntityIndex odin_doc_add_entity(OdinDocWriter *w, Entity *e); -gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type); +gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type, bool cache); template <typename T> gb_internal void odin_doc_writer_item_tracker_init(OdinDocWriterItemTracker<T> *t, isize size) { @@ -61,7 +62,6 @@ gb_internal void odin_doc_writer_prepare(OdinDocWriter *w) { map_init(&w->pkg_cache, 1<<10); map_init(&w->entity_cache, 1<<18); map_init(&w->type_cache, 1<<18); - map_init(&w->stable_type_cache, 1<<18); odin_doc_writer_item_tracker_init(&w->files, 1); odin_doc_writer_item_tracker_init(&w->pkgs, 1); @@ -81,7 +81,6 @@ gb_internal void odin_doc_writer_destroy(OdinDocWriter *w) { map_destroy(&w->pkg_cache); map_destroy(&w->entity_cache); map_destroy(&w->type_cache); - map_destroy(&w->stable_type_cache); } @@ -468,8 +467,8 @@ gb_internal OdinDocArray<OdinDocString> odin_doc_where_clauses(OdinDocWriter *w, return odin_write_slice(w, clauses.data, clauses.count); } -gb_internal OdinDocArray<OdinDocTypeIndex> odin_doc_type_as_slice(OdinDocWriter *w, Type *type) { - OdinDocTypeIndex index = odin_doc_type(w, type); +gb_internal OdinDocArray<OdinDocTypeIndex> odin_doc_type_as_slice(OdinDocWriter *w, Type *type, bool cache=true) { + OdinDocTypeIndex index = odin_doc_type(w, type, cache); return odin_write_item_as_slice(w, index); } @@ -480,7 +479,7 @@ gb_internal OdinDocArray<OdinDocEntityIndex> odin_doc_add_entity_as_slice(OdinDo -gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { +gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type, bool cache=true) { if (type == nullptr) { return 0; } @@ -492,46 +491,13 @@ gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { } } - // Type **mapped_type = map_get(&w->stable_type_cache, type); // may map to itself - // if (mapped_type && *mapped_type) { - // type = *mapped_type; - // } - - OdinDocTypeIndex *found = map_get(&w->type_cache, type); - if (found) { - return *found; - } - for (auto const &entry : w->type_cache) { - // NOTE(bill): THIS IS SLOW - Type *x = type; - Type *y = entry.key; - - if (x == y) { - goto do_set; - } - - if (!x | !y) { - continue; - } - if (y->kind == Type_Named) { - Entity *e = y->Named.type_name; - if (e->TypeName.is_type_alias) { - y = y->Named.base; - } - } - if (x->kind != y->kind) { - continue; + u64 type_hash = {0}; + if (cache) { + type_hash = type_hash_canonical_type(type); + OdinDocTypeIndex *found = map_get(&w->type_cache, type_hash); + if (found) { + return *found; } - - if (!are_types_identical_internal(x, y, true)) { - continue; - } - - do_set: - OdinDocTypeIndex index = entry.value; - map_set(&w->type_cache, type, index); - map_set(&w->stable_type_cache, type, entry.key); - return index; } @@ -539,8 +505,9 @@ gb_internal 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, type, type_index); - map_set(&w->stable_type_cache, type, type); + if (cache) { + map_set(&w->type_cache, type_hash, type_index); + } switch (type->kind) { case Type_Basic: @@ -565,7 +532,10 @@ gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { doc_type.kind = OdinDocType_Generic; doc_type.name = odin_doc_write_string(w, name); if (type->Generic.specialized) { - doc_type.types = odin_doc_type_as_slice(w, type->Generic.specialized); + // NOTE(laytan): do not look at the cache for the specialization, it would resolve + // to the same entry as the type itself because `default_type` resolves to the + // specialization of a generic type. + doc_type.types = odin_doc_type_as_slice(w, type->Generic.specialized, cache=false); } } break; @@ -1177,6 +1147,8 @@ gb_internal void odin_doc_write_to_file(OdinDocWriter *w, char const *filename) } gb_internal void odin_doc_write(CheckerInfo *info, char const *filename) { + g_in_doc_writer.store(true); + OdinDocWriter w_ = {}; OdinDocWriter *w = &w_; defer (odin_doc_writer_destroy(w)); @@ -1192,4 +1164,11 @@ gb_internal void odin_doc_write(CheckerInfo *info, char const *filename) { odin_doc_writer_end_writing(w); odin_doc_write_to_file(w, filename); + + g_in_doc_writer.store(false); +} + + +gb_internal bool is_in_doc_writer(void) { + return g_in_doc_writer.load(); } |