census-instrumentation / opencensus-go

A stats collection and distributed tracing framework
http://opencensus.io
Apache License 2.0
2.05k stars 327 forks source link

Precompute encodeWithKeys buffer size to avoid resizes #1269

Closed howardjohn closed 2 years ago

howardjohn commented 2 years ago

This is strictly an optimization. Right now we always undersize the buffer then later increase it. This changes the code to accurately size it the first time to ensure we never re-allocate.

name                old time/op    new time/op    delta
RecordReqCommand-6    2.26µs ± 5%    2.10µs ± 5%   -7.39%  (p=0.000 n=10+10)
RecordViaStats-6      2.70µs ± 5%    2.53µs ± 4%   -6.31%  (p=0.000 n=10+10)

name                old alloc/op   new alloc/op   delta
RecordReqCommand-6      426B ± 0%      384B ± 0%   -9.86%  (p=0.000 n=10+10)
RecordViaStats-6        594B ± 0%      552B ± 0%   -7.07%  (p=0.000 n=10+10)

name                old allocs/op  new allocs/op  delta
RecordReqCommand-6      25.0 ± 0%      17.0 ± 0%  -32.00%  (p=0.000 n=10+10)
RecordViaStats-6        28.0 ± 0%      20.0 ± 0%  -28.57%  (p=0.000 n=10+10)

See https://github.com/census-instrumentation/opencensus-go/issues/1265

howardjohn commented 2 years ago

I don't think the test failure is from my test. Maybe its a flaky test? Its in the tracing code, not stats.

dashpole commented 2 years ago

Well, fix the typo, and hopefully it doesn't flake this time :)