Skip to content

Reduce allocations in AbstractMoveToNamespaceService#82837

Draft
olegtk wants to merge 1 commit intodotnet:mainfrom
olegtk:dev/olegtk/reduceAllocationsForColorizedGhostText
Draft

Reduce allocations in AbstractMoveToNamespaceService#82837
olegtk wants to merge 1 commit intodotnet:mainfrom
olegtk:dev/olegtk/reduceAllocationsForColorizedGhostText

Conversation

@olegtk
Copy link
Contributor

@olegtk olegtk commented Mar 18, 2026

*** Not ready for review until perf numbers come back ***

Reduce allocations in AbstractMoveToNamespaceService that appear in CopilotCompletions.NextSuggestion Speedometer test.

image
Microsoft Reviewers: Open in CodeFlow

@dotnet-policy-service dotnet-policy-service bot added Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode labels Mar 18, 2026
@olegtk
Copy link
Contributor Author

olegtk commented Mar 18, 2026

/pr-val

@github-actions
Copy link
Contributor

You do not have permission to trigger this workflow. Only Microsoft employees who are contributors to the roslyn repository can run pipelines.

@github-actions
Copy link
Contributor

Failed to trigger the pipeline. Please check the workflow logs for details.

@olegtk
Copy link
Contributor Author

olegtk commented Mar 18, 2026

/pr-val

@github-actions
Copy link
Contributor

You do not have permission to trigger this workflow. Only Microsoft employees who are contributors to the roslyn repository can run pipelines.

@github-actions
Copy link
Contributor

Failed to trigger the pipeline. Please check the workflow logs for details.

@ToddGrun
Copy link
Contributor

/pr-val

@github-actions
Copy link
Contributor

This PR is from an external author. You must specify a commit hash to trigger this workflow. Please use the format /dart <commit-hash> or /pr-val <commit-hash>.

@github-actions
Copy link
Contributor

Failed to trigger the pipeline. Please check the workflow logs for details.

@ToddGrun
Copy link
Contributor

/pr-val 1f8f220

@github-actions
Copy link
Contributor

View PR Validation Run triggered by @ToddGrun

Parameters
  • Validation Type: pr-val
  • Pipeline ID: 8972
  • Pipeline Version: main
  • PR Number: 82837
  • Commit SHA: 1f8f220593c9cf86744eb1c9f335b1e4dd7cb67b
  • Source Branch: dev/olegtk/reduceAllocationsForColorizedGhostText
  • Target Branch: main
  • Build ID: 13593951

=> node.AncestorsAndSelf().OfType<TNamespaceDeclarationSyntax>().Count() + node.DescendantNodes().OfType<TNamespaceDeclarationSyntax>().Count();
{
var count = GetAncestorNamespaceCount(node);
foreach (var descendant in node.DescendantNodes())
Copy link
Member

Choose a reason for hiding this comment

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

This still seems quite expensive if we're just trying to count the number of nodes, since we'll still realize all sorts of red nodes, right? Or put another way I'm not worried about the LINQ here but rather this algorithm just seems not great.

Copy link
Contributor

Choose a reason for hiding this comment

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

This almost fits in with Cyrus's PR, if this could send in a greenfilter that would verify the greennode is of an appropriate type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which PR, @ToddGrun ?

Copy link
Contributor

Choose a reason for hiding this comment

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

result.Add(GetQualifiedName(ns));
}

return result.ToImmutableAndClear();
Copy link
Contributor

Choose a reason for hiding this comment

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

        return compilation.GlobalNamespace.GetAllNamespaces(cancellationToken)
            .SelectAsArray(
                predicate: n => n.NamespaceKind == NamespaceKind.Module && n.ContainingAssembly == compilation.Assembly,
                selector: GetQualifiedName);

bonus points if you add an overload that takes in an arg and make the predicate static

/// <summary>
/// Counts namespace declarations in ancestor nodes (including self) only.
/// This avoids the expensive descendant traversal when the node is inside a type
/// declaration (which cannot contain namespace declarations).
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not clear on this comment, seems like something that is commenting the caller's behavior?


private static bool ContainsMultipleTypesInSpine(SyntaxNode node)
=> node.AncestorsAndSelf().OfType<TNamedTypeDeclarationSyntax>().Count() > 1;
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This is very unlikely to be expensive

/// This avoids the expensive descendant traversal when the node is inside a type
/// declaration (which cannot contain namespace declarations).
/// </summary>
private static int GetAncestorNamespaceCount(SyntaxNode node)
Copy link
Contributor

Choose a reason for hiding this comment

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

Using linq here is very unlikely to be expensive

=> node.AncestorsAndSelf().OfType<TNamespaceDeclarationSyntax>().Count() + node.DescendantNodes().OfType<TNamespaceDeclarationSyntax>().Count();
{
var count = GetAncestorNamespaceCount(node);
foreach (var descendant in node.DescendantNodes())
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem seems to be here. We just need to walk down namespaces. This should bottom out almost immediately without a full walk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants