From 1f26ff9fc3268d8099b449866aa66f7424ca3389 Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Mon, 1 Dec 2025 15:38:25 -0800 Subject: [PATCH 1/3] ability to tell, how many instances of context exist --- include/daScript/simulate/aot_builtin.h | 1 + src/builtin/module_builtin_runtime.cpp | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/include/daScript/simulate/aot_builtin.h b/include/daScript/simulate/aot_builtin.h index add2c96ba9..7275babd2f 100644 --- a/include/daScript/simulate/aot_builtin.h +++ b/include/daScript/simulate/aot_builtin.h @@ -13,6 +13,7 @@ namespace das { DAS_API bool is_compiling ( ); DAS_API bool is_compiling_macros ( ); + DAS_API uint64_t get_context_share_counter ( Context * context ); DAS_API char * builtin_das_root ( Context * context, LineInfoArg * at ); DAS_API void builtin_throw ( char * text, Context * context, LineInfoArg * at ); diff --git a/src/builtin/module_builtin_runtime.cpp b/src/builtin/module_builtin_runtime.cpp index 14343f6b91..8bd4b52780 100644 --- a/src/builtin/module_builtin_runtime.cpp +++ b/src/builtin/module_builtin_runtime.cpp @@ -1258,6 +1258,10 @@ namespace das } + DAS_API uint64_t get_context_share_counter ( Context * context ) { + return (uint64_t) context->code.use_count(); + } + bool is_compiling_macros ( ) { if ( daScriptEnvironment::getBound() && daScriptEnvironment::getBound()->g_Program ) { return daScriptEnvironment::getBound()->g_Program->isCompilingMacros; @@ -1662,6 +1666,9 @@ namespace das ->args({"name"}); addExtern(*this, lib, "is_reporting_compilation_errors", SideEffects::accessExternal, "is_reporting_compilation_errors"); + addExtern(*this, lib, "get_context_share_counter", + SideEffects::accessExternal, "get_context_share_counter") + ->arg("context"); // iterator functions addExtern(*this, lib, "_builtin_iterator_first", SideEffects::modifyArgumentAndExternal, "builtin_iterator_first") From f66f440b66a1950d1d7ddb70c6ca26548f8fdf2f Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Mon, 1 Dec 2025 16:02:56 -0800 Subject: [PATCH 2/3] collecting indicator --- include/daScript/simulate/data_walker.h | 1 + src/simulate/simulate_gc.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/daScript/simulate/data_walker.h b/include/daScript/simulate/data_walker.h index b8ffb5804e..4e75de40f1 100644 --- a/include/daScript/simulate/data_walker.h +++ b/include/daScript/simulate/data_walker.h @@ -26,6 +26,7 @@ namespace das { struct DAS_API DataWalker : ptr_ref_count { // we doing what? class Context * context = nullptr; + bool collecting = false; bool reading = false; bool _cancel = false; // helpers diff --git a/src/simulate/simulate_gc.cpp b/src/simulate/simulate_gc.cpp index 5caedf7a50..2392136455 100644 --- a/src/simulate/simulate_gc.cpp +++ b/src/simulate/simulate_gc.cpp @@ -29,6 +29,10 @@ namespace das int32_t gcFlags = TypeInfo::flag_stringHeapGC | TypeInfo::flag_heapGC; int32_t gcStructFlags = StructInfo::flag_stringHeapGC | StructInfo::flag_heapGC; + BaseGcDataWalker() { + collecting = true; + reading = false; + } virtual bool canVisitStructure ( char * /*ps*/, StructInfo * info ) override { if ( !(info->flags & gcStructFlags) ) return false; return true; From 4fd33ab8782dea32282a0da1be0839a98f9c6b7d Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Mon, 1 Dec 2025 16:34:43 -0800 Subject: [PATCH 3/3] extra type info for handled type walking --- include/daScript/ast/ast_handle.h | 1 + src/ast/ast_handle.cpp | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/include/daScript/ast/ast_handle.h b/include/daScript/ast/ast_handle.h index a058bf25ef..b3465b72ce 100644 --- a/include/daScript/ast/ast_handle.h +++ b/include/daScript/ast/ast_handle.h @@ -111,6 +111,7 @@ namespace das vector fieldsInOrder; mutable DebugInfoHelper helpA; mutable StructInfo * sti = nullptr; + mutable StructInfo * sti_gc = nullptr; ModuleLibrary * mlib = nullptr; vector parents; bool validationNeverFails = false; diff --git a/src/ast/ast_handle.cpp b/src/ast/ast_handle.cpp index 62a2be1f7e..26811a42da 100644 --- a/src/ast/ast_handle.cpp +++ b/src/ast/ast_handle.cpp @@ -171,8 +171,11 @@ namespace das { auto debugInfo = helpA.debugInfo; sti = debugInfo->template makeNode(); sti->name = debugInfo->allocateName(name); + sti_gc = debugInfo->template makeNode(); + sti_gc->name = sti->name; // flags sti->flags = 0; + sti_gc->flags = 0; // count fields sti->count = 0; for ( auto & fi : fields ) { @@ -182,7 +185,9 @@ namespace das { } } // and allocate + sti_gc->count = 0; sti->size = (uint32_t) getSizeOf(); + sti_gc->size = sti->size; sti->fields = (VarInfo **) debugInfo->allocate( sizeof(VarInfo *) * sti->count ); int i = 0; for ( const auto & fn : fieldsInOrder ) { @@ -195,17 +200,37 @@ namespace das { vi->name = debugInfo->allocateName(fn); vi->offset = var.offset; sti->fields[i++] = vi; + if ( vi->flags & (TypeInfo::flag_heapGC | TypeInfo::flag_stringHeapGC) ) { + sti_gc->count++; + } } } } + if ( sti_gc->count ) { + sti_gc->fields = (VarInfo **) debugInfo->allocate( sizeof(VarInfo *) * sti_gc->count ); + int j = 0; + for ( uint32_t n=0; n!=sti->count; ++n ) { + auto & fi = sti->fields[n]; + if ( fi->flags & (TypeInfo::flag_heapGC | TypeInfo::flag_stringHeapGC) ) { + sti_gc->fields[j++] = fi; + } + } + } else { + sti_gc->fields = nullptr; + } sti->module_name = debugInfo->allocateCachedName(this->module->name); } - } void BasicStructureAnnotation::walk ( DataWalker & walker, void * data ) { updateTypeInfo(); - walker.walk_struct((char *)data, sti); + if ( walker.collecting ) { + if ( sti_gc->count ) { + walker.walk_struct((char *)data, sti_gc); + } + } else { + walker.walk_struct((char *)data, sti); + } } void BasicStructureAnnotation::from ( BasicStructureAnnotation * ann ) {