Repository navigation
Speed up stringBytesEntropy on short strings - #132415
Algunenano merged 1 commit into
Conversation
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]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings in each of 2 rounds) plus a separate cold review (1 finding).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261117-161200 |
|
cc @nihalzp @antaljanosbenjamin, could you review this? stringBytesEntropy zero-filled and scanned all 256 byte counters for every row; it now scans only the row's [min_byte, max_byte] range and resets the counters it reads, with bit-identical results (perf test query 1: 3.1 s to about 0.2 s on amd). |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261117-135600 |
|
Workflow [PR], commit [6791712] Summary: ✅
AI ReviewSummaryPR #132415 reduces per-row overhead in Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline. |
18ca134
Related: #129258
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
stringBytesEntropyis faster on short strings: it now scans only the range of byte values that occur in the string instead of all 256 (about 15 times faster for 1-byte strings, 1.5-2.2 times for short ASCII strings such as numbers, host names and hex hashes). The results are unchanged.Description
Query 1 of the
string_bytes_functionsperformance test (sum(stringBytesEntropy(s))over 256M strings of 0 or 1 byte) takes 3.1 s on amd and 2.5 s on arm per master commit, the longest one left in #129258. All these strings have entropy 0, so the query measures only the per-row fixed cost: for each row,StringBytesEntropyImpl::processzero-filled a 1 KiB counter table and then scanned all 256 counters, whatever the string's length.The function now finds the smallest and largest byte of the row in a separate loop (which vectorizes) and scans only that range of counters, in the same ascending order. The counters live in the
Implfor oneexecuteImplcall, as instringBytesUniqsince #125680, and the scan resets each counter it reads, so nothing is cleared between rows. The same nonzero terms are added in the same order, so results are bit-identical.Validation (release build; ratios are time PR / master):
NullableandLowCardinality: the output is bit-identical to master's.toString(number)0.45, host names 0.69, 32-char hex 0.58, 1 KB text 0.79, random 16 and 64 bytes 0.92 and 0.98, random 4 KiB, 64 KiB and 4 MiB 0.94, 0.95 and 1.02. Long strings that use nearly all 256 byte values gain nothing from the narrower scan and pay for the extra pass.perf.pyover the unchanged test, both seat orders: query 1 0.066 (2.5 s to 0.17 s), queries 0, 2 and 3 0.97-1.00.The performance test is unchanged: its query 1 goes from about 3.1 s to 0.2 s on amd (arm not measured locally).
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132415]
Version info
26.11.1.2757-master(included in26.11and later)