Skip to content

Potentially faster FixedString compare - #109381

Closed
4ertus2 wants to merge 10 commits into
ClickHouse:masterfrom
4ertus2:faster-compare-fixstr16
Closed

4ertus2 wants to merge 10 commits into
ClickHouse:masterfrom
4ertus2:faster-compare-fixstr16

Conversation

@4ertus2

@4ertus2 4ertus2 commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

A PR to check if it would be faster to compare FixedStrings(x*16) with memcmpSmallMultipleOf16

Changelog category (leave one):

  • Performance Improvement

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

slightly faster FixedString(x*16) compare

@4ertus2 4ertus2 changed the title potentially faster FixedString comparison Potentially faster FixedString comparison Jul 4, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [13978c0]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, parallel) FAIL
04061_spilling_hash_join_overflow_limits FAIL cidb

AI Review

Summary

This PR switches FixedString(N) ordering comparisons to memcmp16 / memcmpSmallMultipleOf16 when N is a multiple of 16 and adds a focused performance benchmark. The main non-JIT comparison paths are wired up correctly, but the default compiled ORDER BY path still calls the old helper, and the Performance Improvement claim is still unproven because there are no before/after results yet.

Findings

⚠️ Majors

  • [src/Columns/ColumnFixedString.cpp:212-231, src/Core/Settings.cpp:1190-1194, src/Interpreters/JIT/compileFunction.cpp:736] [dismissed by author -- https://github.com/Potentially faster FixedString compare #109381#discussion_r3522384282] The PR promises faster FixedString(x*16) sorting, but repeated ORDER BY under default settings still JITs ColumnFixedString::compileComparator, and that comparator still hardwires memcmpSmallCharsAllowOverflow15 instead of the new multiple-of-16 dispatch. compile_sort_description is enabled by default, so warmed sort workloads can miss the claimed speedup even though the interpreted compareAt path was updated. The new benchmark does not close this gap because perf-comparison runs disable sort-description JIT in tests/performance/scripts/config/users.d/perf-comparison-tweaks-users.xml:21-24. Suggested fix: route compileComparator through the same n == 16 / n % 16 == 0 fast path, or narrow the PR scope/changelog/benchmark to exclude compiled sorts explicitly.
Tests
  • ⚠️ The Performance Comparison shards for SHA 13978c0548be3d3382e4f0dcae825b50d4092126 are still PENDING, and the PR description still has no before/after numbers. The added tests/performance/fixed_string_comparison_x16.xml benchmark is the right vehicle, but the Performance Improvement contract is not satisfied until it produces measurements. The smallest evidence that closes this is one set of before/after timings from that benchmark (or the CI perf shards) for the changed column-vs-column, column-vs-constant, and ORDER BY queries.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: Provide performance evidence for the claimed speedup (or wait for the perf-comparison results and reference them).
  • Minimum required action: Either optimize the compiled sort-description comparator too, or narrow the PR claim so it does not promise faster repeated JIT-compiled ORDER BY.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 4, 2026
Comment thread src/Columns/ColumnFixedString.h Resolved
@4ertus2 4ertus2 changed the title Potentially faster FixedString comparison Potentially faster FixedString compareAt Jul 4, 2026
@4ertus2 4ertus2 changed the title Potentially faster FixedString compareAt Potentially faster FixedString compare Jul 4, 2026
else
{
for (size_t i = 0, j = 0; i < size; i += a_n, ++j)
c[j] = Op::apply(memcmpSmallMultipleOf16(a_data.data() + i, b_data.data() + i, a_n), 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is filed as a Performance Improvement, but there is still no evidence that the new multiple-of-16 branch is a net win on the FixedString(32) / FixedString(48) / FixedString(64) workloads it touches here. The generic Performance Comparison shards are still PENDING, and there is no focused benchmark in the PR description, so the claimed speedup is currently unproven. Please attach before/after numbers (or a small benchmark) for the exact compare paths changed here before we merge under a performance-improvement contract.

@alexey-milovidov alexey-milovidov self-assigned this Jul 4, 2026
Address review: the previous perf test built its `FixedString(32/64/128)`
values with `toFixedString(reinterpretAsFixedString(number), N)`, which only
fills 8 live bytes and zero-pads the rest. Both operands therefore differed
within the first 8 bytes, so `memcmpSmallMultipleOf16` returned after its first
16-byte block and the multi-block path this PR optimizes was never exercised.

Rewrite the test to store the values in `MergeTree` tables (so the heavy string
construction happens once in the fill query and the measured queries are
dominated by the comparison), give every value a long shared `_` prefix and
keep the varying part in the last 16-byte block, and add equal-string and
`ORDER BY` queries. This exercises both changed surfaces: `StringComparisonImpl`
(`<` / `>`) and `ColumnFixedString::compareAt` (sorting). `=` / `!=` are left
out on purpose because they use `memequalSmallAllowOverflow15`, which this PR
does not change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Functions/FunctionsComparison.h Resolved
Address review (ClickHouse#109381):
`StringComparisonImpl` routes both its column-vs-column branch and its
column-vs-constant branch (`WHERE a < '...'`, the `a_n == b_size` case in
`FunctionsComparison.h`) through `memcmpSmallMultipleOf16` for `FixedString(x*16)`,
but the perf test previously exercised only the column-vs-column path and `ORDER BY`.

Add `<` / `>` queries comparing each column against a `FixedString(N)` constant that
shares the same `_` prefix, so the comparison scans every 16-byte block before the
difference in the last one. Verified with `EXPLAIN actions` that `less` receives a
`FixedString(N)` column and a `Const(FixedString(N))` (the `vector_constant` branch
this PR changes) and that every query runs in `clickhouse-local`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The `n == 16 -> memcmp16` fast path added to `ColumnFixedString::compare`
is only reachable through `compareAt` and the sort comparator, i.e. the
`ORDER BY` surface. The `<` / `>` queries do not exercise it: for
`FixedString(16)` `StringComparisonImpl` dispatches to
`memcmpSmallMultipleOf16` directly rather than through
`ColumnFixedString::compare`. The `ORDER BY` block previously started at
`FixedString(32)`, so this branch was left unmeasured.

Add `SELECT a FROM fixstr_cmp_16 ORDER BY a FORMAT Null` so the
`n == 16 -> memcmp16` branch of `compare` is benchmarked, as requested in
the review.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 85.60% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.80% 77.80% +0.00%

Changed lines: Changed C/C++ lines covered: 20/42 (47.62%) · Uncovered code

Full report · Diff report

@4ertus2

4ertus2 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

I've checked it locally for sizes 32 and 128. There's no difference at all.

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 pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants