Skip to content

Commit e3cb09e

Browse files
authored
Merge pull request #498 from GeneralsOnlineDevelopmentTeam/seer/perf/inline-critical-section
perf(mutex): Optimize CriticalSectionClass by inlining CRITICAL_SECTION storage
2 parents 58016e8 + e6b1a68 commit e3cb09e

2 files changed

Lines changed: 7 additions & 21 deletions

File tree

Core/Libraries/Source/WWVegas/WWLib/mutex.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,12 @@ MutexClass::LockClass::~LockClass()
9090

9191
// ----------------------------------------------------------------------------
9292

93-
CriticalSectionClass::CriticalSectionClass() : handle(nullptr), locked(false)
93+
CriticalSectionClass::CriticalSectionClass() : locked(false)
9494
{
9595
#ifdef _UNIX
9696
//assert(0);
9797
#else
98-
// Allocate with proper alignment for CRITICAL_SECTION
99-
// On 64-bit: requires 8-byte alignment, on 32-bit: requires 4-byte alignment
100-
#if defined(_WIN64) || defined(__LP64__)
101-
size_t alignment = 8;
102-
#else
103-
size_t alignment = 4;
104-
#endif
105-
106-
handle = _aligned_malloc(sizeof(CRITICAL_SECTION), alignment);
107-
if (handle != nullptr) {
108-
InitializeCriticalSection((CRITICAL_SECTION*)handle);
109-
}
110-
else {
111-
WWASSERT(false); // Allocation failed
112-
}
98+
InitializeCriticalSection((CRITICAL_SECTION*)handle);
11399
#endif
114100
}
115101

@@ -119,10 +105,7 @@ CriticalSectionClass::~CriticalSectionClass()
119105
//assert(0);
120106
#else
121107
WWASSERT(!locked); // Can't delete locked critical section!
122-
if (handle != nullptr) {
123-
DeleteCriticalSection((CRITICAL_SECTION*)handle);
124-
_aligned_free(handle);
125-
}
108+
DeleteCriticalSection((CRITICAL_SECTION*)handle);
126109
#endif
127110
}
128111

Core/Libraries/Source/WWVegas/WWLib/mutex.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ class MutexClass
8181

8282
class CriticalSectionClass
8383
{
84-
void* handle;
84+
// Inline storage for CRITICAL_SECTION to avoid heap allocation entirely.
85+
// CRITICAL_SECTION is 40 bytes on x64 and 24 bytes on x86; 40 bytes with
86+
// 8-byte alignment is sufficient for both platforms.
87+
alignas(8) char handle[40];
8588
unsigned locked;
8689

8790
// Lock and unlock are private so that you can't use them directly. Use LockClass as a sentry instead!

0 commit comments

Comments
 (0)