Repository navigation
perf(storage): storage search results are bitmaps; faster mask gather (#33444) - #33445
Conversation
Storage search results were []int64 row lists. They are now a *bitmap.Bitmap end to end: - PK and bloom/membership searches, the cached varlen search and the commit-ts filter set bits in a caller-owned bitmap. - ApplyTombstones and the Tombstoner, and the block read path's selections, take the bitmap. - The TopN candidate input and the in-memory workspace scan take it too. On BenchmarkStorageSearchChain (persisted 8192-row block, PK filter, search + tombstones + gather), dense, range, `>` and OR filters are 27-63% faster, and every read allocates a flat 2 KB instead of up to 130 KB. Sorted lookups that select a few rows and have no deletes pay about 0.5-1.3 us more. Also fixes UnionByMask, which dropped rows when the mask was shorter than the column: it cut the mask's last word at the column's length.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Queue requested — the merge queue status continues in this comment ↓. |
…in#33444) UnionByMask cost about 330 ns even for one selected row of 8192, so a mask gather was far behind a row-list gather on sparse selections. The fixed cost had three sources: - CountRange counted the whole mask when its own count answers. - Every word went through an indirect reader, even the zero words. - The scan did not stop after the last selected row. The gather now reads owned words directly, keeps the tail clear out of the loop and stops early. For each nonzero word it picks: - one copy for a full word; - the plain loop, inline, for a sparse word; - a copy per run for a run-heavy word (range results); - two independent 32-bit chains for a dense scattered word (ILP instead of one serial x &= x-1 chain). Results (BenchmarkMaskGatherByCount, 8192 rows): - 1 row: 324 -> 29 ns. - A 128-row run: 367 -> 45 ns. - Dense random selections: 35-48% faster. A position-list tier for small selections (the hybrid idea) was measured and lost to this path at every size, so it is not included. The search result bitmap now comes from objectio's reusable bitmap pool, and Bitmap.First scans owned words directly. On BenchmarkStorageSearchChain, the remaining loss on sorted point or IN lookups without deletes drops to about 0.3-0.5 us (+11-16%). Every other case is even or faster, by up to 69%.
… perf/search-results-bitmap
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 39166cf0be21c316b438a01468d29c7cfd289c57
Assessment: approve
No actionable defects found. The change reuses the existing bitmap and pooling mechanisms for storage selections while retaining row-distance pairing for TopN winners. The author's component benchmark reports substantial gains for several range and OR workloads, alongside 11–16% slower sorted point or IN-16 cases.
Model: gpt-6.1-terra / max
Session: 01a2230f-39de-7f00-bf9a-e19ded95bd85 · slot 0 · generation 39
Validation:
-
Reviewed the complete change, affected search, tombstone, workspace and block-read consumers, linked issues, and all indexed discussion.
-
Isolation preflight passed: checks/1795937631190350377-2.json and preflight.log.
-
A source-extracted gatherWord probe passed against a naive oracle for contiguous runs and 100,000 deterministic masks using Go 1.26.6 and test-exec. The function extraction was verified against the frozen source. See gather-probe.log and checks/1795938298520273199-2.json and checks/1795938358246875327-2.json.
-
git diff --check passed, and the source checkout remained clean. See checks/1795938121725317506-2.json and checks/1795938358295023547-2.json.
-
The frozen CI snapshot reports successful x86 unit, ARM64 SCA, and multi-CN proxy and pessimistic BVT jobs for the assessed head. The author also reports package, race and benchmark results.
Limitations:
-
Local repository package tests were not run: the required Go 1.27.1 toolchain download timed out. See toolchain.log and checks/1795937786363181530-2.json. The extracted Go 1.26.6 probe is a narrow algorithm check, not a substitute for those package tests.
-
Individual CI test logs and the author's raw benchmark artifacts were not included in the supplied evidence; the benchmark results were not independently repeated.
✅ Merged into
|
What type of PR is this?
Which issue(s) this PR fixes:
issue #33444 (subtask of #33181)
What this PR does / why we need it:
#33444 asks two questions. Was PR 2 (#33232) right to keep storage search results as
[]int64? And can the mask loops (UnionByMaskand the others) be made faster? This PR converts storage search results to bitmaps end to end and measures the whole chain. It rewrites the mask gather to remove its fixed per-call cost, and reports the hybrid (list for small selections) experiment. It also fixes a latentUnionByMaskbug.What changes
Storage search results are a
*bitmap.Bitmap, owned by the caller:vector.SearchFuncandobjectio.ReadFilterSearchFuncTypeset bits in a cleared bitmap. This covers the PK search factories, the cached varlenReadFilterSearch, bloom/membership hits andLinearSearchOffsetByValFactory.AddRange. A sorted</>finds its first failing row and sets the prefix or suffix once.pk > xno longer returns rows in descending order, soApplyTombstonesno longer sorts.FilterCachedRowsByCommitTS*andAnyCachedTSInRange*take the bitmap.engine.DataSource.ApplyTombstones(ctx, bid, rows *bitmap.Bitmap, policy) errorremoves rows in place, as doTombstoner.ApplyInMemTombstonesandTombstoner.ApplyPersistedTombstones.Bitmap.AndNot, which walks the smaller side.ReadDataByFilterfills a caller-owned bitmap.BlockDataReadInner,blockDataReadWithFilter, the exact-scan and membership entries, and the loaders (LoadColumnsDataInto, the newCopyCachedVectorMaskandConstructRowidColumnToWithMask) take bitmaps.InitDeposit, replacing two hand-written merges. Ordered limits useKeepFirstN, and live rows are the complement of the delete mask.TopNVector,ReadColumnTopN,BlockTopNRead.TopNand the fused loaders is a bitmap, with nil meaning all rows. Winners stay a ranked[]int64plus distances, and are turned into a mask once where columns are loaded for them.Close. The PK-dedup probes use the bitmap too.Boundaries kept on purpose
[]int64with their distances.cached_index_sample.go) are not a search result and still useCopyCachedVectorRows.Memory ownership
Every new bitmap has one owner and is freed on every exit path. The read path's selection bitmaps are private scratch, so they come from
mp.Base(). They are never charged to an output allocation view (readerOutputMP), matching the row lists they replace;TestMembershipScratchDoesNotStarveOutputcaught this.Bug fix:
UnionByMaskwith a mask shorter than the columngatherFixedByMask,forSelectedRowsandselectedWordcut the mask's last word atrows%64, instead of only the column's last word. A 98-bit mask over a 196-row column lost rows 68–97. Masks onmainalways matched the column length, so the bug was latent. This branch passes shorter masks (for example the checkpoint reader's[start, end)). The regression test isTestUnionByMaskShortMask.Mask gather (#33444)
BenchmarkMaskGatherByCount(new, vector) gathers k of 8192 rows, either scattered or as one run, by mask and by list. Before this PR,UnionByMaskcost about 330 ns even for one row. Three things caused it:CountRange: it counted the whole mask, although an owned mask already knows its count.The gather now works as follows:
x &= x-1chains are independent, so the CPU overlaps them instead of waiting on one serial chain.UnionOn the existing
BenchmarkMaskGather, int32, int64 and varchar10 are 20–55% faster at 2%, 50% and 90% density, and roughly even at 14%. varchar40 (out-of-line, where payload copying dominates) is even.BenchmarkUnionByMaskis −35%,BenchmarkUnionByMaskDense−61%, and theShrinkByMaskbenchmarks are unchanged. Varchar results are inBenchmarkMaskGatherByCountand follow the int64 ones.The hybrid (a list for small selections), measured and not included: UnionByMask gathered selections of up to K rows by position, from a stack list collected out of the mask. Against the optimized mask path:
Bitmapitself (keep a small selection as a sorted list, with no words to scan). That changes the Gobra-verified core and every bitmap user, so it is left as an open question in the design doc.Other measured choices:
copyCachedVectorfor up to 64 or 128 rows: slower, because its buffer escapes to the heap. Not included.UnionOnespecial case for one row: about 1% faster end to end, so dropped to keep one gather path.GetSearchBitmap), which saves a pooled allocation and free per block.Bitmap.First: now scans an owned bitmap's words directly.End to end
BenchmarkStorageSearchChain(new, readutil) reads a persisted 8192-row block throughBlockDataRead. The block has a sorted int64 PK (pk = 2·row) and int64 and varchar payloads. The benchmark covers search, then tombstones, then a gather of 3 columns.tomb_7deletes every 7th row through the productionFastApplyDeletesByRowIdson both sides; the main binary uses an equivalent list stub. Figures are medians of 6 alternating main/branch runs on an Apple M2 Max, with spread ≤ 6%.pk >=4096 rows/tomb_0/sortedpk >=4096 rows/tomb_0/unsortedpk >=4096 rows/tomb_7/sortedpk >=4096 rows/tomb_7/unsorted>and OR filters are 25–69% faster.The design doc section "Storage search results are bitmaps" has these numbers and the hybrid analysis.
Testing
TestSearchLessGreaterMatchesOracleandTestSearchFixedSizeAndVarlenLessGreaterMatchesOracle: random sorted and unsorted inputs with NaN, checked against a per-row oracle, including thequickfirst-failure semantics.TestSearchFuncsUnionIntoOneBitmap,TestAndNotAndFirst(including read-only views) andTestUnionByMaskShortMask.TestCopyCachedVectorMaskMatchesRows: againstCopyCachedVectorRows, covering one-row and dense masks, short masks, NULLs and the past-the-end rejection.TestScatterByMaskInvertsUnionByMaskandTestUnionByMaskShortMaskcover the rewritten gather loops.TestFastApplyDeletesByRowIdsMatchesModel: covers both the merge path and the walk path, with other blocks' rowids around.>results are now ascending.golangci-lintreports 0 issues;molint,go vet,gofmtand the license-eye header check are clean.🤖 Generated with Claude Code