Skip to content
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

Avoid allocations in AbstractSyntaxIndex<>.GetIndexAsync #74075

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,16 @@ protected static async ValueTask<TIndex> GetRequiredIndexAsync(
return null;

// Populate our caches with this data.
s_documentToIndex.GetValue(document, _ => index);
#if NET
s_documentToIndex.TryAdd(document, index);
s_documentIdToIndex.AddOrUpdate(document.Id, index);
#else
// Avoid capturing index on the fast path by making a copy for the slow path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Avoid capturing index on the fast path by making a copy for the slow path

Could this be moved to a local fn to avoid the allocation instead?

Copy link
Member Author

@sharwell sharwell Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but it wouldn't be less net code. Creating a temporary local is pretty standard practice for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've never noticed it done this way whereas I've seen it done the other way a couple times.

var indexCopy = index;
s_documentToIndex.GetValue(document, _ => indexCopy);
s_documentIdToIndex.Remove(document.Id);
s_documentIdToIndex.GetValue(document.Id, _ => index);
s_documentIdToIndex.GetValue(document.Id, _ => indexCopy);
#endif
}

return index;
Expand Down
Loading