Reuse buffers when marshaling messages in unary Connect protocol #561
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.
The
marshalAppender
interface was previously only used in the envelope writer. That means it was already reusing buffers from the pool for marshaling messages in all other protocols (Connect streaming, gRPC, and gRPC-Web); it was just missing from here.The envelope writer logic is much more complicated, where it tries to use a copy to save the allocation of a
bytes.Buffer
struct to wrap the[]byte
. I'm curious if that's been benchmarked and how big of a difference it really makes. I did not add the same logic here, but I can if we really think it's important. Do let me know!I was also looking through the implementation of
bytes.Buffer.Write
, which under the does acopy
between bytes slices. And in the case where marshalling did not have to increase the buffer capacity, the src and dest slices will actually be the same. So I dug into the runtime source code to see how it handles overlapping regions, hoping that maybe it's smart enough to also no-op when src == dest. It is not that smart. So it does indeed perform a copy of bytes even though it's effectively a no-op 🤷 ..