Repository navigation
Cheaper hashing and a cache-line-blocked bloom filter for join runtime filters - #119869
thevar1able wants to merge 9 commits into
Conversation
…e filters The bloom filter of a join runtime filter hashed every probed and inserted key with two `CityHash64WithSeed` calls over its bytes and then tested three bits at random positions of a 512 KiB filter, each located with a `libdivide` modulo: three dependent cache accesses per key. Two changes, both confined to the runtime filters, which live for one query: - For the usual 1/2/4/8-byte integer join keys the first hash is a 64-bit multiply-xorshift mixer of the key value and the second hash is the first times an odd constant, with the bit positions read from the high bits of the product. Longer keys keep CityHash. The choice depends only on the key length, so the build and probe side of a filter always agree. - `RuntimeBloomFilter` replaces `BloomFilter` for the runtime filters: all k bits of a key live in one cache-line block (512 bits) selected by the first hash, at positions given by the second hash. A probe touches one cache line and needs no division; k = 3 is unrolled, and up to 14 hash functions are supported (`join_runtime_bloom_filter_hash_functions` allows 10). The requested byte budget is rounded down to a power of two and never exceeded; the statistics-based size is rounded up before it is clamped. The set-bits worthiness check and the OR-merge of per-thread filters work as before. `BloomFilter` itself (also backing the persisted bloom filter indexes) only gains a mask fast path for power-of-two sizes, which yields the same bit positions as the division. TPC-H SF10 (22 queries, 32 threads, min of 2 interleaved rounds): wall time -8%, instructions -11%, cycles -10%, L1d demand misses -7%; Q21 -21%, Q04 -18%, Q08 -13%, Q22 -12%, Q05 -11%, Q09 -10%, Q03 -5%. All 22 query results identical. CI performance comparison: tpch Q3 -37%, Q21 -34%, Q5 -26%, Q22 -24%, Q4 -18%, `join_runtime_filter` -35%. A test runs the filter with 1, 3, 7, 8 and 10 hash functions on a fixed-size and on a string key.
|
Workflow [PR], commit [b2a4766] Summary: ❌
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units371 translation units recompiled, 3216 s compile time in total, 371 of them have a recent master baseline. |
|
For how much is left after this change: fully disabling the runtime filter is worth -35.8% on Q21 and -22.5% on Q3 on a 4-thread LZ4 setup, and on those queries the filter removes no rows at all.
Answers were hashed per query and are identical in both arms. What makes this more than a cheaper-probe question: The shipped adaptive abandonment ( One reproduction note: the effect is codec-sensitive. Building the same suite with ZSTD(3) instead of the default LZ4 inflates it from 18.6 s to 31.1 s and drops the runtime filter from ~55% to ~9% of query_21 CPU, which hides the headroom. |
|
Correction to my previous comment: the claim that the filter "removes no rows at all" on these queries is wrong, and so is the A join runtime filter discards probe rows after the reader has emitted them, so The end-to-end numbers stand, because they are measured arm timings and not derived from that inference: fully disabling the filter is still -35.8% on query_21 and -22.5% on query_03 in that setup. The corrected reading favours this PR. The filter does reduce join input and still loses on wall clock, so the residual is substantially per-check cost, and cheaper hashing plus a one-cache-line probe attack the right term. It also makes #96095 a complement rather than the whole answer. Please disregard my "What makes this more than a cheaper-probe question" paragraph, which rested on the retracted claim. |
|
TPC-DS doesn't improve but four TPC-H queries do quite significantly. Let's merge from master, clean up the code and try to get this in. |
…ked-bloom Master moved the stats-based bloom sizing into `RuntimeFilterBloomSizing.cpp`, so the power-of-two rounding of the grown size now lives in `growRuntimeBloomFilterBytesFromStats`. Master also added the runtime filter state transport, which needs `getFilterSizeBytes` and `getHashes` on the filter; `RuntimeBloomFilter` gets both.
…tually built `RuntimeBloomFilter` allocates a power of two number of 64-byte blocks, at least one, rounding its byte budget down. `switchToApproximateFilter` predicted the fill rate from the raw `join_runtime_bloom_filter_bytes`, so for a value like 1 (built as 64 bytes) or 1000 (built as 512 bytes) it could drop a filter that would be kept, or build one that the worthiness check then discards. The transport also compared the size of a received filter with the raw budget, so a non-power-of-two setting made every received bloom state invalid. `normalizeRuntimeBloomFilterBytes` gives the size that is actually built. The filter itself, the fill-rate prediction, the stats-based growth and the received-state check all use it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The transported runtime filter state carries the raw bits of the bloom filter. This branch changes both the bit layout (`RuntimeBloomFilter`, one cache-line block per key) and the hashing of 1, 2, 4 and 8 byte keys, so bits written by a build without it mean something else to a build with it, and the other way round. Merged or probed, they would drop rows that match. With the version at 2, such a state is rejected with `Unknown runtime filter state version` instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lel build streams A build side that is a `UNION ALL` of two halves of the key set is read by two streams. Each one builds a partial runtime filter that turns into a bloom filter, and `mergeBloomFilters` ORs the two. The test probes keys from both halves, with 8, 4 and variable-length keys and with 3 and 10 hash functions, and checks that no matching row is lost. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ked-bloom # Conflicts: # src/Interpreters/BloomFilter.cpp # src/Processors/QueryPlan/RuntimeFilterLookup.cpp
…ntime_filter_merge_bloom_partials` The indentation of the `BuildRuntimeFilterTransform` line depends on the plan above it, which varies with the randomized settings, so the test failed in several stateless test jobs: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119869&sha=3845a8c72256a9345f8694e06f709735d6e58bdb&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20targeted%29 #119869 Co-Authored-By: Claude Fable 5.2 <[email protected]>
- Keep the words of `RuntimeBloomFilter` in `VectorWithMemoryTracking`, so the allocation of the filter is checked against the memory limit, as it was with `BloomFilter::Container`. - Annotate `mix64` and `hashFixedKey` with `NO_SANITIZE_UNSIGNED_OVERFLOW`: their multiplications wrap around on purpose, and `master` now runs UBSan with the unsigned integer overflow check. - `countBloomFilterBits`, which came from `master`, takes `RuntimeBloomFilter`. - Add `[[nodiscard]]` to the new getters, as `modernize-use-nodiscard` is enabled now. - Rename `word_index_mask` to `block_index_mask`, which is what it is. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Related: #119047
The bloom filter of a join runtime filter hashed every probed and inserted key with two
CityHash64WithSeedcalls over its bytes and then tested three bits at random positions of a 512 KiB filter, each located with alibdividemodulo: three dependent cache accesses per key.Two changes, both confined to the runtime filters, which live for one query:
RuntimeBloomFilterreplacesBloomFilterfor the runtime filters: all k bits of a key live in one cache-line block (512 bits) selected by the first hash, at positions given by the second hash. A probe touches one cache line and needs no division; k = 3 is unrolled, and up to 14 hash functions are supported (join_runtime_bloom_filter_hash_functionsallows 10). The requested byte budget is rounded down to a power of two and never exceeded. The set-bits worthiness check and the OR-merge of per-thread filters work as before.BloomFilteritself (also backing the persisted bloom filter indexes) only gains a mask fast path for power-of-two sizes, which yields the same bit positions as the division.TPC-H SF10, 32 threads, min of 2 interleaved rounds: wall time -8%, instructions -11%, cycles -10%, L1d demand misses -7%; Q21 -21%, Q04 -18%, Q08 -13%, Q22 -12%, Q05 -11%, Q09 -10%, Q03 -5%. All 22 query results identical. In the CI performance comparison of the parent PR:
tpchQ3 -37%, Q21 -34%, Q5 -26%, Q22 -24%, Q4 -18%,join_runtime_filter-35%, reproduced locally with the CI binaries.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Join runtime filters hash fixed-size keys with a cheap 64-bit mixer and use a cache-line-blocked bloom filter, so a probe touches one cache line instead of three. Join-heavy TPC-H queries run up to 35% faster.
Workflow [PR]
Sync PR [sync-upstream/pr/119869]