Skip to content

Moved a static field initialization from Thread to ProcessorIdCache #114227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading