Skip to content

openmetrics 2.0: avoid composite field allocations - #19747

Merged
bwplotka merged 2 commits into
prometheus:mainfrom
dashpole:om2-composite-zero-alloc
Oct 1, 2026
Merged

bwplotka merged 2 commits into
prometheus:mainfrom
dashpole:om2-composite-zero-alloc

Conversation

@dashpole

@dashpole dashpole commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Which issue(s) does the PR fix:

Part of #19745 (item 12 / PR 3).

This PR eliminates per-line kvMap (map[string]string) and intermediate string/slice allocations during OpenMetrics 2.0 composite value parsing:

  • Direct golex composite field lexing (sComposite, sCompValue, & compositeFields): Expands openmetrics2lex.l to lex composite delimiters (tCompOpen, tCompClose), field keys (tCompCount..tCompQuantile), commas (tComma), and values (tValue), replacing kvMap (map[string]string) and splitCompositeFields ([]string) with a fixed-size stack-allocated compositeFields struct ([numCompFields][]byte + seen uint16 bitmask).
  • Direct []byte parsing for spans and buckets (parseSpans, bucketsHaveFloat, parseIntBuckets, parseFloatBuckets): Replaces strings.Split and intermediate string conversions with bytes.SplitSeq, bytes.Cut, yoloString, and pre-sized destination slices via bytes.Count(inner, []byte{','}) + 1.
  • Reusable parser buffers (p.extraLabels & &p.ts): Reuses p.extraLabels capacity across composite series in nameAndExtraLabelsFromOffsets() and passes &p.ts instead of taking the address of a local copy in buildClassicHistogramPending() and buildSummaryPending().

Benchmarks (benchstat)

goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/model/textparse
cpu: Intel(R) Xeon(R) CPU @ 2.20GHz
                                          │     main     │            byte-offsets            │               golex                │
                                          │    sec/op    │   sec/op     vs base               │   sec/op     vs base               │
ParseOM2Text-2                              73.19µ ±  3%   62.85µ ± 3%  -14.12% (p=0.002 n=6)   60.95µ ± 8%  -16.72% (p=0.002 n=6)
ParseOM1VsOM2_AllTypes/parser=om2text-2     181.8µ ±  4%   168.2µ ± 3%   -7.43% (p=0.002 n=6)   171.1µ ± 3%   -5.86% (p=0.002 n=6)
ParseOM1VsOM2_CT/parser=om2text-2           92.47µ ±  3%   85.99µ ± 4%   -7.00% (p=0.002 n=6)   83.98µ ± 3%   -9.18% (p=0.002 n=6)
ParseOM1VsOM2_Histograms/parser=om2text-2   187.4µ ± 45%   166.6µ ± 5%  -11.10% (p=0.002 n=6)   161.2µ ± 3%  -13.98% (p=0.002 n=6)
geomean                                     123.2µ         110.9µ        -9.96%                 109.0µ       -11.54%

                                          │     main      │            byte-offsets             │                golex                │
                                          │      B/s      │     B/s       vs base               │     B/s       vs base               │
ParseOM2Text-2                              35.25Mi ±  3%   41.05Mi ± 3%  +16.45% (p=0.002 n=6)   42.32Mi ± 8%  +20.08% (p=0.002 n=6)
ParseOM1VsOM2_AllTypes/parser=om2text-2     33.63Mi ±  4%   36.33Mi ± 3%   +8.04% (p=0.002 n=6)   35.72Mi ± 3%   +6.24% (p=0.002 n=6)
ParseOM1VsOM2_CT/parser=om2text-2           28.38Mi ±  3%   30.51Mi ± 3%   +7.53% (p=0.002 n=6)   31.24Mi ± 4%  +10.10% (p=0.002 n=6)
ParseOM1VsOM2_Histograms/parser=om2text-2   16.79Mi ± 31%   18.88Mi ± 5%  +12.47% (p=0.002 n=6)   19.52Mi ± 3%  +16.25% (p=0.002 n=6)
geomean                                     27.41Mi         30.45Mi       +11.06%                 30.99Mi       +13.04%

                                          │     main     │            byte-offsets             │                golex                │
                                          │     B/op     │     B/op      vs base               │     B/op      vs base               │
ParseOM2Text-2                              9.673Ki ± 0%   7.211Ki ± 0%  -25.45% (p=0.002 n=6)   6.711Ki ± 0%  -30.62% (p=0.002 n=6)
ParseOM1VsOM2_AllTypes/parser=om2text-2     19.23Ki ± 0%   15.70Ki ± 0%  -18.36% (p=0.002 n=6)   15.45Ki ± 0%  -19.67% (p=0.002 n=6)
ParseOM1VsOM2_CT/parser=om2text-2           12.97Ki ± 0%   10.47Ki ± 0%  -19.28% (p=0.002 n=6)   10.22Ki ± 0%  -21.20% (p=0.002 n=6)
ParseOM1VsOM2_Histograms/parser=om2text-2   24.75Ki ± 0%   17.78Ki ± 0%  -28.16% (p=0.002 n=6)   17.53Ki ± 0%  -29.17% (p=0.002 n=6)
geomean                                     15.63Ki        12.05Ki       -22.92%                 11.67Ki       -25.32%

                                          │    main    │            byte-offsets            │               golex               │
                                          │ allocs/op  │ allocs/op   vs base               │ allocs/op   vs base               │
