-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Cache extension method import info per project ID #78542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CyrusNajmabadi
merged 2 commits into
dotnet:main
from
CyrusNajmabadi:projectIdExtensionImportCache
May 10, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Runtime.CompilerServices; | ||
|
@@ -26,7 +27,12 @@ namespace Microsoft.CodeAnalysis.Completion.Providers; | |
/// <remarks>It runs out-of-proc if it's enabled</remarks> | ||
internal static partial class ExtensionMethodImportCompletionHelper | ||
{ | ||
private static readonly ConditionalWeakTable<ProjectState, ExtensionMethodImportCompletionCacheEntry> s_projectItemsCache = new(); | ||
/// <summary> | ||
/// Technically never gets cleared out. However, as we're only really storing information about extension | ||
/// methods in a project, this should not be too bad. It's only really a leak if the project is truly | ||
/// unloaded, and not too bad in that case. | ||
/// </summary> | ||
private static readonly ConcurrentDictionary<ProjectId, ExtensionMethodImportCompletionCacheEntry> s_projectItemsCache = new(); | ||
|
||
public static async Task WarmUpCacheAsync(Project project, CancellationToken cancellationToken) | ||
{ | ||
|
@@ -48,7 +54,7 @@ public static async Task WarmUpCacheAsync(Project project, CancellationToken can | |
public static void WarmUpCacheInCurrentProcess(Project project) | ||
=> SymbolComputer.QueueCacheWarmUpTask(project); | ||
|
||
public static async Task<SerializableUnimportedExtensionMethods?> GetUnimportedExtensionMethodsAsync( | ||
public static async Task<ImmutableArray<SerializableImportCompletionItem>> GetUnimportedExtensionMethodsAsync( | ||
SyntaxContext syntaxContext, | ||
ITypeSymbol receiverTypeSymbol, | ||
ISet<string> namespaceInScope, | ||
|
@@ -57,13 +63,10 @@ public static void WarmUpCacheInCurrentProcess(Project project) | |
bool hideAdvancedMembers, | ||
CancellationToken cancellationToken) | ||
{ | ||
SerializableUnimportedExtensionMethods? result = null; | ||
var document = syntaxContext.Document; | ||
var position = syntaxContext.Position; | ||
var project = document.Project; | ||
|
||
var totalTime = SharedStopwatch.StartNew(); | ||
|
||
var client = await RemoteHostClient.TryGetClientAsync(project, cancellationToken).ConfigureAwait(false); | ||
if (client != null) | ||
{ | ||
|
@@ -72,36 +75,24 @@ public static void WarmUpCacheInCurrentProcess(Project project) | |
|
||
// Call the project overload. Add-import-for-extension-method doesn't search outside of the current | ||
// project cone. | ||
var remoteResult = await client.TryInvokeAsync<IRemoteExtensionMethodImportCompletionService, SerializableUnimportedExtensionMethods?>( | ||
var remoteResult = await client.TryInvokeAsync<IRemoteExtensionMethodImportCompletionService, ImmutableArray<SerializableImportCompletionItem>>( | ||
project, | ||
(service, solutionInfo, cancellationToken) => service.GetUnimportedExtensionMethodsAsync( | ||
solutionInfo, document.Id, position, receiverTypeSymbolKeyData, [.. namespaceInScope], | ||
targetTypesSymbolKeyData, forceCacheCreation, hideAdvancedMembers, cancellationToken), | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
result = remoteResult.HasValue ? remoteResult.Value : null; | ||
return remoteResult.HasValue ? remoteResult.Value : default; | ||
} | ||
else | ||
{ | ||
result = await GetUnimportedExtensionMethodsInCurrentProcessAsync( | ||
document, syntaxContext.SemanticModel, position, receiverTypeSymbol, namespaceInScope, targetTypesSymbols, forceCacheCreation, hideAdvancedMembers, remoteAssetSyncTime: null, cancellationToken) | ||
return await GetUnimportedExtensionMethodsInCurrentProcessAsync( | ||
document, syntaxContext.SemanticModel, position, receiverTypeSymbol, namespaceInScope, targetTypesSymbols, forceCacheCreation, hideAdvancedMembers, cancellationToken) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
if (result is not null) | ||
{ | ||
// report telemetry: | ||
CompletionProvidersLogger.LogExtensionMethodCompletionTicksDataPoint( | ||
totalTime.Elapsed, result.GetSymbolsTime, result.CreateItemsTime, result.RemoteAssetSyncTime); | ||
|
||
if (result.IsPartialResult) | ||
CompletionProvidersLogger.LogExtensionMethodCompletionPartialResultCount(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static async Task<SerializableUnimportedExtensionMethods> GetUnimportedExtensionMethodsInCurrentProcessAsync( | ||
public static async Task<ImmutableArray<SerializableImportCompletionItem>> GetUnimportedExtensionMethodsInCurrentProcessAsync( | ||
Document document, | ||
SemanticModel? semanticModel, | ||
int position, | ||
|
@@ -110,27 +101,19 @@ public static async Task<SerializableUnimportedExtensionMethods> GetUnimportedEx | |
ImmutableArray<ITypeSymbol> targetTypes, | ||
bool forceCacheCreation, | ||
bool hideAdvancedMembers, | ||
TimeSpan? remoteAssetSyncTime, | ||
CancellationToken cancellationToken) | ||
{ | ||
var stopwatch = SharedStopwatch.StartNew(); | ||
|
||
// First find symbols of all applicable extension methods. | ||
// Workspace's syntax/symbol index is used to avoid iterating every method symbols in the solution. | ||
semanticModel ??= await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
var symbolComputer = new SymbolComputer( | ||
document, semanticModel, receiverTypeSymbol, position, namespaceInScope); | ||
var (extensionMethodSymbols, isPartialResult) = await symbolComputer.GetExtensionMethodSymbolsAsync(forceCacheCreation, hideAdvancedMembers, cancellationToken).ConfigureAwait(false); | ||
|
||
var getSymbolsTime = stopwatch.Elapsed; | ||
stopwatch = SharedStopwatch.StartNew(); | ||
var extensionMethodSymbols = await symbolComputer.GetExtensionMethodSymbolsAsync(forceCacheCreation, hideAdvancedMembers, cancellationToken).ConfigureAwait(false); | ||
|
||
var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false); | ||
var items = ConvertSymbolsToCompletionItems(compilation, extensionMethodSymbols, targetTypes, cancellationToken); | ||
|
||
var createItemsTime = stopwatch.Elapsed; | ||
|
||
return new SerializableUnimportedExtensionMethods(items, isPartialResult, getSymbolsTime, createItemsTime, remoteAssetSyncTime); | ||
return items; | ||
} | ||
|
||
public static async ValueTask BatchUpdateCacheAsync(ImmutableSegmentedList<Project> projects, CancellationToken cancellationToken) | ||
|
@@ -250,15 +233,16 @@ private static string GetFullyQualifiedNamespaceName(INamespaceSymbol symbol, Di | |
|
||
private static async Task<ExtensionMethodImportCompletionCacheEntry> GetUpToDateCacheEntryAsync( | ||
Project project, | ||
IImportCompletionCacheService<ExtensionMethodImportCompletionCacheEntry, object> cacheService, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not used. |
||
CancellationToken cancellationToken) | ||
{ | ||
var projectId = project.Id; | ||
|
||
// While we are caching data from SyntaxTreeInfo, all the things we cared about here are actually based on sources symbols. | ||
// So using source symbol checksum would suffice. | ||
var checksum = await SymbolTreeInfo.GetSourceSymbolsChecksumAsync(project, cancellationToken).ConfigureAwait(false); | ||
|
||
// Cache miss, create all requested items. | ||
if (!s_projectItemsCache.TryGetValue(project.State, out var cacheEntry) || | ||
if (!s_projectItemsCache.TryGetValue(projectId, out var cacheEntry) || | ||
cacheEntry.Checksum != checksum || | ||
cacheEntry.Language != project.Language) | ||
{ | ||
|
@@ -281,12 +265,7 @@ private static async Task<ExtensionMethodImportCompletionCacheEntry> GetUpToDate | |
} | ||
|
||
cacheEntry = builder.ToCacheEntry(); | ||
#if NET | ||
s_projectItemsCache.AddOrUpdate(project.State, cacheEntry); | ||
#else | ||
s_projectItemsCache.Remove(project.State); | ||
s_projectItemsCache.GetValue(project.State, _ => cacheEntry); | ||
#endif | ||
s_projectItemsCache[projectId] = cacheEntry; | ||
} | ||
|
||
return cacheEntry; | ||
|
This file contains hidden or 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
33 changes: 0 additions & 33 deletions
33
...e/Completion/Providers/ImportCompletionProvider/SerializableUnimportedExtensionMethods.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never used.