Less work for OptimalMaxSpinWaitsPerSpinIteration fast-path (#21702)
authorBen Adams <thundercat@illyriad.co.uk>
Thu, 6 Jun 2019 00:36:16 +0000 (01:36 +0100)
committerJan Kotas <jkotas@microsoft.com>
Thu, 6 Jun 2019 00:36:16 +0000 (17:36 -0700)
src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs

index 9b3118a..f849358 100644 (file)
@@ -468,20 +468,22 @@ namespace System.Threading
         {
             get
             {
-                if (s_optimalMaxSpinWaitsPerSpinIteration != 0)
-                {
-                    return s_optimalMaxSpinWaitsPerSpinIteration;
-                }
-
-                // This is done lazily because the first call to the function below in the process triggers a measurement that
-                // takes a nontrivial amount of time if the measurement has not already been done in the backgorund.
-                // See Thread::InitializeYieldProcessorNormalized(), which describes and calculates this value.
-                s_optimalMaxSpinWaitsPerSpinIteration = GetOptimalMaxSpinWaitsPerSpinIterationInternal();
-                Debug.Assert(s_optimalMaxSpinWaitsPerSpinIteration > 0);
-                return s_optimalMaxSpinWaitsPerSpinIteration;
+                int optimalMaxSpinWaitsPerSpinIteration = s_optimalMaxSpinWaitsPerSpinIteration;
+                return optimalMaxSpinWaitsPerSpinIteration != 0 ? optimalMaxSpinWaitsPerSpinIteration : CalculateOptimalMaxSpinWaitsPerSpinIteration();
             }
         }
 
+        [MethodImpl(MethodImplOptions.NoInlining)]
+        private static int CalculateOptimalMaxSpinWaitsPerSpinIteration()
+        {
+            // This is done lazily because the first call to the function below in the process triggers a measurement that
+            // takes a nontrivial amount of time if the measurement has not already been done in the backgorund.
+            // See Thread::InitializeYieldProcessorNormalized(), which describes and calculates this value.
+            s_optimalMaxSpinWaitsPerSpinIteration = GetOptimalMaxSpinWaitsPerSpinIterationInternal();
+            Debug.Assert(s_optimalMaxSpinWaitsPerSpinIteration > 0);
+            return s_optimalMaxSpinWaitsPerSpinIteration;
+        }
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern int GetCurrentProcessorNumber();