Skip to content

Commit 8f4c768

Browse files
Moved a static field initialization from Thread to ProcessorIdCache (#114273)
Co-authored-by: vsadov <[email protected]>
1 parent 3e2029d commit 8f4c768

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/ProcessorIdCache.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ internal static class ProcessorIdCache
2222
// We will not adjust higher than this though.
2323
private const int MaxIdRefreshRate = 5000;
2424

25+
// a speed check will determine refresh rate of the cache and will report if caching is not advisable.
26+
// we will record that in a readonly static so that it could become a JIT constant and bypass caching entirely.
27+
private static readonly bool s_isProcessorNumberReallyFast = ProcessorIdCache.ProcessorNumberSpeedCheck();
28+
2529
private static int RefreshCurrentProcessorId()
2630
{
2731
int currentProcessorId = Thread.GetCurrentProcessorNumber();
@@ -44,6 +48,9 @@ private static int RefreshCurrentProcessorId()
4448
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4549
internal static int GetCurrentProcessorId()
4650
{
51+
if (s_isProcessorNumberReallyFast)
52+
return Thread.GetCurrentProcessorNumber();
53+
4754
int currentProcessorIdCache = t_currentProcessorIdCache--;
4855
if ((currentProcessorIdCache & ProcessorIdCacheCountDownMask) == 0)
4956
{

src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -713,16 +713,9 @@ public static void SetData(LocalDataStoreSlot slot, object? value)
713713
[MethodImpl(MethodImplOptions.AggressiveInlining)]
714714
public static int GetCurrentProcessorId()
715715
{
716-
if (s_isProcessorNumberReallyFast)
717-
return GetCurrentProcessorNumber();
718-
719716
return ProcessorIdCache.GetCurrentProcessorId();
720717
}
721718

722-
// a speed check will determine refresh rate of the cache and will report if caching is not advisable.
723-
// we will record that in a readonly static so that it could become a JIT constant and bypass caching entirely.
724-
private static readonly bool s_isProcessorNumberReallyFast = ProcessorIdCache.ProcessorNumberSpeedCheck();
725-
726719
#if FEATURE_WASM_MANAGED_THREADS
727720
[ThreadStatic]
728721
public static bool ThrowOnBlockingWaitOnJSInteropThread;

0 commit comments

Comments
 (0)