Repository navigation
Skip packing and decoding pages of a part's merged offsets that are consecutive - #120982
Conversation
Merging a text index remaps every posting's row id from the source part's numbering into the merged part's numbering, one PackedPartOffsets::operator[] call per (row, token) pair. Each call decodes a Frame-of-Reference page, pages[i >> 10][i & 1023]: two dependent loads plus a shift and a mask, with no locality between consecutive postings of a token. A 600k-row merge of 6 parts performs 8,969,216 of these lookups, a 1.8M-row one 11,520,000. MergedPartOffsets::insert feeds each source part's map the merged row numbers of that part's rows, strictly increasing, one per source row, in source-row order. A merge that does not interleave the part's rows with another part's rows leaves those numbers consecutive, which is the ordinary case: parts covering disjoint ascending ranges of the sorting key, and every part of a table without a sorting key, where the merge concatenates. For such a map the lookup is the affine function first_value + i, so tracking consecutiveness during insertion turns it into one addition and the page is never touched. The returned values are unchanged, so the produced index is byte-identical. The branch goes into PackedPartOffsets::operator[], the only reader of the values, rather than one level up: that keeps the flag next to the state that defines it and covers both users of the map, the text index merge and the projection _parent_part_offset remap in MergeTreeSequentialSource. consecutive starts false rather than true so that a map which received no values keeps master's failure behaviour: with true, a lookup on an empty map would return a plausible first_value + i where the page assertion fires today. The ordering chassert in insert is replaced by a strictly stronger one, since the old form skipped the check on the first value of every new page, the buffer having just been flushed. Measured with OPTIMIZE ... DRY RUN PARTS over the fixtures of tests/performance/text_index_merge.xml, 15 paired repetitions per arm with alternating arm order and a same-protocol A/A null subtracted: -6.1% to -9.7% on 6 parts covering disjoint key ranges, all four 95% confidence intervals excluding zero, with CPU time moving with the wall clock. On the counter layout, 6 parts each taking every sixth key, where the branch can never fire, the extra test costs +1.5% to +2.5%. The pages are still built for a consecutive map; eliding them needs a second mechanism and is not attempted here. Related: ClickHouse#120802
Reduce the new member block's comment to the merge property a reader cannot derive here; drop the restatement of insert()'s precondition and the narration of the fast path.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings), plus my own cold review One tradeoff deserves a maintainer's judgment rather than my own. The affine test sits in
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260919-064504 |
|
Workflow [PR], commit [7906c68] Summary: ❌
AI ReviewSummaryThe PR narrows the optimization to page-local consecutiveness inside Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit 7906c68.
Changed lines: Changed C/C++ lines covered: 96/96 (100.00%) · Uncovered code |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260919-044900 |
Build profile diff (arm_release)Comparing commit See the job log for details. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR changes MergeTree merged part-offset packing (adding a consecutive-run page representation in MergedPartOffsets.h), plus unit tests and a text-index merge performance test — all confined to the merge path. Neither flagged query exercises that code: TPC-H Q1 (scan + aggregation) and Q7 (join + aggregation) run read-side SELECTs with no merges involved, so the reported +32.0% (Q1) and +20.5% (Q7) cannot plausibly be caused by this diff. Q1 is additionally an intrinsically noisy query. Both flagged regressions are downgraded to not-sure as off-path run-to-run variance. clickbench🟢 No significant changes tpch_adapted_1_officialFlagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
CI finish ledger - 0171b85Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
154 checks pass, 19 skipped. The perf test this PR targets, Session id: cron:clickhouse-premium-slot-25:20260919-155700 |
A page whose merged offsets are consecutive is now stored as its first value plus a count, with nothing bit-packed and no arena allocation, and read back as min_val + i. flush() recognizes such a page by back() - front() == size() - 1, which is exact for a strictly increasing sequence: the span is at least size() - 1, with equality only when the values are consecutive. The state it stores, bits_per_val == 0 with no compressed data, is the one master's single-value page already used, so Page gains no field, keeps sizeof 32 and stays trivially copyable for PODArray, and the new branch subsumes the size() == 1 case rather than sitting next to it. In the reader the existing i == 0 return is reused, so the decode path gains one comparison and no new branch, since min_val + 0 is min_val for a packed page too. Per page rather than per map, which is what the review on the PR asked for: a part disjoint from the others apart from a few interleaved rows previously lost the fast path entirely, although a break can spoil only the one page holding it. Because consecutiveness is now a local property of the values in a page, evaluated on those values at flush time, no merge semantics, part ordering or mutation can make it lie, and both consumers of the map benefit without being able to disagree. The map-level flag added by the previous commit is therefore removed: it is redundant (pages[p].min_val + page_idx == first_value + i) and measured no faster on any fixture, the best point estimate being -2.5% with a 95% CI of [-5.6%, +0.5%]. Measured with OPTIMIZE ... DRY RUN PARTS over the fixtures of tests/performance/text_index_merge.xml, optimized builds, 16 paired repetitions with the arm order rotated across four positions, an A/A null subtracted. Against master: -9.7% and -8.3% on parts over disjoint key ranges, -9.0% and -6.9% on the new mostly-disjoint layout, -8.4% and -9.4% on the multiblock fixtures, all six 95% CIs excluding 0. Against the previous head the mostly-disjoint layout gains -7.6% and -7.1% and every other fixture's CI includes 0. Of the four fully interleaved fixtures, where no page is consecutive, three show no measurable change and multiblock with posting_list_codec = 'none' is +3.0% [+0.1%, +5.9%], which the previous head already paid at +2.8%. CPU time moves with wall clock. The produced index is byte-identical over six part layouts (disjoint, fully interleaved, mixed, mostly disjoint, single part and a table without a sorting key): all 125 files of the merged parts, 36 of them skp_idx_*, equal pristine master's. A full consecutive page also stops costing 1280 bytes of arena per 1024 rows. The performance test gains the mostly-disjoint layout in its vocabulary family, which no existing fixture expressed: CI had only fully disjoint parts, where the fast path fires for every page, and fully intersecting ones, where it fires for none. Two gtests cover a map whose pages disagree and a page that is consecutive except for its last step. Related: ClickHouse#120802
Internal second-model review: adjudication log (per-page round) (click to expand)Pre-publication review of the per-page commit by an independent model (engine: codex, delta re-review) plus Worth stating plainly rather than burying: moving the test into The byte-identity A-side is pristine master, not this branch's merge commit, whose tree already carries the
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260919-215000 |
CI finish ledger - 7906c68Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260920-113109 |
827b4f0

Closes: #120802
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improve performance of merges of tables with a text index. Pages of a source part's row ids that hold consecutive values are now stored as a first value plus a count, so they are neither bit-packed nor decoded per posting row id and need no storage for packed values. This covers every page of parts over disjoint sorting-key ranges or of a table without one, and most pages of parts disjoint apart from a few interleaved rows. The produced index is unchanged.
Description
Merging a text index remaps every posting's row id into the merged part's numbering, one
PackedPartOffsets::operator[]per (row, token) pair:pages[i >> 10][i & 1023], two dependent loads plus shift and mask. A 600k-row 6-part merge does 9.0M of them, a 1.8M-row one 11.5M.MergedPartOffsets::insertreceives each part's merged row numbers strictly increasing. Where the merge does not interleave that part's rows they are also consecutive, and such a page needs no storage at all:flush()recognizes it byback() - front() == size() - 1(exact for strictly increasing values), stores it asmin_valplus a count with nothing packed and no arena allocation, andPage::operator[]returnsmin_val + i. It reuses the existingi == 0return, so the decode path gains one comparison and no new branch.Per page rather than per map: a break spoils only the page holding it, so a part disjoint but for a few interleaved rows keeps the fast path elsewhere - 97.4% of its pages on such a layout.
Measured with
OPTIMIZE ... DRY RUN PARTSover the fixtures oftests/performance/text_index_merge.xml, 16 paired repetitions with the arm order rotated, an A/A null subtracted:CPU moves with wall clock. Such a page also stops costing 1.25 KiB of arena per 1024 rows.
The produced index is byte-identical: over 6 part layouts (disjoint, interleaved, mixed, mostly disjoint, single part,
ORDER BY tuple()) all 125 files of the merged parts, 36 of themskp_idx_*, equal master's.Workflow [PR]
Sync PR [sync-upstream/pr/120982]
Version info
26.10.1.288-master(included in26.10and later)