From 8dfa92bbbcb1346362726b2930a79871dc5d95ab Mon Sep 17 00:00:00 2001 From: Evan Jones Date: Thu, 20 Jan 2022 17:21:29 -0500 Subject: [PATCH] ddtrace/tracer.Measured(): Cache a global instance (save 1 alloc/call) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This saves one allocation for every call to this function. This function is called in about 26 places in contrib/..., which includes some fairly hot code paths, such as in the gRPC interceptors. The gRPC interceptor microbenchmark shows this saves about ~8% CPU, and reduces one allocation per call. This is part of my attempt to reduce the overhead of gRPC tracing. name old time/op new time/op delta UnaryServerInterceptor/ok_no_metadata-8 5.33µs ± 8% 4.94µs ± 5% -7.17% (p=0.002 n=9+10) UnaryServerInterceptor/ok_with_metadata_no_parent-8 6.44µs ± 7% 5.79µs ± 3% -10.19% (p=0.000 n=9+9) UnaryServerInterceptor/ok_with_metadata_with_parent-8 5.52µs ± 9% 5.05µs ± 3% -8.47% (p=0.000 n=10+9) UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8 5.61µs ±10% 5.15µs ± 3% -8.06% (p=0.007 n=10+10) UnaryServerInterceptor/error_no_metadata-8 13.3µs ±15% 11.7µs ± 0% -12.16% (p=0.007 n=10+6) name old alloc/op new alloc/op delta UnaryServerInterceptor/ok_no_metadata-8 4.94kB ± 0% 4.89kB ± 0% -1.00% (p=0.000 n=9+9) UnaryServerInterceptor/ok_with_metadata_no_parent-8 5.57kB ± 1% 5.52kB ± 0% -0.88% (p=0.000 n=10+9) UnaryServerInterceptor/ok_with_metadata_with_parent-8 4.66kB ± 0% 4.62kB ± 0% -0.97% (p=0.000 n=10+9) UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8 5.14kB ± 0% 5.09kB ± 1% -0.94% (p=0.000 n=10+10) UnaryServerInterceptor/error_no_metadata-8 11.3kB ± 0% 11.2kB ± 0% -0.33% (p=0.004 n=10+10) name old allocs/op new allocs/op delta UnaryServerInterceptor/ok_no_metadata-8 52.0 ± 0% 51.0 ± 0% -1.92% (p=0.000 n=10+10) UnaryServerInterceptor/ok_with_metadata_no_parent-8 57.0 ± 0% 56.0 ± 0% -1.75% (p=0.000 n=10+10) UnaryServerInterceptor/ok_with_metadata_with_parent-8 48.0 ± 0% 47.0 ± 0% -2.08% (p=0.000 n=10+10) UnaryServerInterceptor/ok_no_metadata_with_analytics_rate-8 54.0 ± 0% 53.0 ± 0% -1.85% (p=0.000 n=10+10) UnaryServerInterceptor/error_no_metadata-8 69.0 ± 0% 68.0 ± 0% -1.45% (p=0.000 n=10+10) --- ddtrace/tracer/option.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ddtrace/tracer/option.go b/ddtrace/tracer/option.go index 2dc38796ad..adf229dcde 100644 --- a/ddtrace/tracer/option.go +++ b/ddtrace/tracer/option.go @@ -741,9 +741,12 @@ func SpanType(name string) StartSpanOption { return Tag(ext.SpanType, name) } +var measuredTag = Tag(keyMeasured, 1) + // Measured marks this span to be measured for metrics and stats calculations. func Measured() StartSpanOption { - return Tag(keyMeasured, 1) + // cache a global instance of this tag: saves one alloc/call + return measuredTag } // WithSpanID sets the SpanID on the started span, instead of using a random number.