DataDog / datadog-go

go dogstatsd client library for datadog
MIT License
353 stars 132 forks source link

aggregator: don't allocate memory if we don't have tags #297

Closed iksaif closed 11 months ago

iksaif commented 11 months ago

Before: BenchmarkGetContextNoTags-10 39929401 29.70 ns/op 16 B/op 1 allocs/op

After: BenchmarkGetContextNoTags-10 419886585 2.902 ns/op 0 B/op 0 allocs/op

This might look not much, but this makes a lot of difference in term of allocation when a counter is called repeatedly inside a loop.

This also fixes the other benchmarks that I wanted to run to validate I didn't cause regression but where not able to run properly

We can see it in this profile (referencing line 182 explicitely):

Screenshot 2023-12-15 at 11 52 49

Referencing https://github.com/DataDog/datadog-go/blob/master/statsd/aggregator.go#L182 as the main cause of allocation

From a simple call to Count: statsd.Count("rollout.done", 1, nil, 1.0)