Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions src/Compilers/CSharp/Portable/Symbols/MergedNamespaceSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
Expand Down Expand Up @@ -299,5 +296,18 @@ internal override void GetExtensionMethods(ArrayBuilder<MethodSymbol> methods, s
namespaceSymbol.GetExtensionMethods(methods, name, arity, options);
}
}

// Overridden to avoid NamespaceSymbol.GetExtensionContainers call to GetTypeMembersUnordered. The combination of the
// CreateRange and OfType Linq calls in MergedNamespaceSymbol.GetTypeMembersUnordered causes a full array allocation.
internal sealed override void GetExtensionContainers(ArrayBuilder<NamedTypeSymbol> extensions)
{
foreach (var member in GetMembersUnordered())
{
if (member is NamedTypeSymbol type)
{
AddExtensionContainersInType(type, extensions);
}
}
}
}
}
20 changes: 13 additions & 7 deletions src/Compilers/CSharp/Portable/Symbols/NamespaceSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,24 @@ internal virtual void GetExtensionMethods(ArrayBuilder<MethodSymbol> methods, st
}
}

internal void GetExtensionContainers(ArrayBuilder<NamedTypeSymbol> extensions)
internal virtual void GetExtensionContainers(ArrayBuilder<NamedTypeSymbol> extensions)
{
foreach (var type in this.GetTypeMembersUnordered())
{
if (!type.IsReferenceType || !type.IsStatic || type.IsGenericType || !type.MightContainExtensionMethods) continue;
AddExtensionContainersInType(type, extensions);
}
}

protected void AddExtensionContainersInType(NamedTypeSymbol type, ArrayBuilder<NamedTypeSymbol> extensions)
{
// Consider whether IsClassType could be used instead. Tracked by https://github.com/dotnet/roslyn/issues/78275
if (!type.IsReferenceType || !type.IsStatic || type.IsGenericType || !type.MightContainExtensionMethods) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

IsReferenceType

I think we can replace this with IsClassType to optimize this code even more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not an obvious equivalence (for me, at least). I'd prefer not to make that change, but will if you would like me to.

Copy link
Contributor

@AlekseyTs AlekseyTs Apr 23, 2025

Choose a reason for hiding this comment

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

Consider at least adding a comment capturing the suggestion and starting with // Tracked by https://github.com/dotnet/roslyn/issues/76130 :

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in commit 5


foreach (var nestedType in type.GetTypeMembersUnordered())
foreach (var nestedType in type.GetTypeMembersUnordered())
{
if (nestedType.IsExtension)
{
if (nestedType.IsExtension)
{
extensions.Add(nestedType);
}
extensions.Add(nestedType);
}
}
}
Expand Down