Skip to content

perf(tsdb): skip ID lookup for zero-reference AppenderV2 samples - #20186

Merged
bwplotka merged 1 commit into
mainfrom
perfloop-pr-open-3c2bpz8fzw
Nov 30, 2026
Merged

bwplotka merged 1 commit into
mainfrom
perfloop-pr-open-3c2bpz8fzw

Conversation

@perfloop

@perfloop perfloop Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Note

Triggered and guided by @bwplotka -- let me know if this is too spammy etc!

Which issue(s) does the PR fix:

None.

In headAppenderV2.Append, a zero reference means the caller has no series ID. This change skips the ID-index lookup for zero and keeps the existing label-resolution path. Nonzero references still use the synchronized ID index, including IDs on stripe zero. Separate AppendExemplars lookups are unchanged.

Tests cover colliding label hashes with zero references and a real nonzero reference mapped to stripe zero while its label hash maps elsewhere. A concurrent creation/GC test checks the final series count. The selected-compaction retry case verifies that an unknown-reference append preserves samples before and after WAL restart.

The native DB benchmark is tsdb/db_appender_v2_bench_test.go. It preloads 10,000 stable series per worker on one head, then writes and commits float samples with reference zero. Its rows cover one appender, processor-count workers, and twice the processor count. Run it with go test ./tsdb/ -run '^$' -bench '^BenchmarkDBAppenderV2Append' -benchtime=128x -count=6 -cpu=4,8 -benchmem; compare the before-and-after outputs with benchstat.

The six-run native benchstat comparison for the eight-worker row reported elapsed benchmark time and throughput. These are local benchmark results, distinct from the official paired measurements below.

Benchmark row Metric Before After Change
DBAppenderV2Append-8 sec/op 836.8µ ± 3% 453.3µ ± 9% -45.83% (p=0.002, n=6)
DBAppenderV2Append-8 samples/s 11.95M ± 3% 22.06M ± 9% +84.62% (p=0.002, n=6)
DBAppenderV2Append-8 B/op 129.4Ki ± 8% 131.4Ki ± 17% ~ (p=0.937, n=6)
DBAppenderV2Append-8 allocs/op 7.000 ± 14% 7.000 ± 14% ~ (p=1.000, n=6)
DBAppenderV2AppendOversubscribed-4 B/op 214.3Ki ± 23% 236.7Ki ± 19% +10.45% (p=0.041, n=6)
DBAppenderV2AppendOversubscribed-4 allocs/op 867.5 ± 36% 1.140k ± 17% +31.47% (p=0.043, n=6)

Same-source allocation controls varied similarly or more than the oversubscribed row, so no allocation effect is attributed to this change and no allocation saving is claimed.

The sequential rule-output guard uses a fixed query vector rather than PromQL query execution. Its measurement program and the process-CPU accounting support are not included in this pull request. Process CPU is user plus system time, not hardware-instruction counts or a pinned-core measurement.

Workload Measure Metric Before median After median Paired change Outcome / requirement Paired runs
DB AppenderV2 batches of 10,000 unknown-ref samples on one head with eight disjoint workers time ns/op 844 µs 465.1 µs −45.6% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| ns 9
DB AppenderV2 batches of 10,000 unknown-ref samples on one head with eight disjoint workers cpu-ns/sample 601.6 343.8 -48.1% improvement passed 9
DB AppenderV2 batches of 10,000 unknown-ref samples on one head with eight disjoint workers samples/s 11848111 21501474 +78.8% non-regression passed; higher is better; required population loss ≤ 0.05 × |baseline median| samples/s 9
DB AppenderV2 batches of 10,000 unknown-ref samples on one head with one appender time ns/op 2.659 ms 2.425 ms −8.6% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| ns 11
DB AppenderV2 batches of 10,000 unknown-ref samples on one head with one appender cpu-ns/sample 250 234.4 −9.4% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| cpu-ns/sample 11
DB AppenderV2 batches of 10,000 unknown-ref samples on one head with one appender samples/s 3760902 4122910 +9.4% non-regression passed; higher is better; required population loss ≤ 0.05 × |baseline median| samples/s 11
DB AppenderV2 batches of 10,000 unknown-ref samples with 16 workers on eight Go processors time ns/op 914.2 µs 575.2 µs −38.1% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| ns 12
DB AppenderV2 batches of 10,000 unknown-ref samples with 16 workers on eight Go processors cpu-ns/sample 664.1 418 −38.2% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| cpu-ns/sample 12
DB AppenderV2 batches of 10,000 unknown-ref samples with 16 workers on eight Go processors samples/s 10939038 17385883 +59.7% non-regression passed; higher is better; required population loss ≤ 0.05 × |baseline median| samples/s 12
Single-group rule evaluation with 10,000 recording outputs time ns/op 8.5 ms 7.911 ms −7.1% non-regression passed; lower is better; required population loss ≤ 0.05 × |baseline median| ns 13

On workload DB AppenderV2 batches of 10,000 unknown-ref samples on one head with eight disjoint workers, median metric cpu-ns/sample changed from 601.6 to 343.8; paired median change −289.1 (sequential median bounds −453.2 to −203.1 from 9 complete pairs). All required improvement predicates were proved. This run's false-acceptance error allowance is 1/200, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples on one head with eight disjoint workers, non-regression guard ns/op (lower is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 844016 and after median 465084; paired median change −384818 (sequential median bounds −465274 to −295560 from 9 complete pairs). This run's false-acceptance error allowance is 1/200, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples on one head with eight disjoint workers, non-regression guard samples/s (higher is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 11848111 and after median 21501474; paired median change +9340779 (sequential median bounds +7215374 to +12040842 from 9 complete pairs). This run's false-acceptance error allowance is 1/200, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples on one head with one appender, non-regression guard ns/op (lower is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 2658937 and after median 2425472; paired median change −229836 (sequential median bounds −366142 to −48112 from 11 complete pairs). This run's false-acceptance error allowance is 1/600, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples on one head with one appender, non-regression guard cpu-ns/sample (lower is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 250 and after median 234.4; paired median change −23.4 (sequential median bounds −54.7 to 0 from 11 complete pairs). This run's false-acceptance error allowance is 1/600, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples on one head with one appender, non-regression guard samples/s (higher is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 3760902 and after median 4122910; paired median change +354759 (sequential median bounds +72167 to +552925 from 11 complete pairs). This run's false-acceptance error allowance is 1/600, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples with 16 workers on eight Go processors, non-regression guard ns/op (lower is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 914164 and after median 575183; paired median change −348602 (sequential median bounds −487829 to −270955 from 12 complete pairs). This run's false-acceptance error allowance is 1/1200, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples with 16 workers on eight Go processors, non-regression guard cpu-ns/sample (lower is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 664.1 and after median 418; paired median change −254 (sequential median bounds −312.5 to −179.7 from 12 complete pairs). This run's false-acceptance error allowance is 1/1200, not simultaneous coverage of the displayed bounds.

On workload DB AppenderV2 batches of 10,000 unknown-ref samples with 16 workers on eight Go processors, non-regression guard samples/s (higher is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 10939038 and after median 17385883; paired median change +6535941 (sequential median bounds +5213049 to +10169432 from 12 complete pairs). This run's false-acceptance error allowance is 1/1200, not simultaneous coverage of the displayed bounds.

On workload Single-group rule evaluation with 10,000 recording outputs, non-regression guard ns/op (lower is better) passed: required population loss ≤ 0.05 × |baseline median|; before median 8499573 and after median 7911060; paired median change −606946 (sequential median bounds −1226155 to −217146 from 13 complete pairs). This run's false-acceptance error allowance is 1/2000, not simultaneous coverage of the displayed bounds.

The TSDB and rules tests, TSDB lint with supported label builds, focused race tests, the Prometheus TestTimeMetrics caller test, and dependent Prometheus/promtool builds passed. The 32-bit x86 and Windows Go test suites did not run locally: this Linux sandbox cannot execute ELF32 or Windows binaries, so both remain target-CI work. The synthetic series and concurrency mix do not establish production traffic frequency or the number of concurrent rule outputs.

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

[PERF] TSDB: Skip the ID-index lookup for zero-reference AppenderV2 samples.

Generated by Perfloop.

@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.

Let's kill the extra benchmark - this is a trivial optimization, otherwise LGTM

Comment thread tsdb/db_appender_v2_bench_test.go Outdated
benchmarkDBAppenderV2Append(b, processors*2, 2)
}

func benchmarkDBAppenderV2Append(b *testing.B, workers, parallelism int) {

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.

We already have benchmarks for append, can we use those?

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.

Any response on this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes—the PR now uses the existing BenchmarkHeadAppender_AppendCommit for the native benchmark recipe and local benchstat comparison. I removed the new benchmark file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes—the update removes the added benchmark and uses the existing append benchmark in the description. It also removes the test additions and updates the patch to the current target.

Comment thread tsdb/head_append_v2.go
s := a.head.series.getByID(chunks.HeadSeriesRef(ref))
var s *memSeries
if ref != 0 {
s = a.head.series.getByID(chunks.HeadSeriesRef(ref))

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.

This is a trivial optimization and makes sense, but practically we don't have those cases often (batched samples only) -- still worth doing!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Kept the optimization confined to the sample appender. Nonzero references still take the synchronized ID lookup, and separate exemplar lookups are unchanged.

@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.

Way too many tests, explanations and benchmarks for a trivial optimization that practically changes nothing for majority of users 🙈

Can you remove all tests and benchmarks in this PR and just optimize appender?

Also needs rebase

Comment thread tsdb/db_test.go Outdated
// the appender proceeds to append or resolve it by labels.
h.testAfterSeriesLookup = func(series *memSeries) {
if series != oldSeries {
if appender.unknownRef {

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.

For v2/unknown-ref, testAfterSeriesLookup fires with series == nil before a.getOrCreate(ls) runs, so CompactSelectedSeries evicts oldSeries before label lookup starts and lockForAppend never exercises its isGCed() retry path. Moving testAfterSeriesLookup after the if s == nil block in head_append_v2.go would let all cases pause with oldSeries resolved and drop unknownRef/appendRef, while TestHeadAppenderV2_NonzeroReferenceOnStripeZeroUsesIDLookup can pass labels.EmptyLabels() with targetRef to verify getByID directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed those added tests and the hook-comment changes to follow your overall request to keep this PR to the appender optimization. The PR no longer changes the GC test or the stripe-zero test.

Comment thread tsdb/db_appender_v2_bench_test.go Outdated
const interval = 15 * time.Second

logger := promslog.NewNopLogger()
db, err := Open(b.TempDir(), logger, prometheus.NewRegistry(), DefaultOptions(), nil)

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.

If we keep a concurrent benchmark in head_bench_test.go, Open with DefaultOptions() leaves background compactions enabled so -benchtime=5s crosses 3h at 720 iterations and runs head compaction mid-benchmark, while per-worker 15s steps in b.RunParallel can fail with ErrOutOfBounds if one worker lags another by 240 iterations. Benchmarking Head directly or calling db.DisableCompactions() with a 1ms step avoids both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The concurrent benchmark has been removed from this PR, so it no longer adds either the background-compaction or timestamp-skew behavior you identified.

Comment thread tsdb/db_appender_v2_bench_test.go Outdated
benchmarkDBAppenderV2Append(b, processors*2, 2)
}

func benchmarkDBAppenderV2Append(b *testing.B, workers, parallelism int) {

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.

Any response on this?

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-3c2bpz8fzw branch from cf0eb5f to 28b959e Compare November 3, 2026 08:52
@perfloop

perfloop Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Removed all added tests and benchmarks and updated the patch to the current target. The remaining diff skips the ID lookup for reference zero in the sample appender.

@perfloop

perfloop Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Perfloop updated this change. The new description differs in: the opening text; Which issue(s) does the PR fix:; Release notes for end users (ALL commits must be considered)..

@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.

LGTM, thanks!

@bwplotka

Copy link
Copy Markdown
Member

This is trivial, so I am going to merge this

@bwplotka
bwplotka merged commit 0e59484 into main Nov 30, 2026
39 checks passed
@bwplotka
bwplotka deleted the perfloop-pr-open-3c2bpz8fzw branch November 30, 2026 10:02
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.

2 participants