Allow creating a body from Array[Byte]
#2581
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.
I was doing some profiling on a simple "Hello world" app for large byte payloads (~65kb), and one of the things that stood out was that there was a very big memory allocation due to the copying of the
Chunk[Byte]
into anArray[Byte]
(see screenshot at the bottom).I think that this allocation can be avoided in 1 of 2 ways:
Array[Byte]
fromChunk.Arr[Byte]
in theUnsafeBytes#unsafeAsArray
methodIn this PR, I went with (2) mostly because it eliminates the possibility of the array wrapped in a Chunk to be mutated. Having said that, (1) would not require changes in user's code to see the performance improvement so I'm happy to change this PR and go with option 1 instead.
I also added some extra benchmarks to showcase that besides the memory allocations, this change also improves the throughput of responses with large payloads. In the case of a 100kb payload, that's ~5% improvement in throughput. Note that the copying of the array happens prior to any compression taking place, so it's likely that many users will see some benefit from this change
Flamegraph: