From e6b1a68600026c188631a040c12e9c27d0e87a00 Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 21:30:46 +0000 Subject: [PATCH] perf(mutex): Optimize CriticalSectionClass by inlining CRITICAL_SECTION storage --- Core/Libraries/Source/WWVegas/WWLib/mutex.cpp | 23 +++---------------- Core/Libraries/Source/WWVegas/WWLib/mutex.h | 5 +++- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/Core/Libraries/Source/WWVegas/WWLib/mutex.cpp b/Core/Libraries/Source/WWVegas/WWLib/mutex.cpp index d6cd1ec6d50..d3fd64e2591 100644 --- a/Core/Libraries/Source/WWVegas/WWLib/mutex.cpp +++ b/Core/Libraries/Source/WWVegas/WWLib/mutex.cpp @@ -90,26 +90,12 @@ MutexClass::LockClass::~LockClass() // ---------------------------------------------------------------------------- -CriticalSectionClass::CriticalSectionClass() : handle(nullptr), locked(false) +CriticalSectionClass::CriticalSectionClass() : locked(false) { #ifdef _UNIX //assert(0); #else - // Allocate with proper alignment for CRITICAL_SECTION - // On 64-bit: requires 8-byte alignment, on 32-bit: requires 4-byte alignment - #if defined(_WIN64) || defined(__LP64__) - size_t alignment = 8; - #else - size_t alignment = 4; - #endif - - handle = _aligned_malloc(sizeof(CRITICAL_SECTION), alignment); - if (handle != nullptr) { - InitializeCriticalSection((CRITICAL_SECTION*)handle); - } - else { - WWASSERT(false); // Allocation failed - } + InitializeCriticalSection((CRITICAL_SECTION*)handle); #endif } @@ -119,10 +105,7 @@ CriticalSectionClass::~CriticalSectionClass() //assert(0); #else WWASSERT(!locked); // Can't delete locked critical section! - if (handle != nullptr) { - DeleteCriticalSection((CRITICAL_SECTION*)handle); - _aligned_free(handle); - } + DeleteCriticalSection((CRITICAL_SECTION*)handle); #endif } diff --git a/Core/Libraries/Source/WWVegas/WWLib/mutex.h b/Core/Libraries/Source/WWVegas/WWLib/mutex.h index a9c106ce196..d42b0c3148c 100644 --- a/Core/Libraries/Source/WWVegas/WWLib/mutex.h +++ b/Core/Libraries/Source/WWVegas/WWLib/mutex.h @@ -81,7 +81,10 @@ class MutexClass class CriticalSectionClass { - void* handle; + // Inline storage for CRITICAL_SECTION to avoid heap allocation entirely. + // CRITICAL_SECTION is 40 bytes on x64 and 24 bytes on x86; 40 bytes with + // 8-byte alignment is sufficient for both platforms. + alignas(8) char handle[40]; unsigned locked; // Lock and unlock are private so that you can't use them directly. Use LockClass as a sentry instead!