Repository navigation
Faster conversion of Float32 and Float64 to 128- and 256-bit integers and decimals - #129274
Conversation
Since ClickHouse#126191, `wide_integer_from_builtin(self, float)` falls back to the `double` overload for |x| >= 2^63. That overload passes `self` by reference to `set_multiplier`, which is recursive and never inlined, so wherever it is inlined into a batch loop (`convertToDecimalBatch`, `ConvertImpl`) the result of every row goes through a stack temporary: zeroed, written with two 8-byte stores and read back with a 16-byte load. On arm that made `decimal_casts` query ClickHouse#7 (Float32 -> Decimal128) about 8% slower on master, and 13-20% slower in builds where that temporary lands at 24 mod 64 (16 of 16 such CI builds). The `double` overload now checks the int64 range in `double` and converts the in-range value directly; the rest goes to a `NO_INLINE` helper that returns the integer by value, so the caller's result stays in registers. The range check accepts exactly the same set of `double` values as the old `long double` one (no `double` lies in [2^63 - 1, 2^63)), and on aarch64 it no longer calls `__extenddftf2` + `__lttf2` per row. `float` and `BFloat16` now go through `double`, which converts them like the same value as `Float64` by construction. Results are unchanged: all 2^32 Float32 patterns, all BFloat16 patterns and ~4.7e9 Float64 inputs (6.5e8 on aarch64) convert to the same Int128/UInt128/Int256/UInt256 bytes as before, on x86-64 and aarch64. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Say that the in-range cast truncates rather than converts exactly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings, both nits) plus my own review (3 nits, two of them the same).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261104-132200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261104-104600 |
|
Workflow [PR], commit [44a8f4c] Summary: ✅
AI ReviewSummaryPR #129274, head Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings to post. |
|
cc @thevar1able, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units4281 translation units recompiled, 20910 s compile time in total, 4279 of them have a recent master baseline. Translation units without a recent master baseline:
|
3f94ba5
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster conversion of
Float64values toInt128,UInt128,Int256,UInt256,Decimal128andDecimal256. For example,toDecimal128(x, 6)of aFloat64column is about 4x faster on x86-64.Description
Asked on #119542:
decimal_castsquery #7 (toFloat32(x)toDecimal32/64/128) was 19.9% slower in Performance Comparison (arm_release, master_head, 2/6). That PR is not the cause. Master itself got about 8% slower on ARM at #126191, and some builds 13-20%.Since #126191, a
floatof magnitude>= 2^63goes throughwide_integer_from_builtin(self, double), which passesselfby reference toset_multiplier, a recursive function that is never inlined. When this is inlined into a column loop (convertToDecimalBatch,ConvertImpl), every row's result goes through a stack temporary: it is zeroed, written with two 8-byte stores and reloaded. Thedoubleoverload already did this forFloat64, and on aarch64 itslong doublerange check also called__extenddftf2and__lttf2for every row.Now the
doubleoverload checks theInt64range indoubleand converts in-range values directly. This accepts the same values as the oldlong doublecheck, because nodoublelies in[2^63 - 1, 2^63). Other values go to aNO_INLINEhelper that holds the old code and returns the integer by value.Float32andBFloat16go through thedoubleoverload.Results are byte-identical to master for all 2^32
Float32and 2^16BFloat16bit patterns and about 4.7e9Float64inputs, checked on x86-64 and on aarch64 under qemu. In the codegen of both architectures, the in-range path stores the result straight from registers. On x86-64 (8 threads, 200M rows),toDecimal128(toFloat64(x), 6)goes from 0.33 to 0.07 s,toInt128(toFloat32(x))from 0.31 to 0.07 s, and query #8 from 0.37 to 0.20 s. Values that are>= 2^63after the decimal scale is applied (for exampleDecimal(38, 18)from 9.23 up) are 5-8% slower on x86-64, with the same instruction count.Workflow [PR]
Sync PR [sync-upstream/pr/129274]
Version info
26.11.1.2194-master(included in26.11and later)