Skip to content

Improve GreenNode.ToString() implementation for efficiency#12344

Merged
DustinCampbell merged 9 commits into
dotnet:mainfrom
DustinCampbell:improve-greennode-tostring
Oct 20, 2025
Merged

Improve GreenNode.ToString() implementation for efficiency#12344
DustinCampbell merged 9 commits into
dotnet:mainfrom
DustinCampbell:improve-greennode-tostring

Conversation

@DustinCampbell

@DustinCampbell DustinCampbell commented Oct 16, 2025

Copy link
Copy Markdown
Member

Tip

I recommend reviewing this PR commit-by-commit. Each commit represents a concrete change and includes a description.

The key improvement in this change is to take advantage of the fact that a green node's width is the same as the length of the string produced by ToString(). So, we can use the width to call string.Create(...) and efficiently create the string rather than building it up with a StringWriter.

Using a StringWriter requires all of the characters to first be copied into the writer's underlying StringBuilder and then copy them again when creating the final string. We can avoid this extra copying by using string.Create(...) and copying the characters directly into the final string. The end result is a simpler implementation that avoids the need for a StringWriter pool.


CI Build: https://dev.azure.com/dnceng/internal/_build/results?buildId=2817951&view=results
Test Insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/680209
Toolset Run: https://dev.azure.com/dnceng/internal/_build/results?buildId=2817952&view=results

- Remove #nullable disable and add a couple of annotations.
- Convert a couple of properties to expression-bodied properties.
- Update ChildSyntaxList and SyntaxNode to account for nullability.
- Remove #nullable disable and add a couple of annotations.
- Add IsEmpty property to MemoryBuilder<T>
- Add Push(), Peek(), Pop, and TryPop() methods to MemoryBuilder<T>
- Change AsMemory() to return a Memory<T> rather than a ReadOnlyMemory<T>.
- Mark indexer as read only.
- Add unit tests.
Add GreenNode.GetEnumerator() method that returns a ref struct-based Enumerator to perform a depth-first traversal using the target GreenNode as the root. SyntaxList nodes are transparent and not returned from the enumerator.

Unit tests are included along with some helpers to make constructing GreenNodes a bit easier. The helpers include:

- A handful of SyntaxFactory methods to avoid passing extra arguments.
- A [CollectionBuilder] attribute for SyntaxList<TNode>
- SyntaxList.Create(...) methods for the [CollectionBuilder] to target.
Add GreenNode.Tokens() method that returns a TokenEnumerable, a ref struct that provides a GetEnumeartor() that returns a ref struct-based Enumerator to perform a depth-first traversal that returns SyntaxToken instances. Unit tests for Tokens() and TokenEnumerable.GetEnumerator() have been added.
This change removes the StringWriter machinery from GreenNode. It only adds overhead. In its place is a more efficient implementation using string.Create. This avoids copying the characters into a StringWriter and then allocating a new string from the writer, which copies all of the characters *again* into a new string.

Because we know the width of a GreenNode, we know the length of the string it will produce.
string.Build(...) collects "chunks" of text as ReadOnlyMemory<char>s before creating a string from them. This ensures that characters are not copied to a buffer and copied again when allocating the final string.
@DustinCampbell DustinCampbell requested review from a team and ToddGrun October 16, 2025 18:27
@DustinCampbell DustinCampbell requested a review from a team as a code owner October 16, 2025 18:27
1. GetDebuggerDisplay should just return an interpolated string. (Oops!)
2. ToString should implement two specific optimizations to avoid allocating a new string unless necessary.
  - If the width is zero, return string.Empty.
  - If there is just a single non-zero-width token with the same width as the node, just return that token's content.

@ToddGrun ToddGrun left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

:shipit:

@DustinCampbell

Copy link
Copy Markdown
Member Author

@chsienki: I'm going to go ahead and merge this compiler. Please let me know if you have any concerns.

@davidwengier davidwengier modified the milestones: Next, 18.3 Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants