Repository navigation
Conversation
Unify and speed up posting-list construction in the text index. Previously compressed posting lists were serialized via `PostingListCodecBitpacking` while uncompressed ones were written by ad-hoc code in `TextIndexSerialization::serializePostings`, and the `bitpacking` codec built a full Roaring bitmap during the build only to convert it to a `UInt32` array and re-encode it at serialization time. Now every posting list is encoded directly into its codec's on-disk form during the build via a new streaming `IPostingListAccumulator`: `createAccumulator` replaces `IPostingListCodec::encode`; the `none` codec adds row ids to segmented Roaring bitmaps, and the `bitpacking` codec streams bit-packed 128-row blocks (see `PostingListCodecBitpackingImpl`), both creating new segments of `posting_list_block_size` row ids and flushing on `finalize`. `PostingListBuilder` keeps the first `MAX_CARDINALITY_FOR_RAW_POSTINGS` row ids inline (raw and embedded tokens never touch a codec) and promotes to an accumulator on overflow. To keep the per-row hot path as cheap as the previous inline representation, the accumulators are `final` and dispatched on the codec type once per document (`dispatchByPostingCodec` plus a templated `PostingListBuilder::add`), the roaring `BulkContext` is kept in the builder (cache-warm next to the token's hash-map entry) and passed into `insert`, and the accumulator holder is a `std::vector` (pointee stability comes from the `unique_ptr`). The on-disk format is unchanged and `bitpacking` output is byte-identical. The only behavioral change is that the `none` codec now splits posting lists into segments by `posting_list_block_size` row count, like `bitpacking`, instead of by Roaring container, so the number of posting-list blocks for a dense token can change (reflected in the updated test reference). Building text indexes is faster: `bitpacking` is ~9% faster for small inputs and ~27% faster for large vocabularies and is now the faster codec to build; `none` is ~28% faster for large vocabularies. Related: ClickHouse#105850 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Follow-up to the streaming posting-list accumulators that cuts peak build memory and removes per-token state that does not need to be stored: - Pass the segment size (`posting_list_block_size`) into `insert` instead of storing it in every accumulator (`PostingListAccumulatorNone` and `PostingListCodecBitpackingImpl`). It is a per-index constant, so keeping a copy per token only wasted space. - Make the portable-Roaring serialization buffer in `PostingListAccumulatorNone::finalize` a local instead of a member. As a member it stayed alive for every accumulator until the whole granule was serialized, holding one buffer per token simultaneously. - Union the builder's accumulator pointer with the (dead after promotion) inline `small` array in `PostingListBuilder`, discriminated by a `promoted` flag, so the builder that lives by value in the hash map stays small. - Move the roaring `BulkContext` into `PostingListAccumulatorNone` so `insert` takes no context argument and the generic builder carries no codec-specific field (`bitpacking` no longer takes an ignored context parameter either). Peak build memory over the previous Roaring-based implementation drops from about +23% to +12% at medium token counts and from +14% to +9% at large vocabularies; the smaller per-token objects also speed up the build. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [3c33b60] Summary: ❌
AI ReviewSummaryThis PR replaces text-index posting-list construction with streaming codec accumulators for Missing context / blind spots
Findings
Tests
Final VerdictStatus: Minimum required actions: fix or reject the |
| } | ||
|
|
||
| void PostingListCodecBitpackingImpl::insert(uint32_t row_id) | ||
| void PostingListCodecBitpackingImpl::insert(uint32_t row_id, size_t max_rowids_in_segment) |
There was a problem hiding this comment.
posting_list_block_size = 1 is still accepted by textIndexValidator, but this path now returns before applying the segment-boundary check to the first row. With bitpacking, the first insert leaves row_ids_in_current_segment == 1; every later insert increments it to 2, 3, ... so row_ids_in_current_segment == max_rowids_in_segment never fires and the whole posting list is serialized as one segment instead of one row per segment. Please run the same flush logic for the first row as for the later rows, or reject sizes below the minimum segment size explicitly.
| return compressed_data.capacity() | ||
| + current_segment.capacity() * sizeof(uint32_t) | ||
| + segment_descriptors.capacity() * sizeof(SegmentDescriptor) | ||
| + segment_block_metas.capacity() * sizeof(SegmentBlockMetas); |
There was a problem hiding this comment.
memoryUsageBytes is meant to account for the heap held by the accumulator, but this only counts the outer segment_block_metas array. Each SegmentBlockMetas::metas owns a separate vector and encodeBlock pushes one PackedBlockMeta per packed block, so large bitpacking posting lists can hide roughly block_count * sizeof(PackedBlockMeta) bytes from the granule memory accounting. Please include the inner metas.capacity() * sizeof(PackedBlockMeta) totals here as well.
|
📊 Cloud Performance Report ✅ AI verdict: This change is confined to full-text (text) index posting-list construction and serialization in the MergeTree index writer. ClickBench Q18 is a GROUP BY aggregation that does not use any text index, so the query-execution path it exercises is untouched by this PR. The flagged -5.01% improvement (median 1437ms to 1365ms) is therefore not attributable to the change, and Q18's history is intrinsically noisy, so we read this delta as normal run-to-run variance and downgrade it to not-sure rather than crediting it as a real speedup. clickbenchFlagged queries (1 of 43)
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. tpch_adapted_1_official🟢 No significant changes Debug info
|
When building a text index with `posting_list_codec = 'none'`, each promoted token's row ids are streamed into a Roaring bitmap via `roaring_bitmap_add_bulk`, whose fast path loads the cached container from a `roaring::BulkContext`. The context lived inside the heap-allocated `PostingListAccumulatorNone`, reached only through the builder's `accumulator` pointer, so the per-row `context->container` load missed cache for every token whose accumulator had been evicted by the many other per-token accumulators touched in between. Move the `BulkContext` back into `PostingListBuilder`, paired with the accumulator pointer in a `PromotedState` that is unioned with the now-dead inline buffer. It then shares the cache line of the token's hash-map entry, which the per-row lookup already warms, so the hot `context->container` load hits. The builder stays 56 bytes and trivially relocatable (`BulkContext` holds only a POD). `insert` now takes the context by reference; `PostingListAccumulatorBitpacking` ignores it. Isolated A/B (both RelWithDebInfo, interleaved, merges stopped, TRUNCATE per measure): the `none` build for 5000 distinct tokens is ~8.5% faster; other sizes and `bitpacking` are within noise. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
… state Each posting list accumulator now defines a trivially destructible InsertState struct holding the state its insert reads and writes on every row: for the none codec the Roaring bulk-insert context and the segment row counter, for bitpacking nothing. PostingListBuilder stores it as opaque bytes in the promoted-state union (overlapping the dead inline buffer, so the builder stays 56 bytes) and recovers the concrete type in the templated add, so the builder no longer names any Roaring type and bitpacking no longer takes an ignored context argument. Moving the segment counter out of the heap accumulator also removes the last per-row access to a cold cache line: the per-row path now touches only the line warmed by the token hash-map lookup plus the Roaring container itself, matching the access pattern of the pre-refactoring code. Index files are byte-identical on both the insert and merge paths; the `add` self-time profile is back at parity with master, and inserts of real-world text become 5-9% faster for the none codec. ClickHouse#106735 Co-Authored-By: Claude Fable 5 <[email protected]>
|
|
||
| size_t PostingListAccumulatorNone::memoryUsageBytes() const | ||
| { | ||
| size_t result = current_segment.getSizeInBytes(); |
There was a problem hiding this comment.
memoryUsageBytes also needs to count the heap owned by the segments vector itself. Each sealSegment pushes a PostingList object into that vector; with a small allowed posting_list_block_size this can be one object per row, while the current accounting only adds each bitmap payload via getSizeInBytes. Please include segments.capacity() * sizeof(PostingList) so granule memory accounting sees the vector storage as well.
A CPU profile of the text index build with the bitpacking codec showed that per-row bookkeeping dominates while the SIMD packing kernels are only a few percent: the out-of-line call to insert of PostingListCodecBitpackingImpl alone took 26% of the INSERT. - Define insert inline in the header (including the accumulator shim) so the whole per-row chain folds into the templated PostingListBuilder::add; the segment-start branch is split into a cold out-of-line startSegment. - Reorder the impl fields so everything insert touches per row (previous row id, both counters, the block buffer header) shares the first cache line of the object. - Reserve the block buffer to BLOCK_SIZE once per accumulator. - Grow compressed_data geometrically: the old policy reserved only the shortfall, reallocating and copying the whole buffer every couple of blocks, which made the build quadratic in the posting list size. - Store segment descriptors and block metadata in absl::InlinedVector with one inline element: with the default posting_list_block_size almost every token has a single segment, and tokens with up to 128 rows have a single packed block, so the common case needs no heap allocations and back() reads the accumulator object itself. Index files are byte-identical on both the insert and merge paths. Build time: -7.7% on the 5000-token synthetic workload, -5.1% on a fineweb sample (single-threaded CPU samples -15.1% and -4.6%). ClickHouse#106735 Co-Authored-By: Claude Fable 5 <[email protected]>
| + tokens_and_postings.capacity() * sizeof(SortedTokensAndPostings::value_type) | ||
| + tokens_map.getBufferSizeInBytes() | ||
| + posting_lists_size | ||
| + accumulators_size |
There was a problem hiding this comment.
memoryUsageBytes now sums each accumulator payload, but it still omits the heap owned by the accumulators holder itself. This PR replaced posting_lists with one unique_ptr per promoted token, so large vocabularies can hide accumulators.capacity() * sizeof(std::unique_ptr<IPostingListAccumulator>) bytes from the writable granule accounting. Please add the holder storage alongside accumulators_size.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 225/248 (90.73%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code |
|
Continued in #107367. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved performance of building text indexes during inserts (especially for indexes with
bitpackingcodec).