From af58633f34fa9b180c27ceb718a75916d226d527 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 29 Mar 2021 06:50:48 -0400 Subject: [PATCH] Remove unnecessary delegate/closure allocations from PollForValues It allocates a delegate/closure for each counter group each time the loop runs. --- .../src/System/Diagnostics/Tracing/CounterGroup.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs index 3146d1f70546cb..10da258166b03c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs @@ -271,10 +271,9 @@ private static void PollForValues() // We cache these outside of the scope of s_counterGroupLock because // calling into the callbacks can cause a re-entrancy into CounterGroup.Enable() // and result in a deadlock. (See https://github.com/dotnet/runtime/issues/40190 for details) - List onTimers = new List(); + var onTimers = new List(); while (true) { - onTimers.Clear(); int sleepDurationInMilliseconds = int.MaxValue; lock (s_counterGroupLock) { @@ -284,7 +283,7 @@ private static void PollForValues() DateTime now = DateTime.UtcNow; if (counterGroup._nextPollingTimeStamp < now + new TimeSpan(0, 0, 0, 0, 1)) { - onTimers.Add(() => counterGroup.OnTimer()); + onTimers.Add(counterGroup); } int millisecondsTillNextPoll = (int)((counterGroup._nextPollingTimeStamp - now).TotalMilliseconds); @@ -292,10 +291,11 @@ private static void PollForValues() sleepDurationInMilliseconds = Math.Min(sleepDurationInMilliseconds, millisecondsTillNextPoll); } } - foreach (Action onTimer in onTimers) + foreach (CounterGroup onTimer in onTimers) { - onTimer.Invoke(); + onTimer.OnTimer(); } + onTimers.Clear(); if (sleepDurationInMilliseconds == int.MaxValue) { sleepDurationInMilliseconds = -1; // WaitOne uses -1 to mean infinite