Repository navigation
Faster sipHash128 on String and FixedString columns - #129204
Conversation
`sipHash128` hashes several strings of a `String` or `FixedString` column at a time. The hashes do not change. On aarch64 a group holds 8 strings: 4 in NEON lanes and 4 in general registers. With AVX2 all 8 are in vector lanes. One loop compresses the first words of all the strings of a group, as many as the shortest string has. Each vector then continues until its longest string ends, so its shorter strings waste words. In a `String` column, sorting the rows by length puts strings of similar lengths into the same groups, at about the cost of hashing one or two words per row. So the rows are sorted only when the longer strings can fill whole groups of their own. That needs at least 16 rows, at least 8 of them with more words than the shortest. With fewer than 32 rows, the longest and the shortest string must also differ by at least 4 words. The strings of a `FixedString` column have one length, so they waste no words and need no sort. The code is in `SipHashStringsImpl.h`, written with clang vector extensions and with plain loops that clang vectorizes. Its only intrinsic is the NEON shift-and-insert `SRI` in the rotate, because clang does not generate it from portable code. `sipHash128PaddedStrings` calls it 256 rows at a time. `sipHash128PaddedFixedStrings` needs no offsets, length buckets or chunks. It passes the strings to `hashGroup` 8 at a time and the last few rows to `sipHash128Padded`. Builds without NEON or AVX2 hash the strings one by one. The code reads up to 8 bytes before a string that is shorter than 8 bytes, and up to 7 bytes past the end of any string. The padding of `ColumnString` and `ColumnFixedString` covers both. Every type that `sipHash128` hashes through a `String` or `FixedString` column gets the faster path: `Nullable`, `LowCardinality`, `Array`, `Map` and `Tuple` of them. `sipHash128Keyed`, `sipHash128Reference`, `sipHash128ReferenceKeyed`, constant arguments and the types that are serialized before hashing keep hashing one value at a time. `sipHash128Keyed` with zero keys gives the same hashes, one string at a time. The tests `05462_sip_hash_128_many_strings` and `05493_sip_hash_128_fixed_strings` compare the two. The second one covers `FixedString` widths 1 to 17 and 11 more up to 1000, in blocks with 0, 1 and 125 groups of 8 strings. `benchmark_sip_hash_strings` compares `sipHash128PaddedStrings` and `sipHash128PaddedFixedStrings` with `sipHash128Padded` per string. The performance test `sip_hash_128_strings` measures `sipHash128` on `String` columns of several string lengths and on a `FixedString(16)` column. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [de22684] Summary: ✅
AI ReviewSummaryPR #129204 accelerates Missing context / blind spots
Final VerdictStatus: ✅ Approve. No required code changes identified; merge remains subject to normal CI completion. |
There was a problem hiding this comment.
🔵 Needs a closer look
Stable hash equivalence and padded-read safety remain unverified for the NEON and AVX2 kernels because native regression and sanitizer results are unavailable on both architectures.
No open issues 🎉
What changed in this PR
This PR accelerates sipHash128 for String and FixedString columns by hashing eight values together, while preserving existing hash results. It separates the batched hashing work described in the related packed-string hash-join PR.
Changes:
- Adds NEON and AVX2 hashing kernels, with length-based grouping for variable-length strings and scalar processing on other builds.
- Integrates batched hashing into the SQL function, including supported nested column types.
- Adds compatibility tests, performance queries, and microbenchmarks.
| File | Description |
|---|---|
tests/queries/0_stateless/05493_sip_hash_128_fixed_strings.sh |
Checks fixed-width hashes across widths, block sizes, and wrapper types. |
tests/queries/0_stateless/05493_sip_hash_128_fixed_strings.reference |
Records expected zero mismatches for fixed-width tests. |
tests/queries/0_stateless/05462_sip_hash_128_many_strings.sql |
Checks hash compatibility, nested types, multiple arguments, and batch boundaries. |
tests/queries/0_stateless/05462_sip_hash_128_many_strings.reference |
Records compatibility checksums and expected results. |
tests/performance/sip_hash_128_strings.xml |
Benchmarks hashing across string lengths and realistic datasets. |
src/Functions/FunctionsHashing.h |
Routes unkeyed sipHash128 string columns through batched hashing. |
src/Common/SipHashStringsImpl.h |
Implements vector kernels, length buckets, and row grouping. |
src/Common/SipHashStrings.h |
Declares batched hashing interfaces and padding requirements. |
src/Common/SipHashStrings.cpp |
Implements column-level batching and scalar alternatives. |
src/Common/benchmarks/sip_hash_strings.cpp |
Compares batched and per-row hashing performance and results. |
src/Common/benchmarks/CMakeLists.txt |
Registers the hashing microbenchmark target. |
Review effort: Balanced
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes6 object files changed (+27.55 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units13 translation units recompiled, 43 s compile time in total, 12 of them have a recent master baseline. |
sipHash128sipHash128 on String and FixedString columns
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok. I understand the algorithm. It is fairly clever. And the implementation is neat.
dc0b52e
…tring or IPv6 `uniqExact` stores `sipHash128Padded` of each `String` / `FixedString` value (IPv6 as its 16 bytes) and hashed one row at a time. Now a chunk of 256 rows first chooses the rows that get a key exactly as before (NULL, false `-If`, null place and repeated (place, value) rows are skipped), then hashes them with one call of the vectorized functions of ClickHouse#129204, and inserts the keys in the same order. Fewer than 8 chosen rows are hashed one at a time, as those functions would do anyway. The hashes, and so the serialized states, do not change. Builds without AVX2 or NEON hash one row at a time inside the same functions. Two row-list variants are added next to ClickHouse#129204's functions: `sipHash128PaddedStringRows` (`SipHashStrings::hashRows` over a row list) and `sipHash128PaddedFixedStringRows`. Converted call sites, chosen by measurement (ClickHouse#129436 item 5; 1, 16 and all threads, String 0-15 / 16-64 / 0-300 bytes, FixedString(16) / (64), IPv6, 3 ABAB rounds + 5-round re-runs of cells above 1.03), fix / master time: - GROUP BY without consecutive repeats (the ClickHouse#128190 fallback, before: one `add` per row): 0.37-0.66 at 1 thread, 0.57-0.91 at 16 and all threads. - `-If` true on 50-99% of the rows and `Nullable` without GROUP BY: 0.58-0.77 at 1 thread, 0.63-0.92 at 16 and all threads. - `-If` true on 1-10% of the rows without GROUP BY, 1-50% with GROUP BY: 0.77-1.03. - No key, consecutive values rarely repeat (the last-value cache off), String and FixedString: 0.69-1.02. - GROUP BY with repeats (ClickHouse#128190) and no key with repeats (the last-value cache): 0.80-1.06. Left out: - No key without repeats for IPv6: 1.098 on 20M distinct values after a 5-round re-run. - A `UInt8` key (`addBatchLookupTable8` is `final` in the data helper), `Nullable` with GROUP BY and `-If` with a `UInt8` key (combinator code), `-StateIf` (the `-State` inside `-If` adds one row at a time), the radix `uniqExact` (separate change), variadic `uniqExact` (ClickHouse#129435). `sip_hash_128_strings.xml` gets an `IPv6` table and these shapes, including `-If` true on 50% and on 1% of the rows, and no key without repeats. Related: ClickHouse#129436 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The route of the radix partitioned `uniqExact` hashed each `String` of 16 bytes or more, and each `FixedString` longer than 16 bytes (other than 32), with one serial `sipHash128Padded` per row. Now the rows of a batch of 256 that need a hash are collected and hashed together by the vector code of ClickHouse#129204 (`SipHashStrings::hashRows` for `String`, `hashGroup` for `FixedString`), on x86 with AVX2 and aarch64 with NEON. The keys are unchanged (the same `SipHash`, with `LONG_STRING_KEY_BIT` for strings), and so is their order: the rows of a batch from its first long row on wait for the batch to be hashed, so the inserts, the recent-keys cache and the statistics see the same sequence of keys as before. Shorter strings stay packed and are routed by the same loop as before until the first long row of a batch. The hashing lambdas are out of line and capture by value: inlined, their vector constants were kept in registers across the routing loops and reloaded after every `memcmp` call of the duplicate check (+8% instructions on 64 repeated long strings), and the by-reference captures made the loops reload the column pointers from the stack. Debug builds cross-check every batched hash against `sipHash128Padded`. Builds without AVX2 or NEON keep the per-row hashing. Measured on Xeon 6975P-C, default settings, 1 thread: 16-64 byte strings 0.87x, 0-300 bytes 0.66x, `FixedString(64)` 0.74x of the time; strings of up to 15 bytes +3% to +9% instructions, within 1-2% of the time. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #128972
Related: #129328
sipHash128hashed aStringcolumn one row at a time. Inside one string SipHash is a serial chain of rounds, so most of the CPU’s width sat idle.Now it hashes 8 rows side by side.
For a user:
sipHash128is faster onStringandFixedStringcolumns. It is also faster onNullable,LowCardinality,Array,MapandTupleof them, because it hashes theStringandFixedStringcolumns inside them. The hashes do not change.sipHash128Keyed,sipHash128ReferenceandsipHash128ReferenceKeyedare unchanged. So are constant arguments, and the types that are serialized before hashing.Sample queries,
clickhouse localon m8g.24xlarge (Graviton 4),max_threads = 1, median of 5 runs:strings_0_15strings_16_64strings_0_300strings_1000_2000fixed_strings_8fixed_strings_16fixed_strings_64The same query over 100M rows in one
MergeTreepart, master → this PR, bymax_threads:FixedString(16)FixedString(64)String, 0-63 bytesAt 96 threads a query takes less than 80 ms, so the fixed cost of running a query weighs more.
Reading order:
FunctionAnyHash::executeString, withhashes_many_stringsandsetHashes->sipHash128PaddedStrings->SipHashStrings::hashRows->SipHashStrings::hashGroup->LaneStatesandScalarState->sipHash128PaddedFixedStrings.How it works:
Stringcolumn is hashed in blocks of 256 rows, and a block can first be sorted by the number of words in each string. A group then gets strings of similar lengths.FixedStringcolumn has one length, so it needs no second step and no sort. Every 8 neighbouring rows form a group, and the last rows that do not fill a group are hashed one at a time.ColumnStringandColumnFixedStringcovers both.sipHash128of several arguments, or of aTuple, combines the hash of each later column with the hashes so far. The batched hashes of such a column go to a temporary array first, and are then combined row by row.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
sipHash128is faster onStringandFixedStringcolumns, and onNullable,LowCardinality,Array,MapandTupleof them: it hashes 8 values at a time with NEON on aarch64 and AVX2 on x86. The hashes do not change.Workflow [PR]
Sync PR [sync-upstream/pr/129204]
Version info
26.11.1.1388-master(included in26.11and later)