Remove unnecessary volatile from EventCounter (#37309)
authorStephen Toub <stoub@microsoft.com>
Wed, 3 Jun 2020 02:44:17 +0000 (22:44 -0400)
committerGitHub <noreply@github.com>
Wed, 3 Jun 2020 02:44:17 +0000 (19:44 -0700)
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventCounter.cs

index 2fbe8b4..f489b11 100644 (file)
@@ -38,7 +38,13 @@ namespace System.Diagnostics.Tracing
             _min = double.PositiveInfinity;
             _max = double.NegativeInfinity;
 
-            InitializeBuffer();
+            var bufferedValues = new double[BufferedSize];
+            for (int i = 0; i < bufferedValues.Length; i++)
+            {
+                bufferedValues[i] = UnusedBufferSlotValue;
+            }
+            Volatile.Write(ref _bufferedValues, bufferedValues);
+
             Publish();
         }
 
@@ -136,19 +142,9 @@ namespace System.Diagnostics.Tracing
         // Values buffering
         private const int BufferedSize = 10;
         private const double UnusedBufferSlotValue = double.NegativeInfinity;
-        private volatile double[] _bufferedValues;
+        private readonly double[] _bufferedValues;
         private volatile int _bufferedValuesIndex;
 
-        [MemberNotNull(nameof(_bufferedValues))]
-        private void InitializeBuffer()
-        {
-            _bufferedValues = new double[BufferedSize];
-            for (int i = 0; i < _bufferedValues.Length; i++)
-            {
-                _bufferedValues[i] = UnusedBufferSlotValue;
-            }
-        }
-
         private void Enqueue(double value)
         {
             // It is possible that two threads read the same bufferedValuesIndex, but only one will be able to write the slot, so that is okay.