perf(tsdb/index): allocation-free postings offset table scan#5159
Merged
simonswine merged 2 commits intoMay 15, 2026
Merged
Conversation
newReader spent ~96% of its allocations in ReadOffsetTable: each entry
allocated a []string plus string clones for name and value, even for
the ~31/32 entries discarded by symbolFactor sampling.
Switch to readPostingsOffsetTable which yields (name, value []byte)
aliasing the index buffer. The callback uses yoloString for map lookups,
defers string conversion until an entry is retained, and tracks the
"last" candidate as raw byte-slice aliases — cloning only at flush time.
BenchmarkNewReader (10 label names × 200 values):
│ sec/op │ sec/op vs base │
NewReader-12 305.1µ 124.2µ ± 1% -59.30%
│ B/op │ B/op vs base │
NewReader-12 270.7Ki 169.8Ki ± 0% -37.30%
│ allocs/op │ allocs/op vs base │
NewReader-12 6075.0 244.0 ± 0% -95.98%
aleks-p
previously approved these changes
May 15, 2026
| } | ||
|
|
||
| var lastKey []string | ||
| // lastNameB/lastValueB alias the mmap buffer and are only converted to |
Contributor
There was a problem hiding this comment.
nit, maybe we can remove the mention of mmap here to avoid confusion, it looks like the data is always heap allocated (and it shouldn't matter in general)
Contributor
Author
There was a problem hiding this comment.
Likely coming from the prometheus context 👍
Address review feedback — the reader uses heap-allocated buffers, not mmap, so the term was misleading.
aleks-p
approved these changes
May 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
newReaderspent ~96% of its allocations insideReadOffsetTable: every entry materialised a[]stringplus two string clones for name and value, even for the ~31/32 entries that symbolFactor sampling immediately discards.This PR introduces
readPostingsOffsetTable, a sibling ofReadOffsetTablethat yields(name, value []byte)slices aliasing the mmap buffer instead of allocating strings. ThenewReadercallback:yoloStringfor map lookups (zero allocation),string(name)/string(value)conversion until an entry is actually retained, andImpact
This path represents 66% of the allocation counts in query-backends (and a very small amount of compaction workers)
Benchmark
BenchmarkNewReaderwith 10 label names × 200 values each:Allocation count drops from 6 075 → 244 (−96%), wall time from 305 µs → 124 µs (−59%).
Notes
readPostingsOffsetTableis unexported and used only bynewReaderandPostingsRanges.ReadOffsetTable.Test plan
go test ./pkg/phlaredb/tsdb/index/... -run . -bench BenchmarkNewReaderpasses and shows regression-free numbersTestNewReader, table-driven cases) pass unchangedNote
Medium Risk
Touches TSDB index reading/parsing and introduces more buffer-aliasing (
[]byte/yoloString) to avoid allocations, which could cause subtle lifetime/aliasing bugs if misused despite being localized and covered by tests/bench.Overview
Reduces
NewReaderstartup allocations/time by replacing the genericReadOffsetTablepostings scan with an allocation-freereadPostingsOffsetTablethat returns(name, value []byte)slices aliasing the index buffer, and only materializesstringvalues for sampled/required entries (including correct handling of the per-label “last value” entry).Updates
PostingsRangesto use the new postings-table iterator, adjusts the label-indices test to decode the table directly, and addsBenchmarkNewReaderto track open-index performance/regressions.Reviewed by Cursor Bugbot for commit e07c1e0. Bugbot is set up for automated code reviews on this repo. Configure here.