Repository navigation
Shrink compiled cast kernels and speed up validated casts - #11676
Conversation
Signed-off-by: Robert Kruszewski <github@robertk.io>
## Summary Primitive and decimal casts compiled a fused, checked loop for every (source, target) type pair: about 1.7 MB of code, including 121 instantiations of the primitive→primitive cast. This PR splits each cast into validation and conversion. The cast code shrinks to about 267 KB, and most validated casts get faster. ## Changes - **Validation** is a range check compiled once per source type. Only integer targets can reject a value. Rescaling casts translate the target range back into the source type. - **Conversion** then cannot fail. Integer resizing depends only on the widths, so primitive int→int, decimal and `upcast_decimal_values` share the same loops. Casts involving floats stay direct, because an integer intermediate measured 50–60% slower. - The shared code is the new `IntegerCast` in `primitive/compute/integer_cast.rs`. - Existing fast paths are kept. The old in-place int↔float conversion is dropped: the kernel never owns the buffer, so it never ran. | Cast (median µs) | Before | After | |---|---|---| | i32 → u32, validated | 28.1 | 2.0 | | u32 → u8, validated | 12.4 | 3.7 | | i64 → decimal(38,2) | 35.1 | 13.6 | | decimal(18,2) → i32 | 1592 | 45 | | decimal scale down (÷100) | 1560 | 49 | Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
Merging this PR will improve performance by 80.43%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
A same-width integer cast returned a clone of the source buffer, which carried the source's recorded alignment. A buffer whose pointer is aligned but which only records byte alignment then failed the decimal constructor's layout check, e.g. casting u32 to Decimal(9, 0). Record the target type's alignment on the reused buffer, which stays zero-copy whenever the pointer is aligned. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018SWbEuaJSq7kXEfEUZcoiy Signed-off-by: Robert Kruszewski <github@robertk.io>
|
I didn't notice the automerge when I was restacking it. I have addressed your comments @connortsui20 |
#11676 changed the integer cast's overflow error from "value exceeds target range" to "values exceed target range". The device batch test looked for the old wording, so CUDA tests fail on develop's code. Match the words both share. Signed-off-by: Will Manning <will@willmanning.io>
Primitive and decimal casts compiled a fused, checked loop for every (source, target) type pair, about 1.7 MB of code. Each cast is now a range check compiled once per source type plus a conversion that cannot fail; start the review with
primitive/compute/integer_cast.rs. Float casts keep one loop per pair, because an integer intermediate was 50–60% slower.The cast code shrinks to about 267 KB, and u32→u8 validated casts go from 12.4 µs to 3.7 µs for 8K values.
Behaviour change: float-to-integer casts reject NaN even with cached min/max stats (they cast to
0before), out-of-range casts always reportvalues exceed target range, and all-null input to a converting cast returns a constant null array.This PR started as the benchmark baseline for #11512, which was merged into this branch, so it now carries the whole change together with the new decimal and float-to-integer cast benchmarks. Review comments from #11512 are addressed here.