Skip to content

Commit

Permalink
Fix perf problems found in investigation of issue #107728 - `CheckRun…
Browse files Browse the repository at this point in the history
…ClassInitThrowing` didn't check to see if the class had been initialized before taking a lock - `EnsureTlsIndexAllocated` didn't check if the Tls index had been allocated before setting the flag via an expensive Interlocked call to indicate that it had been allocated - And finally `JIT_GetNonGCThreadStaticBaseOptimized` and `JIT_GetGCThreadStaticBaseOptimized` were missing the fast paths which avoided even calling those apis at all. (#107817)

Perf with a small benchmark which does complex multithreaded work...

| Runtime | Time |
| ---- | ---- |
| .NET 8 | 00.9414682 s |
| .NET 9 before this fix |  22.8079382 |
| .NET 9 with this fix | 00.2004539 |

Fixes #107728

Co-authored-by: David Wrighton <davidwr@microsoft.com>
  • Loading branch information
github-actions[bot] and davidwrighton committed Sep 14, 2024
1 parent 3b622e7 commit 46cfb74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,12 @@ HCIMPL1(void*, JIT_GetNonGCThreadStaticBaseOptimized, UINT32 staticBlockIndex)

FCALL_CONTRACT;

staticBlock = GetThreadLocalStaticBaseIfExistsAndInitialized(staticBlockIndex);
if (staticBlock != NULL)
{
return staticBlock;
}

HELPER_METHOD_FRAME_BEGIN_RET_0(); // Set up a frame
TLSIndex tlsIndex(staticBlockIndex);
// Check if the class constructor needs to be run
Expand Down Expand Up @@ -975,6 +981,12 @@ HCIMPL1(void*, JIT_GetGCThreadStaticBaseOptimized, UINT32 staticBlockIndex)

FCALL_CONTRACT;

staticBlock = GetThreadLocalStaticBaseIfExistsAndInitialized(staticBlockIndex);
if (staticBlock != NULL)
{
return staticBlock;
}

HELPER_METHOD_FRAME_BEGIN_RET_0(); // Set up a frame

TLSIndex tlsIndex(staticBlockIndex);
Expand Down
10 changes: 7 additions & 3 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3840,8 +3840,8 @@ void MethodTable::CheckRunClassInitThrowing()
// To find GC hole easier...
TRIGGERSGC();

// Don't initialize shared generic instantiations (e.g. MyClass<__Canon>)
if (IsSharedByGenericInstantiations())
// Don't initialize shared generic instantiations (e.g. MyClass<__Canon>), or an already initialized MethodTable
if (IsClassInited() || IsSharedByGenericInstantiations())
return;

_ASSERTE(!ContainsGenericVariables());
Expand Down Expand Up @@ -3946,7 +3946,11 @@ void MethodTable::EnsureTlsIndexAllocated()
CONTRACTL_END;

PTR_MethodTableAuxiliaryData pAuxiliaryData = GetAuxiliaryDataForWrite();
if (!pAuxiliaryData->IsTlsIndexAllocated() && GetNumThreadStaticFields() > 0)

if (pAuxiliaryData->IsTlsIndexAllocated())
return;

if (GetNumThreadStaticFields() > 0)
{
ThreadStaticsInfo *pThreadStaticsInfo = MethodTableAuxiliaryData::GetThreadStaticsInfo(GetAuxiliaryDataForWrite());
// Allocate space for normal statics if we might have them
Expand Down

0 comments on commit 46cfb74

Please sign in to comment.