Repository navigation
Inline divide into the signed wide-integer division operators - #127761
Algunenano merged 2 commits into
Conversation
The signed 256-bit `/` and `%` operators (`operator_slash` / `operator_percent` of `wide::integer<256, signed>`) are out of line in release builds (the arm CI binary of ClickHouse#112928 has 812 and 61 call sites), and `divide` was a second out-of-line call below them. So even a division whose operands fit in one limb, which `divide` does with one native division, passed its three 32-byte operands (numerator by reference, denominator by value, quotient through the result pointer) through the stack, at 8 mod 16 in the operator's frame. On arm the speed of `bigint_arithm` query ClickHouse#7 (`intDiv(toInt256(number + 1), number + 1)`) followed the stack placement: the same machine code ran at 0.15 s or at 0.18-0.20 s depending on the binary (84 of 1580 arm binaries in 30 days were always slow; master d5bdd58 and 6a36363 differ only in the size of PT_TLS, by 64 bytes; on ClickHouse#112928 the ThreadPool worker frame is 32 bytes larger), and the whole difference was self time of `divide<256>` (978 -> 3069 samples). Now the call to `divide` in the signed operators carries `[[clang::always_inline]]`, so `divide`'s body, narrow branches included, is part of the operator: a narrow division stays in registers, and the general path calls `divide_knuth` directly. The unsigned operators and `divide` itself are unchanged: the unsigned operators are inlined into their callers and keep their plain call to the out-of-line `divide`, because carrying the division makes them too large to inline (measured +45% for a 1-limb by 2-limb `UInt256` division). Measured on x86 (microbenchmark, 2^24 divisions per kernel, median of 3 runs of 11 interleaved rounds, ratio to master): `Int256` narrow `/` 0.500, narrow `%` 0.501, 4-limb by 1-limb 0.281, 4-limb by 2-limb 0.637, 1-limb by multi-limb `%` 0.541; the `UInt256`, `UInt512`, `Int128` and `UInt128` kernels 0.990-1.005, and the unsigned `divide<256>` / `divide<512>` and the 128-bit kernels are machine-code identical to master. aarch64 objects (`intDiv.cpp.o`, `modulo.cpp.o`): the narrow paths of the signed operators touch no stack slot beyond the prologue/epilogue pairs and the stack guard, the call sites of the operators are unchanged, `.text` grows by 696 and 740 bytes. Related: ClickHouse#112928 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261025-004200 |
|
cc @thevar1able @Algunenano, could you review this and add the |
|
Workflow [PR], commit [e5c2767] Summary: ✅
AI ReviewSummaryPR #127761 forces Final Verdict✅ Approve. No new inline findings to post. |
CI finish ledger - d0023b1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261025-133007 |
|
Fixing PR for test_scheduler_cpu_parking::test_parking_fires_on_idle_worker created: #127780 |
The test needs a serial sleepEachRow(0.5) phase in which the other pipeline workers have no task and park their CPU lease in the idle wait. The outer aggregate was count(), which reads no column, so the analyzer's RemoveUnusedProjectionColumnsPass removed both sleepEachRow projections and the query read 90 numbers in a few milliseconds. The test then passed only when a second worker happened to reach the idle wait before the query ended, and failed with 0 parks otherwise (Integration tests (arm_binary, 3/4) on PR ClickHouse#127761). CIDB: 5391 OK runs in 30 days with a median test time of 437 ms, so the sleeps never ran in CI. Read the sleep column with sum(s) so the sleeps are kept (about 7 s, the idle window the comment describes), and assert SleepFunctionCalls == 90 before the parks asserts so the test fails if the premise is ever optimized away again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units4283 translation units recompiled, 20064 s compile time in total, 4281 of them have a recent master baseline. Translation units without a recent master baseline:
|
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Integer division of signed 256-bit integers (for example
intDivonInt256values) is faster when both operands fit in 64 bits.Description
Related: #112928 (comment)
bigint_arithm #7(intDiv(toInt256(number + 1), number + 1)) runs at one of several per-binary speeds onarm_release: 0.15 s or 0.18-0.20 s with the same machine code (84 of 1580 arm binaries in 30 days were always slow, master itself twice with unrelated commits), which makes the performance check red on unrelated PRs (example).The signed 256-bit
operator_slash/operator_percentare out of line, anddividewas a second out-of-line call below them. So even a division whose operands fit in one limb passed its three 32-byte operands through the stack, and its speed followed the stack placement: in the pairs I measured, a slow binary differs from its fast reference only in the TLS size or in one caller's frame size, and the whole gap is self time ofdivide<256>.The call to
dividein the signed operators now carries[[clang::always_inline]], so a narrow signed division stays in registers and the general path callsdivide_knuthdirectly.divideitself, the unsigned operators (inlined into their callers, and too large for that if they carried the division) and 128-bit division are unchanged, with machine code identical to master on x86. SQL%onInt256divides unsigned magnitudes (ModuloImpl), so it keeps master's code too.Validation: x86 microbenchmark, ratio to master:
Int256narrow/0.50, narrow%0.50, 4-limb by 1-limb 0.28, 4-limb by 2-limb 0.64; unsigned 256/512-bit and 128-bit kernels 0.99-1.005. In aarch64 objects the narrow paths of the signed operators touch no stack slot beyond the prologue and epilogue. The arm effect onbigint_arithm #7can only be shown by this PR's own performance check. No test change: every result still comes from master'sdividebody, which the existing exact-oracle sweeps ingtest_wide_integer.cppcover;bigint_arithmquery #7 is the performance test.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127761]
Version info
26.11.1.1376-master(included in26.11and later)