Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/transport: optimize grpc-message encoding/decoding #5654

Merged
merged 1 commit into from
Oct 4, 2022
Merged

internal/transport: optimize grpc-message encoding/decoding #5654

merged 1 commit into from
Oct 4, 2022

Conversation

tklauser
Copy link
Contributor

@tklauser tklauser commented Sep 14, 2022

Use strings.Builder (introduced in Go 1.10) instead of bytes.Buffer. Also call fmt.Fprintf directly on the builder. This improves performance both in terms of CPU and memory consumption:

name                 old time/op    new time/op    delta
DecodeGrpcMessage-8     345ns ± 5%     225ns ± 4%  -34.56%  (p=0.000 n=20+20)
EncodeGrpcMessage-8    1.33µs ± 4%    1.26µs ± 3%   -5.76%  (p=0.000 n=20+20)

name                 old alloc/op   new alloc/op   delta
DecodeGrpcMessage-8     80.0B ± 0%     24.0B ± 0%  -70.00%  (p=0.000 n=20+20)
EncodeGrpcMessage-8      115B ± 0%       88B ± 0%  -23.48%  (p=0.000 n=20+20)

name                 old allocs/op  new allocs/op  delta
DecodeGrpcMessage-8      2.00 ± 0%      2.00 ± 0%     ~     (all equal)
EncodeGrpcMessage-8      8.00 ± 0%      4.00 ± 0%  -50.00%  (p=0.000 n=20+20)

RELEASE NOTES: none

Use strings.Builder (introduced in Go 1.10) instead of bytes.Buffer.
Also call fmt.Fprintf directly on the builder. This improves performance
both in terms of CPU and memory consumption:

name                 old time/op    new time/op    delta
DecodeGrpcMessage-8     345ns ± 5%     225ns ± 4%  -34.56%  (p=0.000 n=20+20)
EncodeGrpcMessage-8    1.33µs ± 4%    1.26µs ± 3%   -5.76%  (p=0.000 n=20+20)

name                 old alloc/op   new alloc/op   delta
DecodeGrpcMessage-8     80.0B ± 0%     24.0B ± 0%  -70.00%  (p=0.000 n=20+20)
EncodeGrpcMessage-8      115B ± 0%       88B ± 0%  -23.48%  (p=0.000 n=20+20)

name                 old allocs/op  new allocs/op  delta
DecodeGrpcMessage-8      2.00 ± 0%      2.00 ± 0%     ~     (all equal)
EncodeGrpcMessage-8      8.00 ± 0%      4.00 ± 0%  -50.00%  (p=0.000 n=20+20)
@dfawley dfawley added the Type: Performance Performance improvements (CPU, network, memory, etc) label Sep 21, 2022
@dfawley dfawley added this to the 1.50 Release milestone Sep 21, 2022
@dfawley dfawley self-assigned this Sep 21, 2022
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a micro-optimization but it's no more complicated than the existing code and seems to be more of a best practice than the bytes.Buffer so LGTM. Thanks for the PR!

@dfawley dfawley assigned zasweq and unassigned dfawley Sep 21, 2022
@dfawley dfawley requested a review from zasweq September 21, 2022 16:45
Copy link
Contributor

@zasweq zasweq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@zasweq zasweq merged commit 1451c62 into grpc:master Oct 4, 2022
@tklauser tklauser deleted the encode-decode-optimize branch October 4, 2022 18:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants