Skip to content

[SDK] perf: avoid temp allocation for offer measurement - #4475

Open
proost wants to merge 5 commits into
open-telemetry:mainfrom
proost:perf-avoid-temp-allocation
Open

[SDK] perf: avoid temp allocation for offer measurement#4475
proost wants to merge 5 commits into
open-telemetry:mainfrom
proost:perf-avoid-temp-allocation

Conversation

@proost

@proost proost commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Changes

Purpose: Avoid constructing unnecessary MetricAttributes from KeyValueIterable.

Current implementation temporary allocation happens from KeyValueIterable to MetricAttributes. Problem is that offering measurement can be rejected. So temporary allocation can be just useless overhead.

So this improvements can be beneficial under below circumstances:

  1. Exemplar preview is enabled.
  2. The exemplar filter accepts the measurement
  3. The reservoir can reject offers, such as a fixed-size reservoir.

main branch:

-------------------------------------------------------------------------------------------------------
Benchmark                                             Time             CPU   Iterations UserCounters...
-------------------------------------------------------------------------------------------------------
BM_RecordWithExemplarSelection/0/0_mean            35.7 ns         35.5 ns           10 items_per_second=28.132M/s
BM_RecordWithExemplarSelection/0/0_median          35.5 ns         35.5 ns           10 items_per_second=28.1993M/s
BM_RecordWithExemplarSelection/0/0_stddev         0.622 ns        0.357 ns           10 items_per_second=279.713k/s
BM_RecordWithExemplarSelection/0/0_cv              1.74 %          1.00 %            10 items_per_second=0.99%
BM_RecordWithExemplarSelection/0/100_mean          36.1 ns         36.1 ns           10 items_per_second=27.7132M/s
BM_RecordWithExemplarSelection/0/100_median        36.0 ns         36.0 ns           10 items_per_second=27.7903M/s
BM_RecordWithExemplarSelection/0/100_stddev       0.317 ns        0.318 ns           10 items_per_second=242.779k/s
BM_RecordWithExemplarSelection/0/100_cv            0.88 %          0.88 %            10 items_per_second=0.88%
BM_RecordWithExemplarSelection/0/10_mean           38.6 ns         38.6 ns           10 items_per_second=26.1026M/s
BM_RecordWithExemplarSelection/0/10_median         37.3 ns         37.3 ns           10 items_per_second=26.8261M/s
BM_RecordWithExemplarSelection/0/10_stddev         4.09 ns         4.09 ns           10 items_per_second=2.18676M/s
BM_RecordWithExemplarSelection/0/10_cv            10.59 %         10.59 %            10 items_per_second=8.38%
BM_RecordWithExemplarSelection/0/1_mean            39.4 ns         39.4 ns           10 items_per_second=25.3817M/s
BM_RecordWithExemplarSelection/0/1_median          39.3 ns         39.2 ns           10 items_per_second=25.5021M/s
BM_RecordWithExemplarSelection/0/1_stddev         0.457 ns        0.470 ns           10 items_per_second=298.947k/s
BM_RecordWithExemplarSelection/0/1_cv              1.16 %          1.19 %            10 items_per_second=1.18%
BM_RecordWithExemplarSelection/3/0_mean             847 ns          847 ns           10 items_per_second=1.18123M/s
BM_RecordWithExemplarSelection/3/0_median           845 ns          845 ns           10 items_per_second=1.18316M/s
BM_RecordWithExemplarSelection/3/0_stddev          7.96 ns         7.98 ns           10 items_per_second=11.0703k/s
BM_RecordWithExemplarSelection/3/0_cv              0.94 %          0.94 %            10 items_per_second=0.94%
BM_RecordWithExemplarSelection/3/100_mean           841 ns          841 ns           10 items_per_second=1.18906M/s
BM_RecordWithExemplarSelection/3/100_median         840 ns          840 ns           10 items_per_second=1.19007M/s
BM_RecordWithExemplarSelection/3/100_stddev        3.86 ns         3.89 ns           10 items_per_second=5.49187k/s
BM_RecordWithExemplarSelection/3/100_cv            0.46 %          0.46 %            10 items_per_second=0.46%
BM_RecordWithExemplarSelection/3/10_mean            864 ns          864 ns           10 items_per_second=1.15876M/s
BM_RecordWithExemplarSelection/3/10_median          856 ns          856 ns           10 items_per_second=1.16877M/s
BM_RecordWithExemplarSelection/3/10_stddev         23.2 ns         23.1 ns           10 items_per_second=29.324k/s
BM_RecordWithExemplarSelection/3/10_cv             2.68 %          2.68 %            10 items_per_second=2.53%
BM_RecordWithExemplarSelection/3/1_mean             981 ns          981 ns           10 items_per_second=1.01917M/s
BM_RecordWithExemplarSelection/3/1_median           977 ns          977 ns           10 items_per_second=1.02327M/s
BM_RecordWithExemplarSelection/3/1_stddev          11.8 ns         11.7 ns           10 items_per_second=12.1194k/s
BM_RecordWithExemplarSelection/3/1_cv              1.20 %          1.20 %            10 items_per_second=1.19%
BM_RecordWithExemplarSelection/10/0_mean           3250 ns         3250 ns           10 items_per_second=307.687k/s
BM_RecordWithExemplarSelection/10/0_median         3243 ns         3242 ns           10 items_per_second=308.41k/s
BM_RecordWithExemplarSelection/10/0_stddev         35.0 ns         35.0 ns           10 items_per_second=3.29479k/s
BM_RecordWithExemplarSelection/10/0_cv             1.08 %          1.08 %            10 items_per_second=1.07%
BM_RecordWithExemplarSelection/10/100_mean         3223 ns         3223 ns           10 items_per_second=310.311k/s
BM_RecordWithExemplarSelection/10/100_median       3212 ns         3212 ns           10 items_per_second=311.374k/s
BM_RecordWithExemplarSelection/10/100_stddev       26.0 ns         25.9 ns           10 items_per_second=2.4697k/s
BM_RecordWithExemplarSelection/10/100_cv           0.81 %          0.80 %            10 items_per_second=0.80%
BM_RecordWithExemplarSelection/10/10_mean          3329 ns         3329 ns           10 items_per_second=300.513k/s
BM_RecordWithExemplarSelection/10/10_median        3331 ns         3331 ns           10 items_per_second=300.217k/s
BM_RecordWithExemplarSelection/10/10_stddev        56.9 ns         56.9 ns           10 items_per_second=5.16152k/s
BM_RecordWithExemplarSelection/10/10_cv            1.71 %          1.71 %            10 items_per_second=1.72%
BM_RecordWithExemplarSelection/10/1_mean           3720 ns         3720 ns           10 items_per_second=268.876k/s
BM_RecordWithExemplarSelection/10/1_median         3713 ns         3713 ns           10 items_per_second=269.356k/s
BM_RecordWithExemplarSelection/10/1_stddev         62.5 ns         62.6 ns           10 items_per_second=4.49279k/s
BM_RecordWithExemplarSelection/10/1_cv             1.68 %          1.68 %            10 items_per_second=1.67%
BM_RecordWithExemplarSelection/30/0_mean          10997 ns        10997 ns           10 items_per_second=90.9433k/s
BM_RecordWithExemplarSelection/30/0_median        11015 ns        11014 ns           10 items_per_second=90.7923k/s
BM_RecordWithExemplarSelection/30/0_stddev          109 ns          109 ns           10 items_per_second=900.754/s
BM_RecordWithExemplarSelection/30/0_cv             0.99 %          0.99 %            10 items_per_second=0.99%
BM_RecordWithExemplarSelection/30/100_mean        10972 ns        10972 ns           10 items_per_second=91.1444k/s
BM_RecordWithExemplarSelection/30/100_median      10972 ns        10973 ns           10 items_per_second=91.137k/s
BM_RecordWithExemplarSelection/30/100_stddev       95.8 ns         95.8 ns           10 items_per_second=793.527/s
BM_RecordWithExemplarSelection/30/100_cv           0.87 %          0.87 %            10 items_per_second=0.87%
BM_RecordWithExemplarSelection/30/10_mean         11263 ns        11263 ns           10 items_per_second=88.8039k/s
BM_RecordWithExemplarSelection/30/10_median       11243 ns        11243 ns           10 items_per_second=88.9415k/s
BM_RecordWithExemplarSelection/30/10_stddev         175 ns          175 ns           10 items_per_second=1.37273k/s
BM_RecordWithExemplarSelection/30/10_cv            1.56 %          1.56 %            10 items_per_second=1.55%
BM_RecordWithExemplarSelection/30/1_mean          12424 ns        12424 ns           10 items_per_second=80.4958k/s
BM_RecordWithExemplarSelection/30/1_median        12385 ns        12385 ns           10 items_per_second=80.7407k/s
BM_RecordWithExemplarSelection/30/1_stddev          134 ns          134 ns           10 items_per_second=862.957/s
BM_RecordWithExemplarSelection/30/1_cv             1.08 %          1.08 %            10 items_per_second=1.07%

this PR:


-------------------------------------------------------------------------------------------------------
Benchmark                                             Time             CPU   Iterations UserCounters...
-------------------------------------------------------------------------------------------------------
BM_RecordWithExemplarSelection/0/0_mean            31.0 ns         31.0 ns           10 items_per_second=32.3066M/s
BM_RecordWithExemplarSelection/0/0_median          30.8 ns         30.8 ns           10 items_per_second=32.43M/s
BM_RecordWithExemplarSelection/0/0_stddev         0.350 ns        0.284 ns           10 items_per_second=292.666k/s
BM_RecordWithExemplarSelection/0/0_cv              1.13 %          0.92 %            10 items_per_second=0.91%
BM_RecordWithExemplarSelection/0/100_mean          31.8 ns         31.7 ns           10 items_per_second=31.5002M/s
BM_RecordWithExemplarSelection/0/100_median        31.8 ns         31.7 ns           10 items_per_second=31.5872M/s
BM_RecordWithExemplarSelection/0/100_stddev       0.263 ns        0.252 ns           10 items_per_second=249.28k/s
BM_RecordWithExemplarSelection/0/100_cv            0.83 %          0.79 %            10 items_per_second=0.79%
BM_RecordWithExemplarSelection/0/10_mean           33.4 ns         33.4 ns           10 items_per_second=29.9215M/s
BM_RecordWithExemplarSelection/0/10_median         33.4 ns         33.4 ns           10 items_per_second=29.9415M/s
BM_RecordWithExemplarSelection/0/10_stddev        0.076 ns        0.077 ns           10 items_per_second=68.8606k/s
BM_RecordWithExemplarSelection/0/10_cv             0.23 %          0.23 %            10 items_per_second=0.23%
BM_RecordWithExemplarSelection/0/1_mean            42.1 ns         42.1 ns           10 items_per_second=23.726M/s
BM_RecordWithExemplarSelection/0/1_median          42.1 ns         42.1 ns           10 items_per_second=23.7625M/s
BM_RecordWithExemplarSelection/0/1_stddev         0.198 ns        0.198 ns           10 items_per_second=110.382k/s
BM_RecordWithExemplarSelection/0/1_cv              0.47 %          0.47 %            10 items_per_second=0.47%
BM_RecordWithExemplarSelection/3/0_mean             495 ns          495 ns           10 items_per_second=2.02162M/s
BM_RecordWithExemplarSelection/3/0_median           493 ns          493 ns           10 items_per_second=2.02692M/s
BM_RecordWithExemplarSelection/3/0_stddev          8.29 ns         8.30 ns           10 items_per_second=33.409k/s
BM_RecordWithExemplarSelection/3/0_cv              1.67 %          1.68 %            10 items_per_second=1.65%
BM_RecordWithExemplarSelection/3/100_mean           497 ns          497 ns           10 items_per_second=2.01308M/s
BM_RecordWithExemplarSelection/3/100_median         496 ns          496 ns           10 items_per_second=2.01566M/s
BM_RecordWithExemplarSelection/3/100_stddev        3.38 ns         3.39 ns           10 items_per_second=13.6708k/s
BM_RecordWithExemplarSelection/3/100_cv            0.68 %          0.68 %            10 items_per_second=0.68%
BM_RecordWithExemplarSelection/3/10_mean            548 ns          548 ns           10 items_per_second=1.82452M/s
BM_RecordWithExemplarSelection/3/10_median          544 ns          544 ns           10 items_per_second=1.83787M/s
BM_RecordWithExemplarSelection/3/10_stddev         12.5 ns         12.5 ns           10 items_per_second=39.702k/s
BM_RecordWithExemplarSelection/3/10_cv             2.29 %          2.29 %            10 items_per_second=2.18%
BM_RecordWithExemplarSelection/3/1_mean            1043 ns         1042 ns           10 items_per_second=959.721k/s
BM_RecordWithExemplarSelection/3/1_median          1043 ns         1043 ns           10 items_per_second=959.038k/s
BM_RecordWithExemplarSelection/3/1_stddev          21.2 ns         21.8 ns           10 items_per_second=20.0883k/s
BM_RecordWithExemplarSelection/3/1_cv              2.03 %          2.10 %            10 items_per_second=2.09%
BM_RecordWithExemplarSelection/10/0_mean           1841 ns         1841 ns           10 items_per_second=543.148k/s
BM_RecordWithExemplarSelection/10/0_median         1849 ns         1850 ns           10 items_per_second=540.672k/s
BM_RecordWithExemplarSelection/10/0_stddev         21.2 ns         21.2 ns           10 items_per_second=6.32359k/s
BM_RecordWithExemplarSelection/10/0_cv             1.15 %          1.15 %            10 items_per_second=1.16%
BM_RecordWithExemplarSelection/10/100_mean         1840 ns         1840 ns           10 items_per_second=543.569k/s
BM_RecordWithExemplarSelection/10/100_median       1836 ns         1836 ns           10 items_per_second=544.698k/s
BM_RecordWithExemplarSelection/10/100_stddev       14.3 ns         14.3 ns           10 items_per_second=4.17882k/s
BM_RecordWithExemplarSelection/10/100_cv           0.78 %          0.77 %            10 items_per_second=0.77%
BM_RecordWithExemplarSelection/10/10_mean          2031 ns         2031 ns           10 items_per_second=492.34k/s
BM_RecordWithExemplarSelection/10/10_median        2030 ns         2030 ns           10 items_per_second=492.626k/s
BM_RecordWithExemplarSelection/10/10_stddev        24.6 ns         24.6 ns           10 items_per_second=5.93066k/s
BM_RecordWithExemplarSelection/10/10_cv            1.21 %          1.21 %            10 items_per_second=1.20%
BM_RecordWithExemplarSelection/10/1_mean           3840 ns         3840 ns           10 items_per_second=260.45k/s
BM_RecordWithExemplarSelection/10/1_median         3826 ns         3826 ns           10 items_per_second=261.384k/s
BM_RecordWithExemplarSelection/10/1_stddev         52.1 ns         52.1 ns           10 items_per_second=3.52362k/s
BM_RecordWithExemplarSelection/10/1_cv             1.36 %          1.36 %            10 items_per_second=1.35%
BM_RecordWithExemplarSelection/30/0_mean           6194 ns         6194 ns           10 items_per_second=161.512k/s
BM_RecordWithExemplarSelection/30/0_median         6184 ns         6184 ns           10 items_per_second=161.702k/s
BM_RecordWithExemplarSelection/30/0_stddev          121 ns          121 ns           10 items_per_second=3.08747k/s
BM_RecordWithExemplarSelection/30/0_cv             1.95 %          1.95 %            10 items_per_second=1.91%
BM_RecordWithExemplarSelection/30/100_mean         6436 ns         6437 ns           10 items_per_second=156.121k/s
BM_RecordWithExemplarSelection/30/100_median       6276 ns         6275 ns           10 items_per_second=159.351k/s
BM_RecordWithExemplarSelection/30/100_stddev        517 ns          517 ns           10 items_per_second=10.4649k/s
BM_RecordWithExemplarSelection/30/100_cv           8.04 %          8.04 %            10 items_per_second=6.70%
BM_RecordWithExemplarSelection/30/10_mean          6986 ns         6986 ns           10 items_per_second=143.19k/s
BM_RecordWithExemplarSelection/30/10_median        6949 ns         6949 ns           10 items_per_second=143.912k/s
BM_RecordWithExemplarSelection/30/10_stddev         135 ns          135 ns           10 items_per_second=2.71724k/s
BM_RecordWithExemplarSelection/30/10_cv            1.93 %          1.93 %            10 items_per_second=1.90%
BM_RecordWithExemplarSelection/30/1_mean          13377 ns        13377 ns           10 items_per_second=74.8666k/s
BM_RecordWithExemplarSelection/30/1_median        13042 ns        13042 ns           10 items_per_second=76.6757k/s
BM_RecordWithExemplarSelection/30/1_stddev          560 ns          561 ns           10 items_per_second=3.02537k/s
BM_RecordWithExemplarSelection/30/1_cv             4.19 %          4.19 %            10 items_per_second=4.04%

Diffs:

Attributes    Original        New    Improvement
  ━━━━━━━━━━━━  ━━━━━━━━━━  ━━━━━━━━━  ━━━━━━━━━━━━━
            3      847 ns     495 ns          41.6%
  ────────────  ──────────  ─────────  ─────────────
           10     3250 ns    1841 ns          43.4%
  ────────────  ──────────  ─────────  ─────────────
           30    10997 ns    6194 ns          43.7%

If community think this optimization is too narrow a range, i will close it.

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@proost
proost requested a review from a team as a code owner August 24, 2026 14:28
@proost proost changed the title perf: avoid temp allocation for offer measurement [SDK] perf: avoid temp allocation for offer measurement Aug 24, 2026
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 38.88889% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.62%. Comparing base (1c2b007) to head (3bba0ce).

Files with missing lines Patch % Lines
...cs/exemplar/simple_fixed_size_exemplar_reservoir.h 0.00% 9 Missing ⚠️
...plar/aligned_histogram_bucket_exemplar_reservoir.h 44.45% 5 Missing ⚠️
...k/metrics/exemplar/fixed_size_exemplar_reservoir.h 71.43% 4 Missing ⚠️
...metry/sdk/metrics/exemplar/no_exemplar_reservoir.h 0.00% 2 Missing ⚠️
...ntelemetry/sdk/metrics/state/sync_metric_storage.h 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4475      +/-   ##
==========================================
- Coverage   82.67%   82.62%   -0.05%     
==========================================
  Files         516      516              
  Lines       20197    20225      +28     
==========================================
+ Hits        16696    16708      +12     
- Misses       3501     3517      +16     
Files with missing lines Coverage Δ
...ude/opentelemetry/sdk/metrics/exemplar/reservoir.h 100.00% <ø> (ø)
...try/sdk/metrics/exemplar/reservoir_cell_selector.h 100.00% <ø> (ø)
...metry/sdk/metrics/exemplar/no_exemplar_reservoir.h 71.43% <0.00%> (-28.57%) ⬇️
...ntelemetry/sdk/metrics/state/sync_metric_storage.h 84.94% <0.00%> (ø)
...k/metrics/exemplar/fixed_size_exemplar_reservoir.h 83.73% <71.43%> (-5.93%) ⬇️
...plar/aligned_histogram_bucket_exemplar_reservoir.h 71.43% <44.45%> (-15.23%) ⬇️
...cs/exemplar/simple_fixed_size_exemplar_reservoir.h 25.00% <0.00%> (-8.33%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant