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

contrib/google.golang.org/grpc: Reduce allocs in interceptor #1183

Merged
merged 2 commits into from Mar 8, 2022

Conversation

evanj
Copy link
Contributor

@evanj evanj commented Feb 28, 2022

finishWithError needs to allocate a []tracer.FinishOption slice to
pass arguments to span.Finish. However, those arguments are only used
if err != nil. Only allocate the slice if it is used. Allocate the
exactly right size of slice. This appears to remove 3 allocations in
the "no error" case, and 1 allocation if the NoDebugStack() option is
specified. In the case of an error, there is no difference. This may
also make a small difference in the run time. The benchstat results
from my Mac laptop are below.

name                                                         old time/op    new time/op    delta
UnaryServerInterceptor/ok_no_metadata-8                        5.33µs ± 8%    4.73µs ± 5%  -11.27%  (p=0.000 n=9+9)
UnaryServerInterceptor/ok_with_metadata_no_parent-8            6.44µs ± 7%    5.71µs ±13%  -11.41%  (p=0.001 n=9+10)
UnaryServerInterceptor/ok_with_metadata_with_parent-8          5.52µs ± 9%    5.03µs ± 8%   -8.94%  (p=0.002 n=10+9)
UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8    5.61µs ±10%    4.94µs ± 3%  -11.89%  (p=0.000 n=10+10)
UnaryServerInterceptor/error_no_metadata-8                     13.3µs ±15%    11.6µs ± 1%  -12.90%  (p=0.005 n=10+7)

name                                                         old alloc/op   new alloc/op   delta
UnaryServerInterceptor/ok_no_metadata-8                        4.94kB ± 0%    4.85kB ± 0%   -1.87%  (p=0.000 n=9+7)
UnaryServerInterceptor/ok_with_metadata_no_parent-8            5.57kB ± 1%    5.46kB ± 0%   -1.91%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_with_metadata_with_parent-8          4.66kB ± 0%    4.57kB ± 0%   -2.05%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8    5.14kB ± 0%    5.05kB ± 1%   -1.78%  (p=0.000 n=10+10)
UnaryServerInterceptor/error_no_metadata-8                     11.3kB ± 0%    11.3kB ± 0%     ~     (p=0.066 n=10+8)

name                                                         old allocs/op  new allocs/op  delta
UnaryServerInterceptor/ok_no_metadata-8                          52.0 ± 0%      49.0 ± 0%   -5.77%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_with_metadata_no_parent-8              57.0 ± 0%      54.0 ± 0%   -5.26%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_with_metadata_with_parent-8            48.0 ± 0%      45.0 ± 0%   -6.25%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8      54.0 ± 0%      51.0 ± 0%   -5.56%  (p=0.000 n=10+10)
UnaryServerInterceptor/error_no_metadata-8                       69.0 ± 0%      69.0 ± 0%     ~     (all equal)

finishWithError needs to allocate a []tracer.FinishOption slice to
pass arguments to span.Finish. However, those arguments are only used
if err != nil. Only allocate the slice if it is used. Allocate the
exactly right size of slice. This appears to remove 3 allocations in
the "no error" case, and 1 allocation if the NoDebugStack() option is
specified. In the case of an error, there is no difference. This may
also make a small difference in the run time. The benchstat results
from my Mac laptop are below.

name                                                         old time/op    new time/op    delta
UnaryServerInterceptor/ok_no_metadata-8                        5.33µs ± 8%    4.73µs ± 5%  -11.27%  (p=0.000 n=9+9)
UnaryServerInterceptor/ok_with_metadata_no_parent-8            6.44µs ± 7%    5.71µs ±13%  -11.41%  (p=0.001 n=9+10)
UnaryServerInterceptor/ok_with_metadata_with_parent-8          5.52µs ± 9%    5.03µs ± 8%   -8.94%  (p=0.002 n=10+9)
UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8    5.61µs ±10%    4.94µs ± 3%  -11.89%  (p=0.000 n=10+10)
UnaryServerInterceptor/error_no_metadata-8                     13.3µs ±15%    11.6µs ± 1%  -12.90%  (p=0.005 n=10+7)

name                                                         old alloc/op   new alloc/op   delta
UnaryServerInterceptor/ok_no_metadata-8                        4.94kB ± 0%    4.85kB ± 0%   -1.87%  (p=0.000 n=9+7)
UnaryServerInterceptor/ok_with_metadata_no_parent-8            5.57kB ± 1%    5.46kB ± 0%   -1.91%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_with_metadata_with_parent-8          4.66kB ± 0%    4.57kB ± 0%   -2.05%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8    5.14kB ± 0%    5.05kB ± 1%   -1.78%  (p=0.000 n=10+10)
UnaryServerInterceptor/error_no_metadata-8                     11.3kB ± 0%    11.3kB ± 0%     ~     (p=0.066 n=10+8)

name                                                         old allocs/op  new allocs/op  delta
UnaryServerInterceptor/ok_no_metadata-8                          52.0 ± 0%      49.0 ± 0%   -5.77%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_with_metadata_no_parent-8              57.0 ± 0%      54.0 ± 0%   -5.26%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_with_metadata_with_parent-8            48.0 ± 0%      45.0 ± 0%   -6.25%  (p=0.000 n=10+10)
UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8      54.0 ± 0%      51.0 ± 0%   -5.56%  (p=0.000 n=10+10)
UnaryServerInterceptor/error_no_metadata-8                       69.0 ± 0%      69.0 ± 0%     ~     (all equal)
@evanj evanj requested a review from a team February 28, 2022 14:13
@Julio-Guerra Julio-Guerra added this to the 1.37.0 milestone Mar 8, 2022
@Julio-Guerra Julio-Guerra merged commit 7c11ba8 into v1 Mar 8, 2022
@Julio-Guerra Julio-Guerra deleted the evan.jones/allocs-finish branch March 8, 2022 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants