Skip to content

Vectorize <, >, <=, >= on IPv6 and FixedString(16) for CPUs with AVX-512 - #125151

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/vectorize-ipv6-fixedstring16-ordered-compare-avx512
Oct 11, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/vectorize-ipv6-fixedstring16-ordered-compare-avx512

Conversation

@groeneai

@groeneai groeneai commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #120193

Changelog category (leave one):

  • Performance Improvement

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

Comparisons <, >, <= and >= of IPv6 and FixedString(16) values are 1.4 to 1.8 times faster on CPUs with AVX-512.

Description

Performance Comparison (amd_release) often flags uuid_ip_comparison queries #2 (ipv6_a < ipv6_b), #3 (fs16_a < fs16_b), #10 and #11 (< (SELECT ...)) on unrelated PRs, for example this job. Over 21 days of amd runs these four move by 10% or more in about 3% of runs, the other eight queries of the test (UUID <, every =, IPv4) in at most 0.5%; on arm all twelve are quiet.

These four are the only ones whose AVX-512 kernel is not a vector compare. base/base/bigEndianCompare.h uses its vectorizable two-halves form only under __AVX512F__, which official (x86-64-v3) builds do not define, so the x86_64_v4 clone of the IPv6 kernels inlines the unsigned __int128 form and moves every lane back to general purpose registers (16 vpextrq, 17 stack accesses). The FixedString(16) kernels have no AVX-512 clone at all.

This PR adds one loop, compareBigEndian16_x86_64_v4, built for x86-64-v4 from the existing halves form, and uses it for <, >, <=, >= on IPv6 and FixedString(16) (column-column and column-constant) when the CPU has AVX-512: the follow-up scoped in #120193. It compiles to vpcmpuq without lane extraction. Code for other x86 CPUs is unchanged apart from the dispatch, and ARM is not affected.

Release builds with identical flags, each query of uuid_ip_comparison 25 times in 3 interleaved rounds on one pinned core: #2 70 -> 42 ms, #3 57 -> 42, #10 54 -> 32, #11 58 -> 32, the other eight within 2%. 05218_uuid_ip_comparison_vectorized gains FixedString(16) cases and checks all four operators against hex() strings over blocks of 1001 rows; it fails if the halves are compared as signed or swapped. I could not reproduce the CI noise itself locally, so whether it goes away will show on the dashboard after merge.


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

Version info

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

groeneai and others added 2 commits October 11, 2026 03:53
`base/base/bigEndianCompare.h` uses the two-64-bit-halves form, which
vectorizes to `vpcmpuq`, only under `__AVX512F__`, which official
x86-64-v3 builds never define. The `x86_64_v4` clone of the IPv6 ordered
comparison kernels therefore inlined the `unsigned __int128` form and
extracted every lane to general purpose registers (16 `vpextrq`, 17
stack accesses, no vector compare), and the `FixedString(16)` kernels had
no AVX-512 clone. These are exactly the four `uuid_ip_comparison`
queries (ClickHouse#2, ClickHouse#3, ClickHouse#10, ClickHouse#11) that are noisy on the amd performance runners.

Define the halves form unconditionally as `lessBigEndian16Halves` /
`compareBigEndian16Halves` (the default selection is unchanged) and run
it in one x86-64-v4 loop, `compareBigEndian16_x86_64_v4`, for `<`, `>`,
`<=`, `>=` on IPv6 and FixedString(16), column-column and
column-constant, when the CPU supports x86-64-v4. The IPv6 constant goes
through a small x86-64-v4 wrapper that takes it by value: passing `&b`
from `vectorConstant` itself added a stack frame and a stack protector
check to the default path.

Release builds, `uuid_ip_comparison` queries x25, 3 rounds, one core:
ClickHouse#2 70 -> 42 ms, ClickHouse#3 57 -> 42, ClickHouse#10 54 -> 32, ClickHouse#11 58 -> 32, the other eight
within 2%. The non-AVX-512 code is unchanged apart from the dispatch, and
ARM is not affected.

The test gains FixedString(16) cases and checks every ordered operator on
both types against `hex()` strings over blocks of 1001 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the kernel's doc comment to one line and drop the trait comment that restates its specializations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 11, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Independent model review (codex; 1 finding) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ 05218 passes whether or not the AVX-512 kernel is selected DISAGREE Speed is guarded by tests/performance/uuid_ip_comparison.xml #2, #3, #10, #11; 05218 guards results (signed halves change 56 lines, swapped halves 54).
2 💡 FunctionsComparison.h gets base/bigEndianCompare.h only through base/memcmpSmall.h AGREE, not changed memcmpSmall.h needs it for memcmp16, so the include path is stable.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-10:20261011-035600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes for the speedup (release, same flags, 3 rounds x25, pinned core: #2 70 vs 42 ms, #3 57 vs 42, #10 54 vs 32, #11 58 vs 32); the CI runner noise does not reproduce locally, so it is not claimed fixed.
b Root cause explained? The halves form is used only under __AVX512F__, never defined in x86-64-v3 builds, so the x86_64_v4 IPv6 clone extracts every lane (16 vpextrq, 0 vpcmp*uq); FixedString(16) had no AVX-512 clone.
c Fix matches root cause? Yes: one x86-64-v4 loop over the halves form, compiled to vpcmpuq with no lane extraction; no test thresholds changed.
d Test intent preserved / new tests added? 05218 gains FixedString(16) edge rows and checks all 4 operators on both types against hex() strings over blocks of 1001 rows.
e Both directions demonstrated? Speedup measured base vs fix; the test fails with the halves compared as signed (56 lines) or swapped (54 lines).
f Fix is general across code paths? All six ordered 16-byte kernels; =/!=, UUID, IPv4, other widths and other lessBigEndian16 users unchanged (disassembly compared).
g Fix generalizes across inputs (params/datatypes/wrappers)? Nullable, LowCardinality and constants are unwrapped before these kernels; 50/50 randomized runs OK.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or result change; ARM untouched.
i Invariants and contracts preserved? Same byte order as IPv6::operator< / memcmp16; new code only under isArchSupported(TargetArch::x86_64_v4); reads stay inside the input columns.

Session id: cron:clickhouse-impl-slot-6:20261011-021500

@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cd75e4f]

Summary: ✅


AI Review

Summary

This PR factors the 16-byte big-endian helper so the x86_64_v4 ordered-comparison paths for IPv6 and FixedString(16) can reuse the same byte-order-preserving comparator, and it extends 05218_uuid_ip_comparison_vectorized with FixedString(16) and hex-based correctness checks. I did not find any correctness, compatibility, or dispatch issues in the current diff.

Missing context / blind spots
  • ⚠️ I did not have a local ClickHouse build or binary in this checkout, so I could not independently disassemble the new x86_64_v4 clone or rerun the claimed benchmark/perf artifacts here. A built amd_release binary or the CI performance artifact would close that gap.
Final Verdict

✅ No review findings on the current revision.

@groeneai
groeneai requested review from Algunenano and Avogar October 11, 2026 04:21
@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 11, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Oct 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR only adds an AVX-512 path for ordered comparisons (<, >, <=, >=) of IPv6 and FixedString(16) values. No flagged ClickBench query compares values of those types. Q16, Q18, Q32 and Q34 came out between +5.4% and +23.3% slower, but each stayed inside master's normal range and was correctly left as not_sure. I see no reason to challenge those decisions. All four are aggregation-heavy queries that slowed together with higher CPU time, and two of the PR's ClickBench runs shared one host, which points to contention on the test machines rather than this change; tpch_adapted_1_official showed nothing flagged.

clickbench

⚠️ 4 inconclusive

Flagged queries (4 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 507 578 +14.0% <0.0001 aggregation: This query is a GROUP BY over UserID and SearchPhrase and never runs an ordered IPv6 or FixedString(16) comparison, so the +14.0% cannot come from this diff. It stays inside master's normal range.
⚠️ 18 not_sure 816 960 +17.7% <0.0001 aggregation: This is a three-key GROUP BY over integer and String columns, which the new 16-byte comparison kernels do not touch. The +17.7% sits inside master's band on a query that is already noisy.
⚠️ 32 not_sure 730 901 +23.3% 0.0015 aggregation: This query is a GROUP BY over WatchID and ClientIP, where ClientIP is an integer column, not IPv6. The changed comparison path is never reached, and the +23.3% is inside master's wide band.
⚠️ 34 not_sure 1020 1075 +5.4% 0.0034 aggregation: This query groups by the URL String column and does no ordered FixedString(16) or IPv6 comparison. The +5.4% is small and inside master's band.

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
  • StressHouse run: fd5253f5-e7e2-47c5-86ab-9af1d156f46c
  • MIRAI run: 20b21f37-3a88-4d3c-a9d3-b4baa120cdcd
  • PR check IDs:
    • clickbench_836836_1791699045
    • clickbench_836842_1791699045
    • clickbench_836848_1791699045
    • tpch_adapted_1_official_836855_1791699044
    • tpch_adapted_1_official_836875_1791699045
    • tpch_adapted_1_official_836907_1791699045

@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cd75e4f83 with master 46dfb8152 (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

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 11, 2026
Merged via the queue into ClickHouse:master with commit 6dd1c98 Oct 11, 2026
165 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 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... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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