Skip to content

Remove the leftover test-only failpoints and logging from the LIMIT BY loops - #127172

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:limit-by-remove-test-scaffolding
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:limit-by-remove-test-scaffolding

Conversation

@groeneai

@groeneai groeneai commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #125738 (comment)

Changelog category (leave one):

  • Performance Improvement

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

LIMIT BY is slightly faster: its per-row and per-run loops no longer contain unused test-only logging and failpoint checks, so they run fewer instructions and their speed no longer depends on how unrelated logging code is compiled.

Description

limit_by #0 is red on #125738 in Performance Comparison (amd_release, master_head, 4/6) (+28%), and optimize_limit_by_keys #0/#1, which run the same function, are 21-43% slower on all three amd builds of that PR. That PR does not touch LIMIT BY.

The per-row loop of LimitByTransform::consumeImpl contained test-only code: a LOG_TEST in the cancel branch and a row_idx == 5 failpoint. In the ThinLTO build the inlined LOG_TEST block (which calls currentThreadHasGroup) is placed before the loop body, so its size decides where the loop lands. #125738 makes currentThreadHasGroup smaller, and the loop moves 0x60 bytes earlier (same instructions, different alignment padding). The 26 other amd builds of the last 12 days have the old placement and are not slower, and the red job's CPU profile differs only in this function.

The six limit_by_* failpoints and four cancellation log lines were added in #106070 for test_limit_by_transform_kill_query, which #123165 removed, so nothing uses them. This PR deletes them from both LIMIT BY transforms and from FailPoint.cpp. The isCancelled checks stay.

Validation: in a ThinLTO release build with CI's flags, the function is now byte-identical with and without #125738's CurrentThread change, and the per-row loop runs 4 fewer instructions. Without this PR the loop moves exactly as in CI. Local release A/B (Granite Rapids, where the CI slowdown itself does not reproduce), median of 60-300 runs: limit_by #0 0.94x, optimize_limit_by_keys #0/#1 0.93x/0.95x, limit_by #1/#4 0.89x/0.90x, the other LIMIT BY perf queries unchanged within noise. The 71 limit_by stateless tests give the same results as on master.


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

…Y loops

The six `limit_by_*` pause failpoints and the four cancellation `LOG_TEST`
lines were added for `tests/integration/test_limit_by_transform_kill_query`
(ClickHouse#106070). That test was removed in ClickHouse#123165, so nothing uses them anymore.
The `isCancelled` checks themselves stay.

They are not free in a release build: ThinLTO inlines the in-loop `LOG_TEST`
block (with `currentThreadHasGroup`) and the mid-loop failpoint into
`LimitByTransform::consumeImpl`, and the size of that cold code decides where
the per-row loop lands inside the function. ClickHouse#125738 makes
`currentThreadHasGroup` smaller, which moves the loop 0x60 bytes earlier with
the same instructions; on the CI amd runners `limit_by #0` and
`optimize_limit_by_keys #0/ClickHouse#1` were 13-43% slower in all three builds of that
PR, and in none of 26 other recent builds. Without the scaffolding the loop
contains no logging, `CurrentThread` or failpoint code, and each row does one
compare and branch less (`row_idx == 5`).

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) plus my own cold review (3 nits).

# Sev Finding Verdict Evidence / action
1 💡 The changelog number was measured on a CPU where the CI slowdown does not reproduce AGREE Number dropped from the entry; the Validation sentence says where the A/B ran
2 💡 Where the fixed loop lands on the CI CPU can only be measured by this PR's own perf shards AGREE, noted The loop is now compact (function 0xc25 -> 0x38f bytes, 4 fewer instructions per row); this PR's amd Performance Comparison is the check
3 💡 integration_tests_configs.py still lists the deleted test_limit_by_transform_kill_query DISAGREE Left on purpose by the commit that deleted the test; not this PR's change

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

Session id: cron:clickhouse-review-slot-9:20261021-120600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, as a code-layout oracle: ThinLTO release builds (CI flags) of master with and without this PR's diff and #125738's CurrentThread change. Master puts the consumeImpl loop at the CI-fast offsets (cancel check +0x412, key load +0x565), master + that change at the CI-slow ones (+0x3b2, +0x505), every build. The wall-time slowdown is CPU-specific: it does not reproduce on my Granite Rapids box (equal cycles, +1 instruction per row), only on the CI's Sapphire Rapids runners.
b Root cause explained? Test-only LOG_TEST and failpoint code inlined into the LIMIT BY per-row loop function is laid out before the loop, so its size sets the loop's placement. #125738 shrinks currentThreadHasGroup, the loop moves 0x60 bytes with identical instructions, and that placement is slower on the CI CPU (3/3 of its builds vs 0/26 others).
c Fix matches root cause? Yes. It removes the code whose size moved the loop. That code is dead: its only test was removed in #123165. Nothing in the perf test or its thresholds changes.
d Test intent preserved / new tests added? No test changed. The existing perf queries (limit_by #0, optimize_limit_by_keys #0/#1) are the speed oracle and the 71 limit_by stateless tests the correctness oracle. Behaviour is unchanged and a code-placement property cannot be asserted by a stateless test, so I added no test.
e Both directions demonstrated? Without the fix, #125738's change moves the loop (function md5 changes, offsets as in CI). With the fix, the function is byte-identical with and without it (md5 e98143c3 both).
f Fix is general across code paths? Both transforms: the LimitByTransform::consumeImpl per-row loop and transform, and the LimitBySortedStreamTransform::transform per-run loop. NegativeLimitByTransform has none of this code. One template body covers every key method.
g Fix generalizes across inputs (params/datatypes/wrappers)? The template covers all hash methods. Measured on UInt16, UInt8, LowCardinality(String), String and two-String keys, skewed keys, both cardinalities, and the sorted-stream path: 5-11% faster or unchanged within noise.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or SQL change. The six failpoint names are test-only (USE_LIBFIU), with no user left in tests/, ci/ or docs/.
i Invariants and contracts preserved? The cancel exits keep stopReading(); return; at the same four points. run_count only fed the deleted failpoint. No other state is touched. A query with max_execution_time = 2 still stops after 2.09 s.

Session id: cron:clickhouse-impl-slot-4:20261021-101000

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nihalzp @nickitat, could you review this and add the can be tested label so CI runs? It deletes the test-only failpoints and LOG_TEST lines left in the LimitByTransform loops after their only test was removed; their inlined code sits before the per-row loop and decides where it lands, which is why limit_by #0 got slower on an unrelated PR's amd builds.

@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0a42c28]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 5/6) FAIL Performance dashboard
json_to_string #1 FAIL dashboard
iceberg_suite_local_rw_features #9 unstable query history
limit_by_in_pk_order #1 slower query history
new_json_type #1 slower query history
number_formatting_formats #6 unstable query history
number_formatting_formats #7 unstable query history
number_formatting_formats #10 unstable query history
range_hashed_dictionary #6 unstable query history

AI Review

Summary

Removes six unused failpoints and four test-only cancellation log statements from the hash-based and sorted-stream LIMIT BY transforms. The cancellation checks and query-processing logic remain intact. No actionable findings in PR head 0a42c28b73be0a06df28a9c3ab9f0602398dabbc.

Missing context / blind spots
  • ⚠️ The PR supplies local release A/B measurements and reports unchanged results across 71 stateless tests; these runs were not independently reproduced here. The CI performance shard has no results yet, and the overall workflow remains pending. The completed Praktika report would establish the result on CI hardware.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Oct 21, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Closing: this change puts the LimitByTransform::consumeImpl loop for 8-byte keys into a layout that is about 2x slower on arm (limit_by_misc #3 0.61 -> 1.36 s, limit_by_in_pk_order #1 0.59 -> 0.98 s), so removing this code is not a layout-robust fix. Details: #125738 (comment)

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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants