Skip to content

Reduce allocations in DefaultTagHelperTargetExtension.WriteTagHelperProperty#12361

Merged
ToddGrun merged 2 commits into
dotnet:mainfrom
ToddGrun:dev/toddgrun/WriteTagHelperProperty_Allocations
Oct 17, 2025
Merged

Reduce allocations in DefaultTagHelperTargetExtension.WriteTagHelperProperty#12361
ToddGrun merged 2 commits into
dotnet:mainfrom
ToddGrun:dev/toddgrun/WriteTagHelperProperty_Allocations

Conversation

@ToddGrun

Copy link
Copy Markdown

This method was building up strings via calls to GetPropertyAccessor to just write them into the code writer.

Instead, we can just write the string components into the code writer directly, avoiding string concats that were about 0.4% of total allocations during typing in the RazorEditingTests.ScrollingAndTypingInCohosting speedometer test.

image

…roperty

This method was building up strings via calls to GetPropertyAccessor to just write them into the code writer.

Instead, we can just write the string components into the code writer directly, avoiding string concats that were about 0.4% of total allocations during typing in the RazorEditingTests.ScrollingAndTypingInCohosting speedometer test.
@ToddGrun ToddGrun requested a review from a team as a code owner October 17, 2025 20:58

@DustinCampbell DustinCampbell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that CodeWriter.Write(...) has an overload that takes an interpolated string handler that can take lots of different values, including ReadOnlyMemory<char>. I added a few suggestions, but I'm sure you can find a lot more here to clean up and improve.

}

return propertyAccessor;
return writer;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure if write has a position property that could be used for it, but might be worth adding a Debug.Assert here to ensure that the method has written the same number of characters as GetPropertyAccessorLength returns, just to ensure nothing accidentally gets the two methods out of sync.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't think that would work, as the write method may add indentation at it's whim

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ahh, fair enough

@ToddGrun

Copy link
Copy Markdown
Author

I'm sure you can find a lot more here to clean up and improve.

Cleaned up the ones that stood out

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