Skip to content

Commit 65bbeb5

Browse files
committed
Reduce allocations in NamespaceSymbolKey.Resolve
Related to https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2136177
1 parent d5ebaf2 commit 65bbeb5

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/Compilers/Core/Portable/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Microsoft.CodeAnalysis.GeneratorFilterContext
66
Microsoft.CodeAnalysis.GeneratorFilterContext.CancellationToken.get -> System.Threading.CancellationToken
77
Microsoft.CodeAnalysis.GeneratorFilterContext.Generator.get -> Microsoft.CodeAnalysis.ISourceGenerator!
88
Microsoft.CodeAnalysis.GeneratorFilterContext.GeneratorFilterContext() -> void
9+
Microsoft.CodeAnalysis.INamespaceSymbol.GetMembers<TArg>(string! name, System.Action<Microsoft.CodeAnalysis.INamespaceOrTypeSymbol!, TArg>! callback, TArg argument) -> void
910
Microsoft.CodeAnalysis.IPropertySymbol.PartialDefinitionPart.get -> Microsoft.CodeAnalysis.IPropertySymbol?
1011
Microsoft.CodeAnalysis.IPropertySymbol.PartialImplementationPart.get -> Microsoft.CodeAnalysis.IPropertySymbol?
1112
Microsoft.CodeAnalysis.IPropertySymbol.IsPartialDefinition.get -> bool

src/Compilers/Core/Portable/Symbols/INamespaceSymbol.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Collections.Immutable;
78
using Microsoft.CodeAnalysis.Text;
@@ -27,6 +28,11 @@ public interface INamespaceSymbol : INamespaceOrTypeSymbol
2728
/// </summary>
2829
new IEnumerable<INamespaceOrTypeSymbol> GetMembers(string name);
2930

31+
/// <summary>
32+
/// Get all the members of this symbol that have a particular name.
33+
/// </summary>
34+
void GetMembers<TArg>(string name, Action<INamespaceOrTypeSymbol, TArg> callback, TArg argument);
35+
3036
/// <summary>
3137
/// Get all the members of this symbol that are namespaces.
3238
/// </summary>

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/SymbolKey/SymbolKey.NamespaceSymbolKey.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ protected sealed override SymbolKeyResolution Resolve(
100100
result.AddIfNotNull(module.GlobalNamespace);
101101
break;
102102
case INamespaceSymbol namespaceSymbol:
103-
foreach (var member in namespaceSymbol.GetMembers(metadataName))
104-
{
105-
if (member is INamespaceSymbol childNamespace)
103+
namespaceSymbol.GetMembers(
104+
metadataName,
105+
static (member, result) =>
106106
{
107-
result.AddIfNotNull(childNamespace);
108-
}
109-
}
107+
if (member is INamespaceSymbol childNamespace)
108+
{
109+
result.AddIfNotNull(childNamespace);
110+
}
111+
},
112+
result.Builder);
110113

111114
break;
112115
}

0 commit comments

Comments
 (0)