-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Reduce allocations due to multiple levels of ImmutableArray<FinderLocations> created during find references invocations. #73118
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,7 +203,7 @@ private async Task ProcessProjectAsync(Project project, ImmutableArray<ISymbol> | |
{ | ||
await finder.DetermineDocumentsToSearchAsync( | ||
symbol, globalAliases, project, _documents, | ||
static (doc, documents) => documents.Add(doc), | ||
StandardCallbacks<Document>.AddToHashSet, | ||
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. bute. |
||
foundDocuments, | ||
_options, cancellationToken).ConfigureAwait(false); | ||
|
||
|
@@ -272,12 +272,15 @@ private async Task ProcessDocumentAsync( | |
// just grab those once here and hold onto them for the lifetime of this call. | ||
var cache = await FindReferenceCache.GetCacheAsync(document, cancellationToken).ConfigureAwait(false); | ||
|
||
// scratch hashset to place results in. Populated/inspected/cleared in inner loop. | ||
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. this does not look like a hashset :) |
||
using var _ = ArrayBuilder<FinderLocation>.GetInstance(out var foundReferenceLocations); | ||
|
||
foreach (var symbol in symbols) | ||
{ | ||
var globalAliases = TryGet(symbolToGlobalAliases, symbol); | ||
var state = new FindReferencesDocumentState(cache, globalAliases); | ||
|
||
await ProcessDocumentAsync(symbol, state).ConfigureAwait(false); | ||
await ProcessDocumentAsync(symbol, state, foundReferenceLocations).ConfigureAwait(false); | ||
} | ||
} | ||
finally | ||
|
@@ -286,7 +289,7 @@ private async Task ProcessDocumentAsync( | |
} | ||
|
||
async Task ProcessDocumentAsync( | ||
ISymbol symbol, FindReferencesDocumentState state) | ||
ISymbol symbol, FindReferencesDocumentState state, ArrayBuilder<FinderLocation> foundReferenceLocations) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
|
@@ -297,11 +300,13 @@ async Task ProcessDocumentAsync( | |
var group = _symbolToGroup[symbol]; | ||
foreach (var finder in _finders) | ||
{ | ||
var references = await finder.FindReferencesInDocumentAsync( | ||
symbol, state, _options, cancellationToken).ConfigureAwait(false); | ||
foreach (var (_, location) in references) | ||
await finder.FindReferencesInDocumentAsync( | ||
symbol, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, foundReferenceLocations, _options, cancellationToken).ConfigureAwait(false); | ||
foreach (var (_, location) in foundReferenceLocations) | ||
await _progress.OnReferenceFoundAsync(group, symbol, location, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
foundReferenceLocations.Clear(); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,16 +101,21 @@ async ValueTask PerformSearchInDocumentAsync( | |
async ValueTask PerformSearchInDocumentWorkerAsync( | ||
ISymbol symbol, FindReferencesDocumentState state) | ||
{ | ||
// Scratch buffer to place references for each finder. Cleared at the end of every loop iteration. | ||
using var _ = ArrayBuilder<FinderLocation>.GetInstance(out var referencesForFinder); | ||
|
||
// Always perform a normal search, looking for direct references to exactly that symbol. | ||
foreach (var finder in _finders) | ||
{ | ||
var references = await finder.FindReferencesInDocumentAsync( | ||
symbol, state, _options, cancellationToken).ConfigureAwait(false); | ||
foreach (var (_, location) in references) | ||
await finder.FindReferencesInDocumentAsync( | ||
symbol, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, referencesForFinder, _options, cancellationToken).ConfigureAwait(false); | ||
foreach (var (_, location) in referencesForFinder) | ||
{ | ||
var group = await ReportGroupAsync(symbol, cancellationToken).ConfigureAwait(false); | ||
await _progress.OnReferenceFoundAsync(group, symbol, location, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
referencesForFinder.Clear(); | ||
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. it's interesting how close this pattern is to the pipewriting/reading stuff i just did. this might get cleaner (in the future) with a channel that is pulling off the items and reporting to them to the progress, while the main find is pushing into the channel. |
||
} | ||
|
||
// Now, for symbols that could involve inheritance, look for references to the same named entity, and | ||
|
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.
nit: i would be ok with this as a single line.