Skip to content

Commit 6e6ad01

Browse files
authored
Merge pull request #4110 from davidwengier/Perf
Reduce work during solution close
2 parents 130ebd7 + a7149a9 commit 6e6ad01

File tree

25 files changed

+438
-131
lines changed

25 files changed

+438
-131
lines changed

src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/ProjectSystem/ProjectSnapshotManagerBenchmarkBase.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.IO;
7-
using System.Threading;
8-
using System.Threading.Tasks;
94
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
105
using Microsoft.AspNetCore.Razor.Language;
116
using Microsoft.CodeAnalysis;

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/BackgroundDocumentGenerator.cs

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal class BackgroundDocumentGenerator : ProjectSnapshotChangeTrigger
1717
private readonly Dictionary<string, DocumentSnapshot> _work;
1818
private ProjectSnapshotManagerBase _projectManager;
1919
private Timer _timer;
20+
private bool _solutionIsClosing;
2021

2122
public BackgroundDocumentGenerator(
2223
ProjectSnapshotManagerDispatcher projectSnapshotManagerDispatcher,
@@ -176,6 +177,11 @@ private async void Timer_Tick(object state)
176177

177178
for (var i = 0; i < work.Length; i++)
178179
{
180+
if (_solutionIsClosing)
181+
{
182+
break;
183+
}
184+
179185
var document = work[i].Value;
180186
try
181187
{
@@ -189,9 +195,12 @@ private async void Timer_Tick(object state)
189195

190196
OnCompletingBackgroundWork();
191197

192-
await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
193-
() => NotifyDocumentsProcessed(work),
194-
CancellationToken.None);
198+
if (!_solutionIsClosing)
199+
{
200+
await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
201+
() => NotifyDocumentsProcessed(work),
202+
CancellationToken.None);
203+
}
195204

196205
lock (_work)
197206
{
@@ -200,7 +209,7 @@ await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
200209
_timer = null;
201210

202211
// If more work came in while we were running start the worker again.
203-
if (_work.Count > 0)
212+
if (_work.Count > 0 && !_solutionIsClosing)
204213
{
205214
StartWorker();
206215
}
@@ -232,6 +241,14 @@ private void NotifyDocumentsProcessed(KeyValuePair<string, DocumentSnapshot>[] w
232241

233242
private void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
234243
{
244+
// Don't do any work if the solution is closing
245+
if (args.SolutionIsClosing)
246+
{
247+
_solutionIsClosing = true;
248+
return;
249+
}
250+
_solutionIsClosing = false;
251+
235252
_projectSnapshotManagerDispatcher.AssertDispatcherThread();
236253

237254
switch (args.Kind)

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultDocumentVersionCache.cs

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public override void Initialize(ProjectSnapshotManagerBase projectManager)
101101

102102
private void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
103103
{
104+
// Don't do any work if the solution is closing
105+
if (args.SolutionIsClosing)
106+
{
107+
return;
108+
}
109+
104110
_projectSnapshotManagerDispatcher.AssertDispatcherThread();
105111

106112
switch (args.Kind)

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultGeneratedDocumentContainerStore.cs

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public override void Initialize(ProjectSnapshotManagerBase projectManager)
6666
// Internal for testing
6767
internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
6868
{
69+
// Don't do any work if the solution is closing
70+
if (args.SolutionIsClosing)
71+
{
72+
return;
73+
}
74+
6975
_projectSnapshotManagerDispatcher.AssertDispatcherThread();
7076

7177
switch (args.Kind)

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/DefaultGeneratedDocumentPublisher.cs

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ public override void PublishHtml(string filePath, SourceText sourceText, int hos
167167

168168
private void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
169169
{
170+
// Don't do any work if the solution is closing
171+
if (args.SolutionIsClosing)
172+
{
173+
return;
174+
}
175+
170176
_projectSnapshotManagerDispatcher.AssertDispatcherThread();
171177

172178
switch (args.Kind)

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal class OpenDocumentGenerator : ProjectSnapshotChangeTrigger
1818
private readonly Dictionary<string, DocumentSnapshot> _work;
1919
private ProjectSnapshotManagerBase _projectManager;
2020
private Timer _timer;
21+
private bool _solutionIsClosing;
2122

2223
public OpenDocumentGenerator(
2324
ProjectSnapshotManagerDispatcher projectSnapshotManagerDispatcher,
@@ -182,6 +183,11 @@ private async void Timer_Tick(object state)
182183

183184
for (var i = 0; i < work.Length; i++)
184185
{
186+
if (_solutionIsClosing)
187+
{
188+
break;
189+
}
190+
185191
var document = work[i].Value;
186192
try
187193
{
@@ -195,7 +201,7 @@ private async void Timer_Tick(object state)
195201

196202
OnCompletingBackgroundWork();
197203

198-
if (_documentProcessedListeners.Count != 0)
204+
if (_documentProcessedListeners.Count != 0 && !_solutionIsClosing)
199205
{
200206
await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
201207
() => NotifyDocumentsProcessed(work),
@@ -209,7 +215,7 @@ await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
209215
_timer = null;
210216

211217
// If more work came in while we were running start the worker again.
212-
if (_work.Count > 0)
218+
if (_work.Count > 0 && !_solutionIsClosing)
213219
{
214220
StartWorker();
215221
}
@@ -241,6 +247,14 @@ private void NotifyDocumentsProcessed(KeyValuePair<string, DocumentSnapshot>[] w
241247

242248
private void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
243249
{
250+
// Don't do any work if the solution is closing
251+
if (args.SolutionIsClosing)
252+
{
253+
_solutionIsClosing = true;
254+
return;
255+
}
256+
_solutionIsClosing = false;
257+
244258
_projectSnapshotManagerDispatcher.AssertDispatcherThread();
245259

246260
switch (args.Kind)

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectConfigurationStateSynchronizer.cs

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public void ProjectConfigurationFileChanged(ProjectConfigurationFileChangeEventA
7777
return;
7878
}
7979

80-
8180
EnqueueUpdateProject(handle.FilePath, handle);
8281
break;
8382
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorServerReadyPublisher.cs

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public override void Initialize(ProjectSnapshotManagerBase projectManager)
5050
private async void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventArgs args)
5151
#pragma warning restore VSTHRD100 // Avoid async void methods
5252
{
53+
// Don't do any work if the solution is closing
54+
if (args.SolutionIsClosing)
55+
{
56+
return;
57+
}
58+
5359
_projectSnapshotManagerDispatcher.AssertDispatcherThread();
5460

5561
var projectSnapshot = args.Newer;

0 commit comments

Comments
 (0)