Reduce string allocations in ParseUsingDeclaration by replacing LINQ + Concat(this WorkspaceEdit, WorkspaceEdit?) with StringBuilder loop#12774
Merged
davidwengier merged 3 commits intodotnet:mainfrom Feb 13, 2026
Conversation
…+ Concat(this WorkspaceEdit, WorkspaceEdit?) with StringBuilder loop
ToddGrun
reviewed
Feb 12, 2026
ToddGrun
reviewed
Feb 12, 2026
davidwengier
approved these changes
Feb 13, 2026
Member
|
Thanks @nareshjo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI-Generated Pull Request 🤖
This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights
Issue:
ParseUsingDeclarationbuilds two content strings by chaining.Skip(),.Where(),.Select()LINQ extensions over aSyntaxToken[]and passing the results tostring.Concat(IEnumerable<string>). Eachstring.Concatcall internally allocates aStringBuilder, iterates through multiple LINQ enumerator objects (one per.Skip(),.Where(),.Select()), and callsStringBuilder.ToString()to allocate the final string. This creates 4+ enumerator objects and 2 intermediateStringBuilderinstances per@usingdirective parse — a hot path triggered on every keystroke.This matches the allocation stack flow showing
ParseMarkupNodes→ParseCodeTransition→OtherParserBlock→ParseBlock→TryParseKeyword→ParseUsingKeyword→ParseUsingDeclaration→String.Concat→StringBuilder.ToString→FramedAllocateString→TypeAllocated!System.StringIssue type: Eliminate unnecessary LINQ enumerator and
string.Concatallocations in hot pathProposed fix: Replace the two LINQ chains (
.Skip().Where().Select()) +string.Concat(IEnumerable<string>)calls with a singleforloop over the already-snapshottedSyntaxToken[]array, appending to two plainStringBuilderinstances. TheTokenBuilder.ToList().Nodessnapshot is kept at its original position (beforeTryAcceptmutatesTokenBuilder) to preserve exact functional parity. This eliminates 4+ LINQ enumerator allocations and avoidsstring.Concat's internalStringBuilderallocation, while producing identical output strings.Best practices wiki
See related failure in PRISM
ADO work item