From ce6b1a98ce62b85e2a06ccaad38ef6f26a9966e2 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 27 Jan 2020 15:21:40 -0800 Subject: [PATCH] Minimize the new SharedStopwatch API --- .../DiagnosticAnalyzer/AnalyzerExecutor.cs | 12 ++--- .../InternalUtilities/SharedStopwatch.cs | 53 +++---------------- .../Microsoft.CodeAnalysis.Workspaces.csproj | 1 - 3 files changed, 12 insertions(+), 54 deletions(-) diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs index 85b1658895f54..a6e045e67d636 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs @@ -1453,17 +1453,17 @@ private void ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, A { timer = SharedStopwatch.StartNew(); - // This call to Restart isn't required by the API, but is included to avoid measurement errors which - // can occur during periods of high allocation activity. In some cases, calls to Stopwatch + // This call to StartNew isn't required by the API, but is included to avoid measurement errors + // which can occur during periods of high allocation activity. In some cases, calls to Stopwatch // operations can block at their return point on the completion of a background GC operation. When // this occurs, the GC wait time ends up included in the measured time span. In the event the first - // call to Restart blocked on a GC operation, this call to Restart will most likely occur when the + // call to StartNew blocked on a GC operation, this call to StartNew will most likely occur when the // GC is no longer active. In practice, a substantial improvement to the consistency of analyzer // timing data was observed. // - // Note that the call to Stopwatch.Stop() is not affected, because the GC wait will occur after the - // timer has already recorded its stop time. - timer.Restart(); + // Note that the call to SharedStopwatch.Elapsed is not affected, because the GC wait will occur + // after the timer has already recorded its stop time. + timer = SharedStopwatch.StartNew(); } analyze(argument); diff --git a/src/Compilers/Core/Portable/InternalUtilities/SharedStopwatch.cs b/src/Compilers/Core/Portable/InternalUtilities/SharedStopwatch.cs index af862c08955d7..e85adf6326b89 100644 --- a/src/Compilers/Core/Portable/InternalUtilities/SharedStopwatch.cs +++ b/src/Compilers/Core/Portable/InternalUtilities/SharedStopwatch.cs @@ -7,61 +7,20 @@ namespace Roslyn.Utilities { - internal struct SharedStopwatch + internal readonly struct SharedStopwatch { private static readonly Stopwatch s_stopwatch = Stopwatch.StartNew(); - private TimeSpan _accumulated; - private TimeSpan _started; + private readonly TimeSpan _started; - public readonly TimeSpan Elapsed + private SharedStopwatch(TimeSpan started) { - get - { - if (IsRunning) - return _accumulated + s_stopwatch.Elapsed - _started; - - return _accumulated; - } + _started = started; } - public readonly long ElapsedMilliseconds => (long)Elapsed.TotalMilliseconds; - public readonly bool IsRunning => _started != TimeSpan.Zero; + public TimeSpan Elapsed => s_stopwatch.Elapsed - _started; public static SharedStopwatch StartNew() - { - var result = new SharedStopwatch(); - result.Start(); - return result; - } - - public void Reset() - { - Stop(); - _accumulated = TimeSpan.Zero; - } - - public void Restart() - { - Reset(); - Start(); - } - - public void Start() - { - if (!IsRunning) - { - _started = s_stopwatch.Elapsed; - } - } - - public void Stop() - { - if (IsRunning) - { - _accumulated += s_stopwatch.Elapsed - _started; - _started = TimeSpan.Zero; - } - } + => new SharedStopwatch(s_stopwatch.Elapsed); } } diff --git a/src/Workspaces/Core/Portable/Microsoft.CodeAnalysis.Workspaces.csproj b/src/Workspaces/Core/Portable/Microsoft.CodeAnalysis.Workspaces.csproj index 06c554c4babbc..524bc035d673b 100644 --- a/src/Workspaces/Core/Portable/Microsoft.CodeAnalysis.Workspaces.csproj +++ b/src/Workspaces/Core/Portable/Microsoft.CodeAnalysis.Workspaces.csproj @@ -27,7 +27,6 @@ - Execution\Desktop\DesktopAnalyzerAssemblyLoader.cs