Remove buffer copy when writing base64 JSON value #97687
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.
Fix #97628
Benchmark results
I used the
dotnet-performancebenchmarks with*WriteJson<BinaryData>*as the filter.Results before
Results After
Profiler results
While the benchmark so some speedup, they don't show any improvements in allocations. Either because the buffers are too small that they're allocated on the stack, or, since the buffer are rented, subsequent writes will likely reuse the same buffer and it probably won't show up in the out-of-process benchmark results. To demonstrate the allocations improvements, I created a sample repo that performs concurrent: https://github.com/habbes/experiments/tree/master/WriteBase64JsonMemoryRepro
CPU usage before
Span<byte>.CopyTo()andBuffer._Memmove()contribute to a relative high % of CPU useCPU usage after
No traces of the buffer copies
Allocations before
Several large
byte[]allocations fromSharedArrayPool<byte>.RentunderBase64EncodeAndWrite.Allocations after
No traces of
Base64EncodeAndWritein allocations