From cc696254242038fe03ce5feba936980ea20e2eac Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Thu, 3 Apr 2025 10:30:59 -0700 Subject: [PATCH] Moved a static field initialization from Thread to ProcessorIdCache --- .../src/System/Threading/ProcessorIdCache.cs | 7 +++++++ .../System.Private.CoreLib/src/System/Threading/Thread.cs | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ProcessorIdCache.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ProcessorIdCache.cs index a31516c94f048a..25fac4050acf13 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ProcessorIdCache.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ProcessorIdCache.cs @@ -22,6 +22,10 @@ internal static class ProcessorIdCache // We will not adjust higher than this though. private const int MaxIdRefreshRate = 5000; + // a speed check will determine refresh rate of the cache and will report if caching is not advisable. + // we will record that in a readonly static so that it could become a JIT constant and bypass caching entirely. + private static readonly bool s_isProcessorNumberReallyFast = ProcessorIdCache.ProcessorNumberSpeedCheck(); + private static int RefreshCurrentProcessorId() { int currentProcessorId = Thread.GetCurrentProcessorNumber(); @@ -44,6 +48,9 @@ private static int RefreshCurrentProcessorId() [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static int GetCurrentProcessorId() { + if (s_isProcessorNumberReallyFast) + return Thread.GetCurrentProcessorNumber(); + int currentProcessorIdCache = t_currentProcessorIdCache--; if ((currentProcessorIdCache & ProcessorIdCacheCountDownMask) == 0) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs index 4dc0a5002e7e78..f92abc7789ab7c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs @@ -719,16 +719,9 @@ public static void SetData(LocalDataStoreSlot slot, object? value) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetCurrentProcessorId() { - if (s_isProcessorNumberReallyFast) - return GetCurrentProcessorNumber(); - return ProcessorIdCache.GetCurrentProcessorId(); } - // a speed check will determine refresh rate of the cache and will report if caching is not advisable. - // we will record that in a readonly static so that it could become a JIT constant and bypass caching entirely. - private static readonly bool s_isProcessorNumberReallyFast = ProcessorIdCache.ProcessorNumberSpeedCheck(); - #if FEATURE_WASM_MANAGED_THREADS [ThreadStatic] public static bool ThrowOnBlockingWaitOnJSInteropThread;