-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache the MEF composition in the Roslyn LSP. (#76276)
To improve startup time of the LSP we can cache the MEF composition and load it from file instead of building up a new composition. The cache is invalidated when: 1. The location of the Microsoft.CodeAnalysis.LanguageServer changes (such as installing an updated C# extension or `dotnet.server.path` is updated). 2. Changing major .NET runtime version. 3. The set of assembly paths that make up the composition changes. 4. The last write time of any assembly that is part of the composition changes. Resolves #68568
- Loading branch information
Showing
8 changed files
with
232 additions
and
39 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...guageServer/Microsoft.CodeAnalysis.LanguageServer.UnitTests/ExportProviderBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests | ||
{ | ||
public sealed class ExportProviderBuilderTests(ITestOutputHelper testOutputHelper) | ||
: AbstractLanguageServerHostTests(testOutputHelper) | ||
{ | ||
[Fact] | ||
public async Task MefCompositionIsCached() | ||
{ | ||
await using var testServer = await CreateLanguageServerAsync(includeDevKitComponents: false); | ||
|
||
await AssertCacheWriteWasAttemptedAsync(); | ||
|
||
AssertCachedCompositionCountEquals(expectedCount: 1); | ||
} | ||
|
||
[Fact] | ||
public async Task MefCompositionIsReused() | ||
{ | ||
await using var testServer = await CreateLanguageServerAsync(includeDevKitComponents: false); | ||
|
||
await AssertCacheWriteWasAttemptedAsync(); | ||
|
||
// Second test server with the same set of assemblies. | ||
await using var testServer2 = await CreateLanguageServerAsync(includeDevKitComponents: false); | ||
|
||
AssertNoCacheWriteWasAttempted(); | ||
|
||
AssertCachedCompositionCountEquals(expectedCount: 1); | ||
} | ||
|
||
[Fact] | ||
public async Task MultipleMefCompositionsAreCached() | ||
{ | ||
await using var testServer = await CreateLanguageServerAsync(includeDevKitComponents: false); | ||
|
||
await AssertCacheWriteWasAttemptedAsync(); | ||
|
||
// Second test server with a different set of assemblies. | ||
await using var testServer2 = await CreateLanguageServerAsync(includeDevKitComponents: true); | ||
|
||
await AssertCacheWriteWasAttemptedAsync(); | ||
|
||
AssertCachedCompositionCountEquals(expectedCount: 2); | ||
} | ||
|
||
private async Task AssertCacheWriteWasAttemptedAsync() | ||
{ | ||
var cacheWriteTask = ExportProviderBuilder.TestAccessor.GetCacheWriteTask(); | ||
Assert.NotNull(cacheWriteTask); | ||
|
||
await cacheWriteTask; | ||
} | ||
|
||
private void AssertNoCacheWriteWasAttempted() | ||
{ | ||
var cacheWriteTask2 = ExportProviderBuilder.TestAccessor.GetCacheWriteTask(); | ||
Assert.Null(cacheWriteTask2); | ||
} | ||
|
||
private void AssertCachedCompositionCountEquals(int expectedCount) | ||
{ | ||
var mefCompositions = Directory.EnumerateFiles(MefCacheDirectory.Path, "*.mef-composition", SearchOption.AllDirectories); | ||
|
||
Assert.Equal(expectedCount, mefCompositions.Count()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.