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

Reduce Linq usage in SymbolDisplayVisitor.CreateAliasMap #73793

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from 1 commit
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 @@ -196,26 +196,39 @@ private IDictionary<INamespaceOrTypeSymbol, IAliasSymbol> CreateAliasMap()
// NOTE(cyrusn): If we're currently in a block of usings, then we want to collect the
// aliases that are higher up than this block. Using aliases declared in a block of
// usings are not usable from within that same block.
var usingDirective = GetAncestorOrThis<UsingDirectiveSyntax>(startNode);
if (usingDirective != null)
while (startNode != null)
Copy link
Contributor

Choose a reason for hiding this comment

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

while (startNode != null)

It looks like, if we are not in in a using directive, we will traverse syntax all the way to the top in this loop, and then restart reversion from the beginning looking for a namespace declaration or a compilation unit node. Instead, we could also stop once we reached a BaseNamespaceDeclarationSyntax or CompilationUnitSyntax node. Then the next loop could continue from the current startNode instead of resetting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'd prefer to pass on this unless you feel strongly. It seems like the current code is authentic to the original implementation, and making the change that you suggested will complicate things slightly. If this method shows up in CPU profiles, then this suggestion sounds like a good idea to pursue.

Copy link
Contributor

@AlekseyTs AlekseyTs May 31, 2024

Choose a reason for hiding this comment

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

while (startNode != null)

It would be good to add a comment explaining that with this loop we are trying to get out of immediately enclosing using directive, if any. #Closed

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 2

{
startNode = usingDirective.Parent!.Parent!;
if (startNode is UsingDirectiveSyntax)
{
startNode = startNode.Parent!.Parent!;
break;
}

startNode = startNode.Parent;
}

var usingAliases = GetAncestorsOrThis<BaseNamespaceDeclarationSyntax>(startNode)
.SelectMany(n => n.Usings)
.Concat(GetAncestorsOrThis<CompilationUnitSyntax>(startNode).SelectMany(c => c.Usings))
.Where(u => u.Alias != null)
.Select(u => semanticModel.GetDeclaredSymbol(u) as IAliasSymbol)
.WhereNotNull();
startNode ??= token.Parent;

var builder = ImmutableDictionary.CreateBuilder<INamespaceOrTypeSymbol, IAliasSymbol>();
foreach (var alias in usingAliases)
while (startNode != null)
{
if (!builder.ContainsKey(alias.Target))
var usings = (startNode as BaseNamespaceDeclarationSyntax)?.Usings;
usings ??= (startNode as CompilationUnitSyntax)?.Usings;

if (usings != null)
{
builder.Add(alias.Target, alias);
foreach (var u in usings)
{
if (u.Alias != null
&& semanticModel.GetDeclaredSymbol(u) is IAliasSymbol aliasSymbol
&& !builder.ContainsKey(aliasSymbol.Target))
{
builder.Add(aliasSymbol.Target, aliasSymbol);
}
}
}

startNode = startNode.Parent;
}

return builder.ToImmutable();
Expand Down Expand Up @@ -287,18 +300,6 @@ private string RemoveAttributeSuffixIfNecessary(INamedTypeSymbol symbol, string
return symbolName;
}

private static T? GetAncestorOrThis<T>(SyntaxNode node) where T : SyntaxNode
{
return GetAncestorsOrThis<T>(node).FirstOrDefault();
}

private static IEnumerable<T> GetAncestorsOrThis<T>(SyntaxNode node) where T : SyntaxNode
{
return node == null
? SpecializedCollections.EmptyEnumerable<T>()
: node.AncestorsAndSelf().OfType<T>();
}

private IDictionary<INamespaceOrTypeSymbol, IAliasSymbol> AliasMap
{
get
Expand Down