Override and short-circuit Chunk.flatten #2990
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.
Override and short-circuit
Chunk.flatten
method. This should help outhttp4s
when parsing JSON bodies via circe.Currently JSON is parsed via the follow method:
https://github.com/http4s/http4s/blob/fbcbcf3e23031a5ebba3c8f24b3486f0f5d956a8/circe/src/main/scala/org/http4s/circe/CirceInstances.scala#L55
Which calls
https://github.com/http4s/http4s/blob/fbcbcf3e23031a5ebba3c8f24b3486f0f5d956a8/core/shared/src/main/scala/org/http4s/EntityDecoder.scala#L208
The latter method calls
Chunk.flatten
.From what I can tell (this one is for http4s people), most requests (depending on size) will contain one chunk, so the short-circuit in this PR should reduce allocations and reduce CPU usage quite a lot.
A brief jmh benchmark shows this obviously has a big impact, as now
flatten
in certain cases isO(1)
in both time and space. Note I've only tested two size: a chunk of 1 chunk, and a chunk of 5 chunks.before
after