Skip to content

Cheaper hashing and a cache-line-blocked bloom filter for join runtime filters - #119869

Draft
thevar1able wants to merge 9 commits into
masterfrom
runtime-filter-blocked-bloom
Draft

thevar1able wants to merge 9 commits into
masterfrom
runtime-filter-blocked-bloom

Conversation

@thevar1able

@thevar1able thevar1able commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Related: #119047

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 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, 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: tpch Q3 -37%, Q21 -34%, Q5 -26%, Q22 -24%, Q4 -18%, join_runtime_filter -35%, reproduced locally with the CI binaries.

Changelog category (leave one):

  • Performance Improvement

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]

…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.
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b2a4766]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 14, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Sep 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3845a8c72 with master b98a0900b (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e031bc0b0; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

371 translation units recompiled, 3216 s compile time in total, 371 of them have a recent master baseline.

Job report

@snelheidai

Copy link
Copy Markdown

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.

enable_join_runtime_filters=0 vs the shipped default, same binary in both arms: TPC-H adapted SF10 with the shipped tests/benchmarks/tpc-h schema/queries/settings (join_use_nulls=1) and DEFAULT LZ4 compression, ClickHouse 26.9.1.1 at 1e93c0a plus one unrelated local patch, one server, 4 vCPU Xeon 8375C, max_threads=4, warm medians over 4 interleaved reps with the arm order rotated per rep, warmup excluded. The bracketed column is a byte-identical A/A null for the same query.

query default filters off Δ A/A null
query_21 2703 ms 1735 ms -35.8% -0.59%
query_03 870 ms 674 ms -22.5% +1.32%
query_08 1028 ms 884 ms -14.0% +0.34%
query_09 1670 ms 1521 ms -8.9% -1.74%
query_05 978 ms 953 ms -2.6% +0.10%
query_10 1092 ms 1068 ms -2.2% +0.37%

Answers were hashed per query and are identical in both arms.

What makes this more than a cheaper-probe question: read_rows is identical in both arms for every query (query_21 196,539,745; query_09 85,086,077; query_08 78,586,107; query_03 76,486,052). On these PK-FK joins with no extra predicate the filter eliminates zero rows, so its entire cost is overhead. A cheap mixer and a one-cache-line probe can shrink that overhead but not remove it. Taken at face value, your -21% on Q21 against this -35.8% ceiling leaves roughly a third to a half of the filter's cost on the table there — though the two numbers come from different thread counts (32 vs 4 here) and machines, so treat the fraction as indicative rather than measured. Recovering the remainder needs a decision not to build the filter: #96095 is that complementary direction.

The shipped adaptive abandonment (join_runtime_filter_pass_ratio_threshold_for_disabling=0.7) does not catch these queries. query_21 hashes tens of millions of build rows before any probe-side pass ratio can be observed, so a probe-side threshold cannot recover the build cost.

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.

@snelheidai

Copy link
Copy Markdown

Correction to my previous comment: the claim that the filter "removes no rows at all" on these queries is wrong, and so is the read_rows argument I based it on.

A join runtime filter discards probe rows after the reader has emitted them, so read_rows is unchanged by construction and says nothing about selectivity — aside from the separate enable_join_runtime_filters_index_analysis granule-pruning path, which these arms did not vary. By the pass-ratio counters the filter is in fact selective on this suite: 0.107 over 236.0M checked rows on query_21, and 0.147 over 39.6M on query_03, so it rejects roughly 85-90% of probed rows. Those counters come from an earlier run on a different build and configuration, not from the table above.

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.

@cv4g cv4g self-assigned this Sep 23, 2026
@rschu1ze

Copy link
Copy Markdown
Member

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.

thevar1able and others added 4 commits October 22, 2026 21:52
…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]>
thevar1able and others added 4 commits November 24, 2026 15:31
…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]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants