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

Make the impl of SyntaxNode.GetText more efficient #73512

Merged
merged 4 commits into from
May 16, 2024

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented May 16, 2024

The existing impl woudl always allocation into the LOH for large trees. This manifested as huge growth in the LOH for normal IDE scenarios (like 'fix all'):

image

With this change, we drop to:

image

--

Total char[] allocs also drops from:

image

To:

image

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 16, 2024
this.WriteTo(new StringWriter(builder));
return new StringBuilderText(builder, encoding, checksumAlgorithm);
var fullWidth = this.Green.FullWidth;
var writer = SourceTextWriter.Create(encoding, checksumAlgorithm, fullWidth);
Copy link
Member Author

Choose a reason for hiding this comment

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

this helper gets an appropriate writer depending on the length of the final source text. For <85k (the loh limit) you get a simple string writer. For >85k you get a chunking writer.

@@ -238,87 +236,6 @@ public static SourceText ReadFrom(ITextFactoryService textService, ObjectReader
return textService.CreateText(textReader, encoding, checksumAlgorithm, cancellationToken);
}

public static SourceText CreateSourceText(
Copy link
Member Author

Choose a reason for hiding this comment

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

IDE impl that can be entirely removed now that the compiler side is just smarter.

Copy link
Member Author

Choose a reason for hiding this comment

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

This also means we avoid the IDE making an entire copy of the node into our chunks, which then the compiler makes a copy of when making its LargeText instance itself.

@@ -48,8 +48,7 @@ public override SourceText GetText(CancellationToken cancellationToken)
{
if (_lazyText == null)
{
var sourceText = SourceTextExtensions.CreateSourceText(GetRoot(cancellationToken), Encoding, _checksumAlgorithm, cancellationToken);
Interlocked.CompareExchange(ref _lazyText, sourceText, null);
Interlocked.CompareExchange(ref _lazyText, GetRoot(cancellationToken).GetText(Encoding, _checksumAlgorithm), null);
Copy link
Member Author

Choose a reason for hiding this comment

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

can just delegate to compiler, now that it is not inefficient.

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review May 16, 2024 17:46
@CyrusNajmabadi CyrusNajmabadi requested review from a team as code owners May 16, 2024 17:46
@CyrusNajmabadi
Copy link
Member Author

@dotnet/roslyn-compiler for review. This is a tiny change in the impl here, with a big net win for IDE scenarios. BIG :D

@CyrusNajmabadi CyrusNajmabadi requested a review from ToddGrun May 16, 2024 17:47
Copy link
Contributor

@RikkiGibson RikkiGibson left a comment

Choose a reason for hiding this comment

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

changes under compilers lgtm

@CyrusNajmabadi CyrusNajmabadi enabled auto-merge May 16, 2024 18:01
Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@CyrusNajmabadi CyrusNajmabadi merged commit 20a93e1 into dotnet:main May 16, 2024
28 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the nodeToText2 branch May 16, 2024 19:16
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone May 16, 2024
@Cosifne Cosifne modified the milestones: Next, 17.11 P2 May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants