From 756309e065cb468c5989802c47fbc08f53568d21 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 20 Jun 2024 08:22:49 -0500 Subject: [PATCH] Avoid allocations in AbstractSyntaxIndex<>.GetIndexAsync --- .../FindSymbols/Shared/AbstractSyntaxIndex.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs b/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs index e7fa239714abf..6731eaecf5de5 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/Shared/AbstractSyntaxIndex.cs @@ -61,9 +61,16 @@ protected static async ValueTask 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 + 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;