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

Pool CodeWriter ReadOnlyMemory<char> pages #10585

Merged
merged 3 commits into from
Jul 16, 2024

Conversation

ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Jul 4, 2024

These allocations are present in a customer trace I'm looking at, accounting for 1.4% of allocations in the VS process (around 100 MB).

The owner of the CodeWriter is already disposable, so making the CodeWriter disposable is trivial, and allows for all ReadOnlyMemory pages added to _pages to be released back to a pool.

*** Customer trace ***
image

*** local trace WITHOUT changes opening OrchardCore ***
image

*** local trace WITH changes opening OrchardCore ***
image

These allocations are present in a customer trace I'm looking at, accounting for 1.4% of allocations in the VS process (around 100 MB).

The owner of the CodeWriter is already disposable, so making the CodeWriter disposable is trivial, and allows for all ReadOnlyMemory<char> pages added to _pages to be released back to a pool.
@ToddGrun ToddGrun requested review from a team as code owners July 4, 2024 17:00
Code review feedback: Move CodeWriter ctor to make code cleaner
@ToddGrun
Copy link
Contributor Author

ToddGrun commented Jul 5, 2024

@dotnet/razor-compiler -- ptal

Copy link
Member

Choose a reason for hiding this comment

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

Did you go through all CodeWriter type references to make sure we are disposing CodeWriter instances now? It looks like RazorHtmlWriter is owning a CodeWriter but not disposing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just went through and in addition to the RazorHtmlWriter usage, there were a bunch of test file usages that I changed too. Thanks!


if (_pageOffset == 0)
{
lastPage = ArrayPool<ReadOnlyMemory<char>>.Shared.Rent(PageSize);
Copy link
Member

Choose a reason for hiding this comment

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

Consider renaming PageSize to MinimumPageSize to signal that the page length might not be be the same as the requested size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


// Add our chunk of text (the ReadOnlyMemory<char>) and increment the offset.
lastPage[_pageOffset] = value;
_pageOffset++;

// We've reached the end of a page, so we reset the offset to 0.
// This will cause a new page to be added next time.
if (_pageOffset == PageSize)
if (_pageOffset == lastPage.Length)
Copy link
Member

Choose a reason for hiding this comment

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

Good change, though it's a bit subtle. Could you add a comment or two describing this behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added, hopefully more obvious now.

@jjonescz jjonescz added the area-compiler Umbrella for all compiler issues label Jul 9, 2024
2) Rename PageSize => MinimumPageSize
3) Add comment around subtle comparison
Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

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

Looks good to me, though you'll need another sign off from razor-compiler.

@ToddGrun
Copy link
Contributor Author

@jjonescz -- any remaining concerns / requests?

@ToddGrun ToddGrun merged commit 59362df into main Jul 16, 2024
12 checks passed
@ToddGrun ToddGrun deleted the dev/toddgrun/PoolCodeWriterPages branch July 16, 2024 16:23
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jul 16, 2024
@RikkiGibson RikkiGibson modified the milestones: Next, 17.12 Preview 1 Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants