Repository navigation
Conversation
… mask `StringBytesUniqImpl::process` kept a 32-byte `UInt64 mask[4]` on the stack and, for every row, zeroed it with one 32-byte store, did a load-or-store per byte and reloaded all 32 bytes for the popcount. On aarch64 the speed of that per-row store/reload depends on the alignment of `mask`, which is decided by the stack depth at the call, i.e. by unrelated caller frames: `executeImpl` is identical in 8 arm builds, 4 fast and 4 slow, and the slow ones are exactly those where `mask` lands 32-byte aligned (depth at entry mod 32 = 16). ClickHouse#124599 (thread pool worker frame 592 -> 608 bytes) flipped master to fast, ClickHouse#112705 (`ExpressionTransform::transform` 400 -> 480 bytes) flipped it back to slow, so `string_bytes_functions #0` runs at 0.050 or 0.061 s per arm binary and is reported `slower` on PRs whose binary draws the other mode than its reference (21 PRs plus master in 30 days on arm_release, none on amd_release). Count with a per-call table of the last row number in which each byte value occurred instead. It is zeroed once per `executeImpl` call and never cleared between rows, so the only per-row stack traffic left is the 8-byte, 8-aligned load and store of the table entry per byte. `alignas` on `mask` was rejected: the 32-byte-aligned placement is the slow one, so it would pin every build to the slow mode. A register-resident mask was measured 1.2-1.85x slower on x86. `FunctionStringBytes::executeImpl` now calls `process` on a local `Impl`, and `StringBytesEntropyImpl::process` becomes a `const` member; entropy's machine code is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y` as is Entries of `last_seen_row` never exceed the current row number, so a byte is new in this row exactly when its entry is smaller. On x86 the byte loop becomes 7 instructions instead of 9 (`cmp` + `adc` instead of `xor` + `setne` + `add`), the same count as the old bit mask, which removes the slowdown the `!=` form had on random binary strings. The `stringBytesEntropy` hunk is not needed: `impl.process` calls the static member, and clang-tidy does not flag it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The existing queries only use 0/1-byte strings, so CI did not measure the byte loop. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the design note from the `last_seen_row` comment, keep its invariant. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) plus my own review (two rounds, nits only).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20261013-201000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261013-191000 |
|
Workflow [PR], commit [8ab0812] Summary: ✅
AI ReviewSummaryPR #125680 replaces the per-row bitmask in Missing context / blind spots
Final VerdictStatus: ✅ Approve. No review changes required. |
|
📊 Cloud Performance Report 🟢 AI verdict: This PR only rewrites stringBytesUniq, replacing per-row bitmasks with a last-seen-row table reused across rows, and adds tests for it. None of the ClickBench or TPC-H queries call that function. ClickBench Q20 still shows an improvement (-44.1%, CPU time roughly halved). The verdict is fixed by the large-change rule, but this diff cannot cause it, so it most likely reflects a build or baseline difference and should not count as a gain from this PR. Q28 is borderline, and Q33/Q34 at -6.8% sit inside master's normal variance band. Neither result is linked to this change, so the PR's actual effect should be judged with the string_bytes_functions perf test it adds. clickbench🟢 1 improved · Flagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. 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
|
CI finish ledger - 8094f27Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261014-040009 |
|
@groeneai Merge from master. |
|
Merged master in 62bc0ca (no conflicts; the diff against master is unchanged). |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units8 translation units recompiled, 9 s compile time in total, 8 of them have a recent master baseline. |
| UInt64 mask[4] = {0}; | ||
| const UInt8 * end = data + size; | ||
| /// For each byte value, the number of the last row that contained it (never greater than `row`). | ||
| std::array<UInt64, 256> last_seen_row{}; |
There was a problem hiding this comment.
Can we not change this to an array of 256 bits (unsigned char)?
There was a problem hiding this comment.
A 256-bit mask has to be cleared for every row, and that per-row 32-byte stack array is what made the arm speed depend on its alignment. So each entry holds a row stamp instead, and nothing is cleared between rows.
I tried std::array<UInt8, 256> stamps (cleared every 255 rows): same results, but on an x86 release build (1 thread, median of 30) it is slower than UInt64 on longer strings and equal on short ones:
| strings | UInt64 |
UInt8 |
|---|---|---|
| text, ~1 KB | 0.574 s | 0.604 s |
| random, 16 B | 0.250 s | 0.277 s |
| random, 256 B | 0.369 s | 0.379 s |
So I kept UInt64 (2 KiB on the stack per call). If you prefer the smaller array anyway, I'll switch.
03442_string_bytes_functions is back to master's version, as asked. The table stays std::array<UInt64, 256>. A 256-bit mask has to be cleared for every row, which is the per-row clear this change removes. A std::array<UInt8, 256> of row stamps (cleared every 255 rows) gives the same results, but on x86 release (median of 30, 1 thread) it is 5-10% slower than UInt64 for 16 B to 1 KB strings: text ~1 KB 0.574 -> 0.604 s, random 16 B 0.250 -> 0.277 s, random 256 B 0.369 -> 0.379 s; short strings are the same. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
This isn't totally terrible, thanks. |
For every row, `StringBytesEntropyImpl::process` zero-filled a 1 KiB counter table and then scanned all 256 counters with a branch each, so every call paid about 70 ns whatever the string's length. Query 1 of the `string_bytes_functions` performance test (256M strings of 0 or 1 byte, all of entropy 0) measures only that cost: 3.1 s on amd and 2.5 s on arm per master commit. The row's smallest and largest byte are now found in a separate loop, which vectorizes, and the result scan covers only that range, in the same ascending order. The counters are a member of the `Impl`, zeroed once per `executeImpl` call as in `stringBytesUniq` since ClickHouse#125680, and the scan resets every counter it reads, so the table is zero again before the next row. The skipped counters were zero and never reached the sum, so the same terms are added in the same order and the result is bit-identical (checked against master on 200,000 generated strings plus edge rows, at block sizes 1, 7 and 65409, plain, `Nullable` and `LowCardinality`). Release build, single thread, time PR / master: 0/1-byte strings 0.064, `toString(number)` 0.45, host names 0.69, 32-char hex 0.58, 1 KB text 0.79, random 16 / 64 bytes 0.92 / 0.98, random 4 KiB / 64 KiB / 4 MiB 0.94 / 0.95 / 1.02, `stringBytesUniq` 0.99. `perf.py` over the unchanged test, both seat orders: query 1 0.066, queries 0, 2 and 3 0.97-1.00. Long strings that use nearly all 256 byte values gain nothing from the narrower scan and pay for the extra pass. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #115988
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Function
stringBytesUniqis up to 2.3 times faster on long strings.Description
string_bytes_functionsquery #0 (SELECT sum(stringBytesUniq(s))) runs at one of two speeds per arm binary, 0.050 or 0.061 s, so it isslowerwhenever a PR's binary draws the other mode than its reference: 21 arm PRs plus master in 30 days, never on amd, e.g. #115988 perf 5/6.StringBytesUniqImpl::processkeepsUInt64 mask[4]on the stack and, for every row, zeroes it with one 32-byte store, sets bits with 8-byte stores, then reloads it with one 32-byte load. Its machine code is identical in 8 arm builds; the 4 slow ones are exactly those where the stack depth at entry makes the mask 32-byte aligned, which unrelated caller frames change (#124599, #112705).The change keeps, per call, the last row number that contained each byte value. A byte is new in a row exactly when its entry is smaller than the row number, so nothing is reset between rows. The only stack accesses left are an aligned 8-byte load and store per byte (checked in the aarch64 object).
x86 release build (Xeon 6975P), median of 30 runs, speed-up vs master:
stringBytesEntropy(unchanged) 0.99x. Tests: two single-threaded long-string queries (text, random 64 B) in thestring_bytes_functionsperf test. This PR's arm perf comparison: query #0 0.063 -> 0.035 s, text 0.71 -> 0.29 s, random 64 B 0.24 -> 0.17 s.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125680]
Version info
26.11.1.1998-master(included in26.11and later)