Skip to content

Speed up stringBytesEntropy on short strings - #132415

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/string-bytes-entropy-byte-range
Nov 18, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/string-bytes-entropy-byte-range

Conversation

@groeneai

@groeneai groeneai commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #129258

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

stringBytesEntropy is 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_functions performance 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::process zero-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 Impl for one executeImpl call, as in stringBytesUniq since #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):

  • 200,000 generated strings (lengths 0-4999, byte ranges including 0x00 and 0xFF) and edge rows, at block sizes 1, 7 and 65409, plain, Nullable and LowCardinality: the output is bit-identical to master's.
  • One thread, per row: 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 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.py over 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

  • Merged into: 26.11.1.2757-master (included in 26.11 and later)

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 ⚠️ Long strings that span nearly all 256 byte values were not timed, and for them the extra min/max pass is pure overhead AGREE, fixed Timed random 4 KiB / 64 KiB / 4 MiB strings at 0.94 / 0.95 / 1.02 of master's time; the numbers and a sentence on long strings are in the description

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261117-161200

@groeneai

Copy link
Copy Markdown
Collaborator Author

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).

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. sum(stringBytesEntropy(s)) over 0/1-byte strings costs about 77 ns per row on master on every run: 32M rows single-threaded took 2.42-2.46 s in 11 of 11 runs (release build), and query 1 of the perf test took 2.43-2.81 s in 30 of 30 perf.py runs. With the change: 0.154-0.194 s and 0.159-0.192 s, no overlap.
b Root cause explained? For every row, StringBytesEntropyImpl::process zero-filled a 1 KiB counter table and then scanned all 256 counters with a branch each, so each call paid a fixed cost whatever the string's length. On 0/1-byte strings that fixed cost is the whole cost.
c Fix matches root cause? Yes. The scan covers only the row's byte range [min_byte, max_byte], and the table stays zero because the scan resets every counter it reads. Nothing is skipped or guarded, and no test changed.
d Test intent preserved / new tests added? The perf test is unchanged, and its query 1 is the CI check for this change. Results are pinned by the unchanged 03442_string_bytes_functions and 03593_funcs_on_empty_string (40/40 OK, randomized settings) and by a bit-exact differential against master. No new test: the change preserves behaviour, and an existing perf query already exercises this path.
e Both directions demonstrated? Yes. In the single-thread matrix, query-1 rows take 0.064 of master's time. Two perf.py passes, both seat orders: query 1 at 0.066 of master in each. The differential output is byte-identical to master's, and two broken variants (no reset; < instead of <=) each change 366k-407k of its 460k lines.
f Fix is general across code paths? Yes. FunctionStringBytes has exactly two implementations. stringBytesUniq lost its per-row fixed cost in #125680, and this change removes the entropy one, so the family is covered.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. The differential covers plain, Nullable (with NULLs), LowCardinality, constant and materialize inputs; lengths 0 to 4999; empty strings; bytes 0x00 and 0xFF; one distinct byte and all 256; block sizes 1, 7 and 65409. The timing matrix spans 1-byte strings to 4 MiB random binary: short strings 0.06-0.98, random 4 KiB / 64 KiB / 4 MiB 0.94 / 0.95 / 1.02.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change, and the results are bit-identical.
i Invariants and contracts preserved? Every counter is zero when process is entered. The table is value-initialized once per executeImpl call, every incremented entry lies in the scanned range and is reset by the scan, the size == 0 return counts nothing, and process cannot throw. The sum adds the same terms in the same order as before.

Session id: cron:clickhouse-impl-slot-7:20261117-135600

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6791712]

Summary: ✅


AI Review

Summary

PR #132415 reduces per-row overhead in stringBytesEntropy by reusing byte counters within each execution and scanning only the input row’s byte range. The supplied before/after benchmarks and differential results support the performance and compatibility claims. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Nov 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 67917127c with master a604b7c42 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@Algunenano Algunenano self-assigned this Nov 18, 2026
@Algunenano
Algunenano added this pull request to the merge queue Nov 18, 2026
Merged via the queue into ClickHouse:master with commit 18ca134 Nov 18, 2026
385 of 386 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants