Skip to content

[SDK] Optimize CircularBufferBuckets.Copy to use bulk array copies - #7670

Merged
martincostello merged 2 commits into
open-telemetry:mainfrom
matt-hensley:matt-hensley/prom-circular-buffer
Aug 21, 2026
Merged

martincostello merged 2 commits into
open-telemetry:mainfrom
matt-hensley:matt-hensley/prom-circular-buffer

Conversation

@matt-hensley

@matt-hensley matt-hensley commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Changes

Optimizes CircularBufferBuckets.Copy to use bulk array copies instead of computing indices per-item.

Copy populates a snapshot from the circular bucket storage on every histogram collection. It previously iterated over all array items, computing a modulo for each to translate the logical index into the ring position.

The loop has been replaced with at most two Array.Copy calls. On the first copy, the items from the offset to the end of the array are copied. If there were too many elements to fit (offset + size > capacity), the values wrap, and a second copy moves the remaining items to the start of the destination array.

160 bucket benchmark run:

Scenario Before After Improvement
CircularBufferBuckets.Copy, non-wrapped 257 ns 20.1 ns ~12.8×
ExponentialHistogramBuckets.SnapshotBuckets, non-wrapped 263 ns 18.5 ns ~14.2×
CircularBufferBuckets.Copy, wrapped 249 ns 21.8 ns ~11.4×
ExponentialHistogramBuckets.SnapshotBuckets, wrapped 241 ns 19.8 ns ~12.2×

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@github-actions github-actions Bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Aug 20, 2026
@matt-hensley
matt-hensley marked this pull request as ready for review August 20, 2026 20:13
@matt-hensley
matt-hensley requested a review from a team as a code owner August 20, 2026 20:13
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-08-21 08:03 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.41%. Comparing base (551f6eb) to head (3a23974).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7670      +/-   ##
==========================================
- Coverage   91.42%   91.41%   -0.02%     
==========================================
  Files         318      318              
  Lines       17711    17715       +4     
==========================================
+ Hits        16193    16194       +1     
- Misses       1518     1521       +3     
Flag Coverage Δ
unittests-Project-Experimental 91.48% <100.00%> (-0.05%) ⬇️
unittests-Project-Stable 91.46% <100.00%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/OpenTelemetry/Metrics/CircularBufferBuckets.cs 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@martincostello martincostello added this to the v1.18.0 milestone Aug 21, 2026
@martincostello
martincostello added this pull request to the merge queue Aug 21, 2026
Merged via the queue into open-telemetry:main with commit f6f6022 Aug 21, 2026
79 checks passed
This was referenced Aug 21, 2026
This was referenced Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants