Skip to content

Commit

Permalink
Fix perf problems found in investigation of issue dotnet#107728 (dotn…
Browse files Browse the repository at this point in the history
…et#107806)

- `CheckRunClassInitThrowing` 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.

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 dotnet#107728
  • Loading branch information
davidwrighton authored and sirntar committed Sep 30, 2024
1 parent a2d370c commit 887ae22
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 @@ -3797,8 +3797,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 @@ -3903,7 +3903,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 887ae22

Please sign in to comment.