ParseOM2Text-2                              232.0 ± 0%   154.0 ± 0%  -33.62% (p=0.002 n=6)   150.0 ± 0%  -35.34% (p=0.002 n=6)
ParseOM1VsOM2_AllTypes/parser=om2text-2     415.0 ± 0%   342.0 ± 0%  -17.59% (p=0.002 n=6)   339.0 ± 0%  -18.31% (p=0.002 n=6)
ParseOM1VsOM2_CT/parser=om2text-2           224.0 ± 0%   170.0 ± 0%  -24.11% (p=0.002 n=6)   167.0 ± 0%  -25.45% (p=0.002 n=6)
ParseOM1VsOM2_Histograms/parser=om2text-2   480.0 ± 0%   337.0 ± 0%  -29.79% (p=0.002 n=6)   334.0 ± 0%  -30.42% (p=0.002 n=6)
geomean                                     319.0        234.4       -26.52%                 230.8       -27.65%

Release notes for end users (ALL commits must be considered).

Reviewers should verify clarity and quality.

NONE

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Negative histogram values and IgnoreNativeHistograms behavior require correction.

Review effort: Lite
Findings: None

What changed in this PR

This PR optimizes OpenMetrics 2.0 parsing to reduce allocations and enforce canonical field ordering.

Changes:

  • Replaces map-based composite parsing with reusable byte offsets and slices.
  • Optimizes native histogram span and bucket parsing.
  • Adds field-order validation and updates parser tests.
File Summary
model/​textparse/​openmetrics2parse.go Parser optimization, field-order validation, and histogram handling.
model/​textparse/​openmetrics2parse_test.go Updated fixtures and ordering-validation tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dashpole dashpole changed the title textparse: avoid composite field allocations and enforce field ordering openmetrice 2.0: avoid composite field allocations and enforce field ordering Sep 21, 2026
@dashpole
dashpole force-pushed the om2-composite-zero-alloc branch from 56be4c7 to 0ac26e3 Compare September 21, 2026 15:21
@dashpole dashpole changed the title openmetrice 2.0: avoid composite field allocations and enforce field ordering textparse: avoid composite field allocations in OpenMetrics 2.0 parser Sep 21, 2026
@dashpole dashpole changed the title textparse: avoid composite field allocations in OpenMetrics 2.0 parser openmetrics 2.0: avoid composite field allocations and enforce field ordering Sep 21, 2026
@dashpole
dashpole force-pushed the om2-composite-zero-alloc branch from 0ac26e3 to 7cd5149 Compare September 21, 2026 15:45
@dashpole dashpole changed the title openmetrics 2.0: avoid composite field allocations and enforce field ordering openmetrics 2.0: avoid composite field allocations Sep 21, 2026
@dashpole
dashpole marked this pull request as ready for review September 21, 2026 15:55
@dashpole
dashpole requested a review from a team as a code owner September 21, 2026 15:55
@rbizos

rbizos commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Nice !

@dashpole
dashpole force-pushed the om2-composite-zero-alloc branch from 7cd5149 to 73e5b9a Compare September 23, 2026 19:45
Comment thread model/textparse/openmetrics2parse.go Resolved

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I wonder if we shouldn't expand our lex instead of AI/hand typing.

Replace per-line kvMap and intermediate string/slice allocations during
OpenMetrics 2.0 composite value parsing with byte-offset parsing and
stack-allocated compositeFields.

Signed-off-by: David Ashpole <[email protected]>
@dashpole
dashpole force-pushed the om2-composite-zero-alloc branch from 73e5b9a to 473e5e3 Compare September 28, 2026 00:35
@bwplotka

bwplotka commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Thanks, did you rerun benchmark on how it compares to new lex implementation?

@dashpole

dashpole commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Yep, updated the PR description with the refreshed benchmarks comparing main, the previous byte-offsets approach, and golex. Comparing golex against byte-offsets, CPU time is within noise / slightly faster on histograms (-3.24% on ParseOM1VsOM2_Histograms, -1.75% geomean), and it saves an additional 3–4 allocs/op (-3.11% B/op geomean) since p.compositeOffsets []int is no longer needed.

@bwplotka bwplotka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@bwplotka
bwplotka merged commit fadf780 into prometheus:main Oct 1, 2026
40 checks passed
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.

4 participants