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

Pool sortables used to create attribute sets #3832

Merged
merged 13 commits into from
Mar 13, 2023

Conversation

MrAlias
Copy link
Contributor

@MrAlias MrAlias commented Mar 3, 2023

The metric SDK converts accepted attribute slices into attribute Sets. Currently it creates a temporary slice to sort the attributes each time a measurement is made and it does so for each aggregation. This change optimizes that Set creation in the following ways:

  1. Reuse the created attribute Set for each aggregation. Attribute sets are immutable and the only operation the aggregation will perform on them is to filter. Filtering is an operation that returns a new copied Set. Therefore, reusing the attribute set across all aggregations means allocations only need to be made once.
  2. Use a pool of Sortable in the attribute package to create Sets that the user does not provide their own Sortable. This amortizes the cost of allocating a new temporary sorting slice during the Set creation across all Set creations.

Benchmarks

$ benchstat old.txt new.txt
name                                   old time/op    new time/op    delta
CounterAddNoAttrs-8                      41.3ns ± 1%    41.7ns ± 4%     ~     (p=0.483 n=9+10)
CounterAddOneAttr-8                       277ns ± 5%     248ns ± 2%  -10.52%  (p=0.000 n=10+10)
CounterAddOneInvalidAttr-8                391ns ± 6%     362ns ± 2%   -7.49%  (p=0.000 n=10+8)
CounterAddSingleUseAttrs-8               1.02µs ± 3%    0.88µs ± 4%  -14.27%  (p=0.000 n=8+9)
CounterAddSingleUseInvalidAttrs-8        1.31µs ± 3%    1.25µs ± 1%   -4.22%  (p=0.000 n=10+10)
CounterAddSingleUseFilteredAttrs-8       1.43µs ± 1%    1.41µs ± 2%   -1.17%  (p=0.016 n=8+10)
CounterCollectOneAttr-8                   327ns ± 7%     302ns ± 5%   -7.63%  (p=0.000 n=9+10)
CounterCollectTenAttrs-8                 3.12µs ± 8%    2.87µs ± 5%   -8.24%  (p=0.000 n=10+9)
CollectHistograms/1-8                     527ns ± 7%     512ns ± 5%     ~     (p=0.236 n=9+8)
CollectHistograms/5-8                    1.95µs ± 4%    1.95µs ± 5%     ~     (p=0.813 n=10+10)
CollectHistograms/10-8                   3.90µs ±11%    3.64µs ± 3%   -6.58%  (p=0.007 n=10+9)
CollectHistograms/25-8                   8.96µs ± 7%    8.93µs ± 6%     ~     (p=0.971 n=10+10)
Instrument/instrumentImpl/aggregate-8    4.22µs ± 4%    3.94µs ±23%     ~     (p=0.481 n=10+10)
Instrument/observable/observe-8          4.01µs ± 6%    3.90µs ±20%     ~     (p=0.661 n=10+9)
ManualReader/Collect-8                   1.22µs ± 3%    1.22µs ± 1%     ~     (p=0.813 n=10+7)
InstrumentCreation-8                     7.93µs ± 7%    7.97µs ± 4%     ~     (p=0.436 n=10+10)
PeriodicReader/Collect-8                 1.24µs ± 1%    1.26µs ± 2%   +1.43%  (p=0.001 n=10+8)

name                                   old alloc/op   new alloc/op   delta
CounterAddNoAttrs-8                       0.00B          0.00B          ~     (all equal)
CounterAddOneAttr-8                        152B ± 0%      128B ± 0%  -15.79%  (p=0.000 n=10+10)
CounterAddOneInvalidAttr-8                 280B ± 0%      256B ± 0%   -8.57%  (p=0.000 n=10+10)
CounterAddSingleUseAttrs-8                 275B ± 0%      228B ± 1%  -16.93%  (p=0.000 n=8+9)
CounterAddSingleUseInvalidAttrs-8          403B ± 0%      379B ± 0%   -5.96%  (p=0.000 n=9+10)
CounterAddSingleUseFilteredAttrs-8         595B ± 0%      571B ± 0%   -4.03%  (p=0.000 n=9+10)
CounterCollectOneAttr-8                    168B ± 0%      144B ± 0%  -14.29%  (p=0.000 n=10+10)
CounterCollectTenAttrs-8                 1.54kB ± 0%    1.30kB ± 0%  -15.62%  (p=0.000 n=10+10)
CollectHistograms/1-8                      592B ± 0%      592B ± 0%     ~     (all equal)
CollectHistograms/5-8                    2.64kB ± 0%    2.64kB ± 0%     ~     (all equal)
CollectHistograms/10-8                   5.20kB ± 0%    5.20kB ± 0%     ~     (all equal)
CollectHistograms/25-8                   13.1kB ± 0%    13.1kB ± 0%     ~     (all equal)
Instrument/instrumentImpl/aggregate-8    1.18kB ± 2%    0.89kB ± 5%  -24.44%  (p=0.000 n=10+8)
Instrument/observable/observe-8          1.17kB ± 3%    0.91kB ± 2%  -21.97%  (p=0.000 n=10+8)
ManualReader/Collect-8                     160B ± 0%      160B ± 0%     ~     (all equal)
InstrumentCreation-8                     2.37kB ± 0%    2.37kB ± 0%     ~     (all equal)
PeriodicReader/Collect-8                   160B ± 0%      160B ± 0%     ~     (all equal)

name                                   old allocs/op  new allocs/op  delta
CounterAddNoAttrs-8                        0.00           0.00          ~     (all equal)
CounterAddOneAttr-8                        3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)
CounterAddOneInvalidAttr-8                 3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)
CounterAddSingleUseAttrs-8                 3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)
CounterAddSingleUseInvalidAttrs-8          3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=10+10)
CounterAddSingleUseFilteredAttrs-8         5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
CounterCollectOneAttr-8                    4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.000 n=10+10)
CounterCollectTenAttrs-8                   31.0 ± 0%      21.0 ± 0%  -32.26%  (p=0.000 n=10+10)
CollectHistograms/1-8                      6.00 ± 0%      6.00 ± 0%     ~     (all equal)
CollectHistograms/5-8                      22.0 ± 0%      22.0 ± 0%     ~     (all equal)
CollectHistograms/10-8                     42.0 ± 0%      42.0 ± 0%     ~     (all equal)
CollectHistograms/25-8                      102 ± 0%       102 ± 0%     ~     (all equal)
Instrument/instrumentImpl/aggregate-8      7.00 ± 0%      2.00 ± 0%  -71.43%  (p=0.000 n=10+10)
Instrument/observable/observe-8            7.00 ± 0%      2.00 ± 0%  -71.43%  (p=0.000 n=10+10)
ManualReader/Collect-8                     5.00 ± 0%      5.00 ± 0%     ~     (all equal)
InstrumentCreation-8                       70.0 ± 0%      70.0 ± 0%     ~     (all equal)
PeriodicReader/Collect-8                   5.00 ± 0%      5.00 ± 0%     ~     (all equal)

This amortizes the allocation of a Sortable across multiple instrument measurements, meaning the benchmarks show one less allocation per measurement per aggregation.

@MrAlias MrAlias added pkg:SDK Related to an SDK package area:metrics Part of OpenTelemetry Metrics labels Mar 3, 2023
@MrAlias MrAlias added this to the Metric SDK v0.38.0 milestone Mar 3, 2023
@codecov
Copy link

codecov bot commented Mar 3, 2023

Codecov Report

Merging #3832 (83c7b62) into main (4af35a9) will increase coverage by 0.0%.
The diff coverage is 100.0%.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #3832   +/-   ##
=====================================
  Coverage   81.8%   81.8%           
=====================================
  Files        169     169           
  Lines      12570   12581   +11     
=====================================
+ Hits       10283   10294   +11     
  Misses      2072    2072           
  Partials     215     215           
Impacted Files Coverage Δ
attribute/set.go 62.8% <100.0%> (+0.9%) ⬆️
sdk/metric/instrument.go 96.0% <100.0%> (+0.2%) ⬆️

@MrAlias MrAlias marked this pull request as ready for review March 3, 2023 19:22
@MrAlias MrAlias changed the title Pool sortables used to create attribute sets in metric SDK Pool sortables used to create attribute sets Mar 3, 2023
sdk/metric/instrument.go Outdated Show resolved Hide resolved
sdk/metric/instrument.go Outdated Show resolved Hide resolved
attribute/set.go Outdated Show resolved Hide resolved
sdk/metric/instrument.go Outdated Show resolved Hide resolved
@MrAlias MrAlias requested a review from Petrie March 13, 2023 15:25
@MrAlias MrAlias merged commit b62eb2c into open-telemetry:main Mar 13, 2023
@MrAlias MrAlias deleted the pool-sortable branch March 13, 2023 18:19
@ash2k ash2k mentioned this pull request Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metrics Part of OpenTelemetry Metrics pkg:SDK Related to an SDK package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants