Skip to content

Optimize GreenNode.ToString to not potentially not allocate when it has only a single non-null slot value.#12227

Merged
ToddGrun merged 5 commits intodotnet:mainfrom
ToddGrun:dev/toddgrun/Optimize_GreenNode_ToString
Sep 15, 2025
Merged

Optimize GreenNode.ToString to not potentially not allocate when it has only a single non-null slot value.#12227
ToddGrun merged 5 commits intodotnet:mainfrom
ToddGrun:dev/toddgrun/Optimize_GreenNode_ToString

Conversation

@ToddGrun
Copy link
Copy Markdown

@ToddGrun ToddGrun commented Sep 13, 2025

I see a lot of calls to this where the only non-null slot value is a token, which has a non-allocating ToString implementation.

The numbers from the test insertion were a little hard to decipher. The cohosting test has huge variances in general (which we should try to fix). Of the runs that didn't have the extremely high numbers, it looked like this change reduced allocations under GreenNode.ToString by about 50% while not adding any CPU cost (again, hard to read, but GetSingleSlotValueOrDefault looked like it was an insignificant CPU cost wrt the rest of the ToString impelementation).

Test insertion: https://dev.azure.com/devdiv/DevDiv/_git/VS/pullrequest/670195

@ToddGrun ToddGrun requested a review from a team as a code owner September 13, 2025 01:30
…as only a single non-null slot value.

I see a lot of calls to this where the only non-null slot value is a token, which has a non-allocating ToString implementation.
Copy link
Copy Markdown
Member

@davidwengier davidwengier left a comment

Choose a reason for hiding this comment

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

Makes sense to me. Probably wouldn't achieve any meaningful difference, but the thought did occur to me that for the most common candidates, would it make sense to just override ToString in the syntax class, and directly check the uncommonly filled in slots rather than looping, but its only a for loop so yeah.

@ToddGrun ToddGrun force-pushed the dev/toddgrun/Optimize_GreenNode_ToString branch from d5df7be to 8da68b3 Compare September 15, 2025 14:48
@ToddGrun
Copy link
Copy Markdown
Author

I can be convinced otherwise, but it seems to me it makes more sense to handle this generically, rather than needing each class to code their own ToString optimization. From what I could gather from the perf runs, GetSingleSlotValueOrDefault didn't seem to add much in the way of CPU costs.


In reply to: 3219295301

@ToddGrun ToddGrun merged commit f5cf25b into dotnet:main Sep 15, 2025
11 checks passed
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Sep 15, 2025
@akhera99 akhera99 modified the milestones: Next, 18.0 P1, 18.0 P2 Sep 22, 2025
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.

5 participants