Improved, configurable buffer pools #87
Merged
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
GenericBufferPool
interface has been added that allows users to not only modify theSizedBufferPool
andBufferPool
implementations, but to implement their own if need be.Results
Two benchmarks have been added:
BenchmarkBigHTMLBuffers
andBenchmarkSmallHTMLBuffers
where 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