Repository navigation
Avoid per-state heap allocation in UniquesHashSet::read and batch merge - #111493
alexey-milovidov merged 14 commits into
Conversation
read() allocated a scratch buffer of m_size elements for every state with m_size >= 2, while typical stored uniq states are tiny. Read through a fixed 256-element stack chunk instead; this also folds the m_size <= 1 special case into the general path. merge() inserted elements one by one, each probe serialized behind the previous one. Apply the same batching technique as insertMany: when spare capacity guarantees that no resize/rehash can happen mid-batch, compute place() for a whole batch upfront so cache misses on the destination buffer overlap. The probe-and-place body shared by insertImpl, insertMany and merge is extracted into insertAtPlace. The non-batched fallback re-evaluates good() per element because shrinkIfNeed() can raise skip_degree. Results are bit-identical to the previous implementation, including under saturated skip_degree.
|
Workflow [PR], commit [f3555f4] Summary: ✅
AI ReviewSummaryThis PR removes the per-state heap scratch allocation from Final Verdict
|
numbers(150) never produced a state larger than 149 elements, so the round-trip test never exercised the multi-chunk loop in UniquesHashSet::read (chunk size 256). Use numbers(258) instead, which covers state sizes 255/256/257 and still verifies the finalized cardinality against the exact expected value.
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 44/44 (100.00%) · Uncovered code |
The squashed commit above omitted them. Repository instructions require the supplied CI report URL in the commit message, and a link to the corresponding pull request when the URL carries `PR=...`. Found by the stress test, STID `2508-224e`, check `Stress test (amd_asan_ubsan)`: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111493&sha=2054925548cba91a78383141bd49da2126f92307&name_0=PR&name_1=Stress%20test%20%28amd_asan_ubsan%29 Carrier pull request (the discoverer, not the author, it touches no join file): ClickHouse#111493 Co-Authored-By: Claude Fable 5 <[email protected]>
Build profile diff (arm_release)Commit See the job log for details. |
…hSet::merge` After merging master, the wide-set merge loop added by ClickHouse#115393 declares its own `i`, which shadows the batch index and fails the build with `-Wshadow`. This removed the PR from the merge queue: https://github.com/ClickHouse/ClickHouse/actions/runs/37976827113 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111493&sha=00ba4e00e36ef6f686be7a845e86d93ee4de33e3&name_0=PR&name_1=Stress%20test%20%28arm_release%29 ( The other failure, The earlier merge-queue rejection ( |
|
I looked at this run. The server is not hung: it is up and answering on 9440, but every login fails with How the access directory got into that state (
After the restart the mark is missing, so
|
|
The fix for steps 2 and 3 is #125058: |
|
🕵 Thanks @groeneai for the analysis of the stress-test failure (tracked by #120943 and #125058). The only remaining red job on the current head No code changes are needed here; the branch already contains master from 2026-10-10. |
|
🕵 Merged |
The performance test `uniq_stored` query ClickHouse#2 (merging 2 million stored single-element `uniq` states) regressed by 22% on ARM: the chunked read and the batched merge add overhead that does not pay off for tiny states. Restore the scalar read for `m_size <= 1` and use the plain merge loop when the right-hand state has fewer than `insert_many_batch_size` elements. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111493&sha=f844d188b1bf6c9c553a5346b12da46a93d601ed&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29 ClickHouse#111493 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The perf check flagged I pushed 15375b7, which brings back the fast paths for tiny states:
Larger states still use the chunked read and the batched merge. |
|
🕵 Merged Neither red job is related to
|
…iles `DiskAccessStorage` changes `<id>.sql` immediately and rewrites the `.list` index files a minute later from a background thread. `need_rebuild_lists.mark` is what tells the next start not to trust a stale index. `scheduleWriteLists` created it only when it started a new writer thread, and assumed in its two early returns that it already existed. Neither assumption held: - `SYSTEM RELOAD USERS` (`reloadAllAndRebuildLists`) writes the lists and removes the mark while the writer thread keeps waiting, so a DDL in the rest of that minute changed `<id>.sql` with no mark on disk; - after a failed list write (`failed_to_write_lists`) nothing writes the lists again until restart, and when that write came from `reloadAllAndRebuildLists` no mark existed either. A restart from either state trusted the stale index: an entity created in the window was lost, and a dropped one stayed listed without its `.sql`. For a row policy every new session then fails with `FILE_DOESNT_EXIST`, which is how `Stress test (arm_release)` on PR ClickHouse#111493 ended with a server that never became ready. Create the mark at the top of `scheduleWriteLists`, before both early returns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The The fuzzer had recreated the table as Measured on a local server with the CI cluster config, the job's DDL and
|
|
🕵 Thanks @groeneai: the |
…rter of the states No existing query number or text changes: 2 queries are disabled and 2 copies appended as 5 and 6. The copies are queries 0 and 2 with `WHERE k < 1250000`: the first quarter of the one part of uniq_tiny_states (153 of 611 granules), the same mix of 2- and 9-element states; each of the 1000 GROUP BY sets ends two size degrees smaller at the same fill. Locally (official amd builds, perf.py, two repetitions on different NUMA nodes, L3 capped to 1 way and to 3 ways): per-row time of the copies 0.94-1.06 of the originals', per-row user time too; the GROUP BY copy keeps 0.86-0.995 of the effect of ClickHouse#111493 (9b9c059 -> d1a7da6, CI x1.14 amd / x1.20 arm on query 2), the finalizeAggregation copy keeps 0.94 of the effect of the ClickHouse#129553 CI build (CI x1.31 / x1.39 on query 0). The pair passes ran 15 runs per query and server. The copy's 80 MB of sets sits between arm's 36 MB and amd's 105 MB shared L3, so the regime and the effect were gated at both cache widths. Saving: about 27 s of query time per master perf job on amd, 25 s on arm. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
UniquesHashSet::readallocated a heap scratch buffer for every state withm_size >= 2, while typical storeduniqstates hold only a few elements. It now reads through a fixed 256-element stack chunk, which also folds them_size <= 1special case into the general path.mergeinserted elements one by one, each hash-table probe serialized behind the previous one; it now uses the batching technique ofinsertMany(#95904) so misses on the destination buffer overlap. Results are bit-identical to the previous implementation, including under saturatedskip_degree.Measured with the added
tests/performance/uniq_states_read_merge.xml(before vs. after, median of the built-in benchmark, single query thread):Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up reading and merging of
uniqaggregate function states (e.g.uniqMergeover anAggregatingMergeTree) by removing a per-state heap allocation during deserialization and batching hash-table probes during merges, by 10-35% depending on state sizes and merge shape.Workflow [PR]
Sync PR [sync-upstream/pr/111493]
Version info
26.10.1.2296-master(included in26.10and later)