Skip to content

Commit 55bb792

Browse files
committed
textparse [PERF]: pool OM2 composite expansion buffers across parsers
The OM 2.0 parser explodes classic histograms and summaries into flat series through p.pending and p.seriesBuf. Both were allocated per parser instance and grown to the size of the largest family on the page, and since the scrape loop builds a new parser per scrape that was a few KiB of fresh allocations on every scrape of a page with composites, on top of what OM 1.0 needs for the same page (which just points into the input). Keep the backing arrays in a sync.Pool: take them on the first composite line and hand them back when Next() reaches io.EOF, at which point every pending entry has been served and nothing references them. On parse errors they are dropped with the parser. The release also detaches the slices from the parser so a stray Next() after EOF cannot touch pooled memory. Signed-off-by: Bartek Plotka <[email protected]>
1 parent 961c9ba commit 55bb792

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

‎model/textparse/openmetrics2parse.go‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"math"
2626
"strconv"
2727
"strings"
28+
"sync"
2829
"unicode/utf8"
2930

3031
"github.com/prometheus/common/model"
@@ -90,6 +91,26 @@ type pendingEntry struct {
9091
ts *int64
9192
}
9293

94+
// om2CompositeScratch holds the buffers that back the exploded composite
95+
// entries (pending queue and the series bytes it points into). They are
96+
// pooled across parser instances because callers like the scrape loop create
97+
// a new parser per scrape, and without pooling every scrape of a page with
98+
// classic histograms or summaries re-grows both buffers to the size of its
99+
// largest family (a few KiB per scrape).
100+
type om2CompositeScratch struct {
101+
pending []pendingEntry
102+
seriesBuf []byte
103+
}
104+
105+
var om2CompositeScratchPool = sync.Pool{
106+
New: func() any {
107+
return &om2CompositeScratch{
108+
pending: make([]pendingEntry, 0, 16),
109+
seriesBuf: make([]byte, 0, 1024),
110+
}
111+
},
112+
}
113+
93114
// om2Exemplar holds a fully parsed exemplar.
94115
type om2Exemplar struct {
95116
e exemplar.Exemplar
@@ -152,6 +173,11 @@ type openMetrics2Parser struct {
152173
// allocating one identity per entry.
153174
seriesBuf []byte
154175

176+
// scratch is the pooled owner of pending and seriesBuf's backing arrays,
177+
// taken lazily on the first composite line and returned on io.EOF. Nil
178+
// means no composite was parsed yet (or the buffers were already returned).
179+
scratch *om2CompositeScratch
180+
155181
// extraLabels holds the non-__name__ labels for the current composite line,
156182
// reused across composite parses.
157183
extraLabels []labels.Label
@@ -356,6 +382,10 @@ func (p *openMetrics2Parser) Next() (Entry, error) {
356382
if t := p.nextToken(); t != tEOF {
357383
return EntryInvalid, errors.New("unexpected data after # EOF")
358384
}
385+
// All pending entries were served above, so nothing references the
386+
// composite buffers anymore and they can go back to the pool. On
387+
// parse errors they are simply dropped together with the parser.
388+
p.releaseCompositeScratch()
359389
return EntryInvalid, io.EOF
360390
case tEOF:
361391
return EntryInvalid, errors.New("data does not end with # EOF")
@@ -1297,8 +1327,9 @@ func (p *openMetrics2Parser) buildClassicHistogramPending(
12971327
tsPtr = &p.ts
12981328
}
12991329

1300-
// p.pending has been reset to [:0] by Next() before any parsing begins,
1301-
// so we reuse its backing array across composite parses.
1330+
// p.pending and p.seriesBuf have been reset to [:0] by Next() before any
1331+
// parsing begins, so we reuse their backing arrays across composite parses.
1332+
p.acquireCompositeScratch()
13021333
pending := p.pending
13031334

13041335
// GaugeHistogram Samples with Classic Buckets expose count/sum as
@@ -1468,6 +1499,33 @@ func (p *openMetrics2Parser) buildNHCBHistogram(cf compositeFields, isNative boo
14681499
return h, fh, nil
14691500
}
14701501

1502+
// acquireCompositeScratch makes p.pending and p.seriesBuf point at pooled,
1503+
// empty backing arrays. It is a no-op once the parser owns a scratch.
1504+
func (p *openMetrics2Parser) acquireCompositeScratch() {
1505+
if p.scratch != nil {
1506+
return
1507+
}
1508+
p.scratch = om2CompositeScratchPool.Get().(*om2CompositeScratch)
1509+
p.pending = p.scratch.pending[:0]
1510+
p.seriesBuf = p.scratch.seriesBuf[:0]
1511+
}
1512+
1513+
// releaseCompositeScratch hands the (possibly grown) backing arrays back to
1514+
// the pool and detaches them from the parser, so a parser that is used after
1515+
// io.EOF cannot write into memory another parser may already own.
1516+
func (p *openMetrics2Parser) releaseCompositeScratch() {
1517+
if p.scratch == nil {
1518+
return
1519+
}
1520+
p.scratch.pending = p.pending[:0]
1521+
p.scratch.seriesBuf = p.seriesBuf[:0]
1522+
om2CompositeScratchPool.Put(p.scratch)
1523+
p.scratch = nil
1524+
p.pending = nil
1525+
p.pendingIdx = 0
1526+
p.seriesBuf = nil
1527+
}
1528+
14711529
func (p *openMetrics2Parser) buildSummaryPending(
14721530
cf compositeFields,
14731531
hasTS bool,
@@ -1481,8 +1539,9 @@ func (p *openMetrics2Parser) buildSummaryPending(
14811539
tsPtr = &p.ts
14821540
}
14831541

1484-
// p.pending has been reset to [:0] by Next() before any parsing begins,
1485-
// so we reuse its backing array across composite parses.
1542+
// p.pending and p.seriesBuf have been reset to [:0] by Next() before any
1543+
// parsing begins, so we reuse their backing arrays across composite parses.
1544+
p.acquireCompositeScratch()
14861545
pending := p.pending
14871546

14881547
cv := cf.fields[compFieldCount]

‎model/textparse/openmetrics2parse_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ rpc_duration_seconds {count:100,sum:30000.0,quantile:[0.5:100.0,0.9:200.0,0.99:3
367367
p := NewOpenMetrics2Parser([]byte(input), labels.NewSymbolTable(), ParserOptions{})
368368
got := testParse(t, p)
369369
requireEntries(t, exp, got)
370+
371+
// testParse drained the input to io.EOF, so the pooled composite buffers
372+
// must be detached from the parser and a stray Next() must not touch them.
373+
op := p.(*openMetrics2Parser)
374+
require.Nil(t, op.scratch)
375+
require.Nil(t, op.pending)
376+
require.Nil(t, op.seriesBuf)
377+
_, err := p.Next()
378+
require.Error(t, err)
370379
}
371380

372381
func TestOpenMetrics2ParseCompositeClassicHistogram(t *testing.T) {

0 commit comments

Comments
 (0)