-
Notifications
You must be signed in to change notification settings - Fork 196
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
@@ -10,13 +11,13 @@ | |
|
||
namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration; | ||
|
||
public sealed partial class CodeWriter | ||
public sealed partial class CodeWriter : IDisposable | ||
{ | ||
// This is the size of each "page", which are arrays of ReadOnlyMemory<char>. | ||
// This number was chosen arbitrarily as a "best guess". If changed, care should be | ||
// taken to ensure that pages are not allocated on the LOH. ReadOnlyMemory<char> | ||
// takes up 16 bytes, so a page size of 1000 is 16k. | ||
private const int PageSize = 1000; | ||
private const int MinimumPageSize = 1000; | ||
|
||
// Rather than using a StringBuilder, we maintain a linked list of pages, which are arrays | ||
// of "chunks of text", represented by ReadOnlyMemory<char>. This avoids copying strings | ||
|
@@ -65,17 +66,27 @@ private void AddTextChunk(ReadOnlyMemory<char> value) | |
} | ||
|
||
// If we're at the start of a page, we need to add the page first. | ||
var lastPage = _pageOffset == 0 | ||
? _pages.AddLast(new ReadOnlyMemory<char>[PageSize]).Value | ||
: _pages.Last!.Value; | ||
ReadOnlyMemory<char>[] lastPage; | ||
|
||
if (_pageOffset == 0) | ||
{ | ||
lastPage = ArrayPool<ReadOnlyMemory<char>>.Shared.Rent(MinimumPageSize); | ||
_pages.AddLast(lastPage); | ||
} | ||
else | ||
{ | ||
lastPage = _pages.Last!.Value; | ||
} | ||
|
||
// 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) | ||
// _pageOffset is checked against the lastPage.Length as the Rent call that | ||
// return that array may return an array longer that MinimumPageSize. | ||
if (_pageOffset == lastPage.Length) | ||
alexgav marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment added, hopefully more obvious now. |
||
{ | ||
_pageOffset = 0; | ||
} | ||
|
@@ -370,4 +381,14 @@ public string GenerateCode() | |
return result; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var page in _pages) | ||
{ | ||
ArrayPool<ReadOnlyMemory<char>>.Shared.Return(page, clearArray: true); | ||
} | ||
|
||
_pages.Clear(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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!