Skip to content

Conversation

@dashpole
Copy link
Contributor

@dashpole dashpole commented Oct 2, 2025

Depends on #7441, #7443

This improves the concurrent performance of the fixed size reservoir's Offer function by 4x (i.e. 75% reduction). This improves the performance of Measure() for fixed-size reservoirs by 60% overall.

Accomplish this by:

  • using a single atomic for count and next. This assumes that both can fit in a uint32.
  • only use a lock to guard changing w and next together.

Offer benchmarks:

                           │   main.txt   │           fixedsize.txt            │
                           │    sec/op    │   sec/op     vs base               │
FixedSizeReservoirOffer-24   185.25n ± 4%   45.58n ± 1%  -75.40% (p=0.002 n=6)

Measure benchmarks:

                                                                          │   main.txt   │            fixedsize.txt            │
                                                                          │    sec/op    │    sec/op     vs base               │
SyncMeasure/NoView/ExemplarsEnabled/Int64Counter/Attributes/0-24            175.45n ± 6%   67.01n ±  9%  -61.81% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Int64Counter/Attributes/1-24            170.25n ± 1%   69.82n ±  6%  -58.99% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Int64Counter/Attributes/10-24           167.40n ± 2%   64.52n ± 10%  -61.46% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Float64Counter/Attributes/0-24          173.55n ± 0%   69.17n ± 12%  -60.14% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Float64Counter/Attributes/1-24          169.50n ± 1%   68.55n ±  5%  -59.56% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Float64Counter/Attributes/10-24         166.95n ± 1%   65.82n ±  6%  -60.58% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Int64UpDownCounter/Attributes/0-24      168.85n ± 1%   67.99n ± 11%  -59.73% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Int64UpDownCounter/Attributes/1-24      173.50n ± 1%   66.69n ±  2%  -61.56% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Int64UpDownCounter/Attributes/10-24     171.30n ± 5%   67.73n ±  8%  -60.46% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Float64UpDownCounter/Attributes/0-24    168.90n ± 2%   67.69n ±  9%  -59.92% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Float64UpDownCounter/Attributes/1-24    173.35n ± 2%   68.25n ±  9%  -60.63% (p=0.002 n=6)
SyncMeasure/NoView/ExemplarsEnabled/Float64UpDownCounter/Attributes/10-24   172.95n ± 2%   70.90n ±  7%  -59.01% (p=0.002 n=6)
geomean                                                                      171.0n        67.83n        -60.33%

@dashpole dashpole force-pushed the optimize_fixedsize_reservoir branch from bf5b8be to 7705d99 Compare October 2, 2025 16:37
@codecov
Copy link

codecov bot commented Oct 2, 2025

Codecov Report

❌ Patch coverage is 90.24390% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.2%. Comparing base (87c8b6a) to head (d6a538f).

Files with missing lines Patch % Lines
sdk/metric/exemplar/fixed_size_reservoir.go 90.2% 2 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #7447   +/-   ##
=====================================
  Coverage   86.2%   86.2%           
=====================================
  Files        302     302           
  Lines      21991   22002   +11     
=====================================
+ Hits       18968   18980   +12     
+ Misses      2642    2641    -1     
  Partials     381     381           
Files with missing lines Coverage Δ
sdk/metric/exemplar/fixed_size_reservoir.go 91.8% <90.2%> (-3.0%) ⬇️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dashpole dashpole force-pushed the optimize_fixedsize_reservoir branch 4 times, most recently from 906bb67 to a894b33 Compare October 6, 2025 14:42
@dashpole dashpole force-pushed the optimize_fixedsize_reservoir branch 3 times, most recently from 5daf244 to cb4d407 Compare December 15, 2025 18:43
@dashpole dashpole marked this pull request as ready for review December 15, 2025 18:44
@dashpole
Copy link
Contributor Author

@MrAlias @dmathieu this is ready for review!

Comment on lines +112 to 119
r.wMu.Lock()
defer r.wMu.Unlock()
r.advance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the lock be held here? Why not within advance? This seems very prone to deadlocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't hold the lock within advance because we also call advance() from within reset(), which needs to hold the lock for all operations.

It shouldn't be prone to deadlocks because both locations where we acquire the lock (reset() and Offer()) don't block on anything else while they hold the lock.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test cases for when k is 0 and greater than math.Maxuint32.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for k < 0. I tried to add a test case for greater than maxuint32, but I can't allocate a slice that large on my computer. I could use a bunch of mocks and such to test the if statement, but i'm pretty sure it isn't worth it.

@dashpole dashpole force-pushed the optimize_fixedsize_reservoir branch from da22953 to 1f2e8e0 Compare December 17, 2025 19:22
@dashpole dashpole force-pushed the optimize_fixedsize_reservoir branch from 1f2e8e0 to 22944a9 Compare December 17, 2025 19:33
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.

3 participants