Skip to content

Keep the UTF-8 comparison iterators uninitialized under -ftrivial-auto-var-init - #3

Open
groeneai wants to merge 1 commit into
ClickHouse:ClickHouse/release-78.1from
groeneai:uninitialized-utf8-compare-iterators
Open

groeneai wants to merge 1 commit into
ClickHouse:ClickHouse/release-78.1from
groeneai:uninitialized-utf8-compare-iterators

Conversation

@groeneai

Copy link
Copy Markdown

ClickHouse builds with -ftrivial-auto-var-init=zero in ClickHouse/ClickHouse#127909 and compares strings with ucol_strcollUTF8(). With the flag, every comparison that leaves the Latin fast path first zero-fills its two collation iterators (400 bytes each for UTF8CollationIterator, 496 for FCDUTF8CollationIterator, mostly the inline CE buffer). ORDER BY ... COLLATE on Cyrillic text is +16% in the collations performance test on arm_release.

The constructors set all iterator state that is read before it is written, and the CE buffer is only read below its length, so this marks the four iterators of the UTF-8 RuleBasedCollator::doCompare() with __attribute__((uninitialized)) (empty where unsupported). The identical-level NFD iterators are left as they are. Builds without the flag are unchanged.

On a local amd64 release build, ORDER BY Title DESC COLLATE 'tr' LIMIT 1000, 10 over 2M Cyrillic strings goes from 0.131 s to 0.113-0.119 s, and sorted output is identical for 8 collations (including en-u-kk-true and sv-u-ks-identic). ClickHouse/ClickHouse#127909 pins this commit so that its Performance Comparison measures it on both architectures.

…o-var-init

ClickHouse builds with -ftrivial-auto-var-init=zero and compares strings with
ucol_strcollUTF8(). With the flag, every comparison that leaves the Latin fast
path zero-fills its two collation iterators first: 400 bytes each for
UTF8CollationIterator, 496 for FCDUTF8CollationIterator, mostly the inline CE
buffer. ORDER BY ... COLLATE on Cyrillic text was +15% on aarch64 in the
ClickHouse performance test collations, and 10% slower than without these
memsets on x86-64 (ClickHouse/ClickHouse#127909).

The constructors set all iterator state that is read before it is written, and
the CE buffer is only read below its length, so mark these four iterators in the
UTF-8 doCompare() with `__attribute__((uninitialized))` (empty where unsupported).
The identical-level NFD iterators are left as they are. Builds without the flag
are not affected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 28, 2026
…init=zero`

The remaining arm_release slowdowns on this PR come from contrib code:
`codecs_float_select` ClickHouse#11 (+24%) and `ip_trie` ClickHouse#2 (+18%) decode `ZSTD`, and
`collations` ClickHouse#1 (+16%) compares strings with ICU.

- contrib/zstd -> ClickHouse/zstd#6 (0c4a7b5): `seqState`, the
  `sequences` ring and the decoded `seq_t` temporaries of the sequence decoders.
  `ZSTD_decompressSequences`, `...SplitLitBuffer` and `...Long` then compile to
  the same code as without the flag on aarch64 and x86-64.
- contrib/icu -> ClickHouse/icu#3 (e16b61b): the two collation
  iterators of the UTF-8 `RuleBasedCollator::doCompare`, 400 or 496 bytes each,
  zero-filled on every comparison that leaves the Latin fast path.

Local amd64 release builds, flag only vs with these annotations (max_threads=1,
3 x 7 runs): ORDER BY ... COLLATE 'tr' on 2M Cyrillic strings 0.131 s -> 0.113-0.119 s;
the ZSTD Float64 scan returns to 0.183 s, as without the flag (0.164 s with it).
Query results are identical (8 collations, zstd levels 3 and 19 with a 128 MiB window,
a ZSTD MergeTree column).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
motsc
motsc previously approved these changes Oct 29, 2026
@motsc
motsc dismissed their stale review October 29, 2026 15:02

AI-generated approval submitted from @motsc without explicit per-PR authorization or disclosure; withdrawn so an independent maintainer can review.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants