proto: avoid deep defensive copy when marshaling #1480
Closed
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 following pattern is oft-repeated:
Notably it appears in both
transport.WriteStatus
implementations. Theearlier revision with
proto.Clone()
is unnecessary for the immediatemarshaling as the proto message is not mutated. The inner calls to the
reflect
package shows up on CPU profiles in addition to a tinyallocation cost that can be easily avoided.
An alternate
Status.ProtoClone()
method is provided for when a deepcopy is in fact necessary, namely when the proto message is the be
subsequently mutated. One such example of this is
Status.WithDetails
where the embedded proto is annotated with additional detail messages.
NB: This is a breaking API change if imports depend on this specific
behaviour. Alternatively another accessor can be added.