From 9ecfa08dc846605d61ebed461d8ff3d986ba49a3 Mon Sep 17 00:00:00 2001 From: Aidan Lee Date: Thu, 12 Sep 2024 17:42:05 +0100 Subject: [PATCH] eagerly free large objects --- include/hx/GC.h | 1 + src/hx/Telemetry.cpp | 1 + src/hx/TelemetryTracy.cpp | 26 +++++++++++++++++--------- src/hx/gc/Immix.cpp | 4 ++++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/hx/GC.h b/include/hx/GC.h index 76b550101..ec11690b3 100644 --- a/include/hx/GC.h +++ b/include/hx/GC.h @@ -24,6 +24,7 @@ #ifdef HXCPP_TELEMETRY extern void __hxt_gc_new(hx::StackContext *inStack, void* obj, int inSize, const char *inName); extern void __hxt_gc_alloc(void* obj, int inSize); +extern void __hxt_gc_free_large(void* obj); #endif diff --git a/src/hx/Telemetry.cpp b/src/hx/Telemetry.cpp index 6866bb841..593299a4c 100644 --- a/src/hx/Telemetry.cpp +++ b/src/hx/Telemetry.cpp @@ -650,6 +650,7 @@ void __hxt_new_hash(void* obj, int inSize) #endif } void __hxt_gc_alloc(void* obj, int inSize) { } +void __hxt_gc_free_large(void*) {} void __hxt_gc_new(hx::StackContext *stack, void* obj, int inSize, const char* name) { #ifdef HXCPP_STACK_TRACE diff --git a/src/hx/TelemetryTracy.cpp b/src/hx/TelemetryTracy.cpp index 5a3952a2f..62b0005c6 100644 --- a/src/hx/TelemetryTracy.cpp +++ b/src/hx/TelemetryTracy.cpp @@ -82,15 +82,6 @@ void __hxt_gc_alloc(void* obj, int inSize) { AutoLock lock(largeAllocsLock); - // Skip multiple large object allocations since they can be recycled in-between GC collections. - for (auto i = 0; i < largeAllocs.size(); i++) - { - if (largeAllocs[i] == obj) - { - return; - } - } - largeAllocs.push(obj); TracyAllocN(obj, inSize, lohName); @@ -104,6 +95,23 @@ void __hxt_gc_alloc(void* obj, int inSize) #endif } +void __hxt_gc_free_large(void* obj) +{ + AutoLock lock(largeAllocsLock); + + for (auto i = 0; i < largeAllocs.size(); i++) + { + if (largeAllocs[i] == obj) + { + largeAllocs.erase(i); + + TracyFreeN(obj, lohName); + + return; + } + } +} + void __hxt_gc_realloc(void* oldObj, void* newObj, int newSize) { } void __hxt_gc_after_mark(int byteMarkId, int endianByteMarkId) diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index f740f01ea..ee8fcde19 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -3179,6 +3179,10 @@ class GlobalAllocator void FreeLarge(void *inLarge) { +#ifdef HXCPP_TELEMETRY + __hxt_gc_free_large(inLarge); +#endif + ((unsigned char *)inLarge)[HX_ENDIAN_MARK_ID_BYTE] = 0; // AllocLarge will not lock this list unless it decides there is a suitable // value, so we can't doa realloc without potentially crashing it.