Improved, configurable buffer pools#87
Merged
unrolled merged 2 commits intounrolled:v1from Apr 20, 2021
nsmith5:nsmith/better-buffer-pool
Merged
Improved, configurable buffer pools#87unrolled merged 2 commits intounrolled:v1from nsmith5:nsmith/better-buffer-pool
unrolled merged 2 commits intounrolled:v1from
nsmith5:nsmith/better-buffer-pool
Conversation
unrolled
approved these changes
Apr 19, 2021
Owner
unrolled
left a comment
There was a problem hiding this comment.
This is awesome! Just one tiny comment to fix up. Thanks for tackling this issue 👍
Contributor
Author
|
Which comment @unrolled? I didn't see any comments on the PR just yet. |
unrolled
reviewed
Apr 19, 2021
Owner
|
Thanks!! |
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.
Problem
As discussed in #86 and #85, the current buffer pool leads to heap allocations that are 64 * the largest template rendered. This wastes memory. We cannot simply remove the buffer pool though because it is needed to check template errors before writing HTTP status codes.
Solution
Add a sized buffer pool implementation that garbage collects buffers that grow past a certain point. Use this sized buffer pool by default with some heuristically choosen defaults (I've selected 32 buffers of size 512KiB), but allow users to specify their own if needed. A
GenericBufferPoolinterface has been added that allows users to not only modify theSizedBufferPoolandBufferPoolimplementations, but to implement their own if need be.Results
Two benchmarks have been added:
BenchmarkBigHTMLBuffersandBenchmarkSmallHTMLBufferswhere the buffer pool buffers are larger and smaller than the rendered template respectively. As expected, the small buffers lead to more allocations and slower HTML templating than the larger buffers.Upon inspecting the memory profiles for each benchmark we see the difference in allocation is precisely where we think it should be: buffers are being allocated upon return to the pool because they've out grown our size limit
Buffer larger than templates
Buffer smaller than templates