Skip to content

Fix ARM performance regressions in indexOf, skipASCIIBlocks and computeWidth - #123377

Merged
Algunenano merged 5 commits into
ClickHouse:masterfrom
Algunenano:fix-arm-perf-regressions
Oct 2, 2026
Merged

Algunenano merged 5 commits into
ClickHouse:masterfrom
Algunenano:fix-arm-perf-regressions

Conversation

@Algunenano

@Algunenano Algunenano commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Fix three ARM performance regressions from recent vectorization changes. Each fix is a separate commit, with no intrinsics, and adds a short performance test.

  • indexOf with a constant needle: findInBlocks returned the rescan from inside the block loop, which kept clang from vectorizing the reduction for 4- and 8-byte elements on ARM. Now the loop only breaks and the rescan happens after it, except on x86, where returning from the loop is unrolled into branchless code that reuses the comparisons. The x86 code of indexOf and has is identical to master.
  • skipASCIIBlocks: in non-ASCII text the 64-byte check almost always failed and the 16-byte check ran after it. Now one 16-byte block is checked first.
  • computeWidth: the block skip ran at every byte of every multi-byte character, where it cannot advance, and building the mask is expensive on ARM. Now it runs only at a printable ASCII byte.

clickhouse local on Neoverse-V2, max_threads = 1, best of 9, master -> this PR:

Case Master PR
indexOf, Array(UInt32) of 24, hit in 8..15 72 ms 52 ms
indexOf, Array(UInt32) of 24, hit at 9 48 ms 26 ms
indexOf, Array(Int64) of 16, hit in 8..15 58 ms 53 ms
FORMAT XML, accented Latin, 300000 rows of 810 bytes 1.14 s 0.89 s
FORMAT XML, Cyrillic, 300000 rows of 1 KB 0.96 s 0.79 s
JSONEachRow with output_format_json_validate_utf8 = 1, Cyrillic 1.05 s 0.88 s
toValidUTF8 of 63 é and an a, 2M rows 0.57 s 0.52 s
visibleWidth, 149 bytes of Cyrillic, 2M rows 4.48 s 3.42 s
visibleWidth, 90 bytes of CJK, 2M rows 2.27 s 1.50 s
PrettyCompactNoEscapes, 10000 rows of CJK 41 ms 27 ms

ASCII inputs are unchanged. The x86 code of skipASCIIBlocks and computeWidth callers changes too (verified by diffing the assembly of the real translation units), but it was not benchmarked on x86. CI showed about +6% on to_valid_utf8 on x86 as well.

Closes: #123338
Closes: #123346
Closes: #123351

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/123377]

Version info

  • Merged into: 26.10.1.1339-master (included in 26.10 and later)

Algunenano and others added 3 commits October 2, 2026 00:46
`NumericArrayIndex<IndexOfAction, T>::findInBlocks` returned the rescan of the
matching block from inside the block loop. That kept clang from vectorizing the
`found |=` reduction for 4- and 8-byte elements on ARM, so `indexOf` ran a
scalar prefix, a scalar block reduction and a scalar rescan, which is slower
than the scalar loop it replaced. `has` was not affected.

Now the loop only breaks, and the block or the tail is rescanned after it.
`NumericArrayIndex<IndexOfAction, T>::vector` for `Int32`, `UInt32`, `Int64`,
`UInt64`, `Float32` and `Float64` now has the same NEON compares as `has`. The
`has` code is unchanged on both x86 and ARM. On x86 the `indexOf` block loop
stays the same, and the unrolled rescan becomes a short loop.

`clickhouse local` on Neoverse-V2, `max_threads = 1`, best of 9, 4M rows:
- `Array(UInt32)` of 24, hit in 8..15: 72 -> 52 ms
- `Array(UInt32)` of 24, hit at 9: 48 -> 26 ms
- `Array(Int64)` of 16, hit in 8..15: 58 -> 53 ms

Add a short-array case to the `array_index_simd_numeric` performance test.

Closes: ClickHouse#123338

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`WriteBufferValidUTF8::nextImpl` and `writeValidUTF8` call `skipASCIIBlocks`
once per sequence they step over. In non-ASCII text the next such byte is
usually within 64 bytes, so the 64-byte check failed and the 16-byte check ran
after it: two vector checks per step where the old SSE2 and NEON loops did one.

Now one 16-byte block is checked first, and the function returns at once if it
has a byte of `0x80` or more. The result is the same. Long ASCII runs pay one
extra 16-byte check.

`clickhouse local` on Neoverse-V2, `max_threads = 1`, best of 9:
- `FORMAT XML`, accented Latin, 300000 rows of 720 bytes: 1.14 -> 0.89 s
- `FORMAT XML`, Cyrillic, 300000 rows of 1 KB: 0.96 -> 0.79 s
- `JSONEachRow` with `output_format_json_validate_utf8 = 1`, Cyrillic: 1.05 -> 0.88 s
- `toValidUTF8` of 63 `é` and an `a`, 2M rows: 0.57 -> 0.52 s
- ASCII `FORMAT XML` and `toValidUTF8`: unchanged

The x86 code of the two callers changes too: CI showed about +6% on
`to_valid_utf8` there as well.

Add a non-ASCII `XML` case to the `writing_valid_utf8` performance test.

Closes: ClickHouse#123346

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The block skip in `computeWidthImpl` ran on every outer iteration outside an
escape sequence. That includes every byte of every multi-byte character,
because the decoder takes one byte per iteration, and there the skip cannot
advance at all. Since the skip is enabled on every little-endian target, ARM
builds the 32-byte mask for nothing at each such byte, and without `movemask`
that mask takes several instructions.

Now the skip runs only when the current byte is printable ASCII. The result is
the same, since the skip stops at the first byte that is not.

`clickhouse local` on Neoverse-V2, `max_threads = 1`, best of 9, 2M rows:
- `visibleWidth` of 140 bytes of Cyrillic: 4.48 -> 3.42 s
- `visibleWidth` of 90 bytes of CJK: 2.27 -> 1.50 s
- `visibleWidth` of 80 bytes of accented Latin: 0.79 -> 0.68 s
- `PrettyCompactNoEscapes` of 10000 rows of CJK: 41 -> 27 ms
- ASCII and numbers: unchanged

Add a `visible_width` performance test.

Closes: ClickHouse#123351

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Algunenano Algunenano added the pr-performance Pull request with some performance improvements label Oct 2, 2026
@Algunenano Algunenano self-assigned this Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fd6dd3c]

Summary: ✅


AI Review

Summary

This PR reshapes three hot-path fast paths to recover ARM performance in indexOf, skipASCIIBlocks, and computeWidth without changing their observable behavior. I read the current diff, the touched callers, and the only prior review thread; the follow-up skipASCIIBlocks fix addresses the earlier ASCII-blocking concern, and I do not see a remaining correctness, safety, or high-confidence performance issue in the current head.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit fd6dd3c.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 15/16 (93.75%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Oct 2, 2026
Comment thread src/IO/writeValidUTF8.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fd6dd3cfd with master 918d3af67 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 49a47ff77; 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

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

Job report

Skipping the first 16-byte block shifted the 64-byte blocks. For pure ASCII
whose length is `64 * k` to `64 * k + 15` the loops then checked three extra
16-byte blocks: four checks instead of one for 64 bytes. Now the loops check
the first block again, which costs one 16-byte check for every length. The
gains on non-ASCII text stay the same.

Related: ClickHouse#123377 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Algunenano
Algunenano marked this pull request as ready for review October 2, 2026 07:20
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 queries analysed

I marked both flagged improvements as not sure: tpch_adapted_1_official Q1 (-20.0%) and Q7 (-17.3%). This PR changes three things: the numeric has/indexOf block search, visibleWidth's ASCII fast path, and the ASCII block skip used when writing valid UTF-8 output. Q1 and Q7 are an aggregation and a join that return only a few rows, so they run none of that code. Both queries have a noisy history on master, so these shifts are most likely normal run-to-run variation. Nothing was suppressed by the master variance band, and the era-split refit only applied to clickbench Q29, which was not flagged.

clickbench

🟢 No significant changes

tpch_adapted_1_official

⚠️ 2 inconclusive

Flagged queries (2 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 1 not_sure 65 52 -20.0% <0.0001 aggregation: Q1 is a scan-and-aggregate over lineitem with a few output rows, so it never runs has/indexOf, visibleWidth or bulk UTF-8 output. The query also has a noisy history and widely spread timings.
⚠️ 7 not_sure 81 67 -17.3% <0.0001 join_operator: Q7 is a multi-table join plus aggregation with a small result, so none of the changed array-search or UTF-8 width/escaping code runs. The query also has a noisy history.

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.

Debug info
  • StressHouse run: 19a8f98f-6f9b-485c-a1e9-d7b7657084fa
  • MIRAI run: 378c8724-c8e1-4162-a33a-a90f9185eecb
  • PR check IDs:
    • clickbench_2899818_1790946688
    • clickbench_2899824_1790946688
    • clickbench_2899835_1790946688
    • tpch_adapted_1_official_2899860_1790946689
    • tpch_adapted_1_official_2899874_1790946688
    • tpch_adapted_1_official_2899888_1790946689

On x86, returning the rescan of the matching block from inside the block loop
is unrolled into branchless code that reuses the comparisons of the block.
Rescanning after the loop made it a loop whose exit is mispredicted when the
match position varies: `array_index_simd_numeric` #2 got 36% slower on amd.
Return from inside the loop on x86 again, so its code is the same as before
this PR. Other targets keep the `break`, which ARM needs to vectorize the block
loop for 4- and 8-byte elements (the same query got 25% faster on arm).

Related: ClickHouse#123377

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Algunenano
Algunenano added this pull request to the merge queue Oct 2, 2026
Merged via the queue into ClickHouse:master with commit c662045 Oct 2, 2026
149 checks passed
@Algunenano
Algunenano deleted the fix-arm-perf-regressions branch October 2, 2026 22:05
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-not-for-changelog This PR should not be mentioned in the changelog pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

2 participants