Skip to content

Shrink compiled cast kernels and speed up validated casts - #11676

Merged
robert3005 merged 3 commits into
developfrom
rk/cast-kernels-bench
Nov 20, 2026
Merged

robert3005 merged 3 commits into
developfrom
rk/cast-kernels-bench

Conversation

@robert3005

@robert3005 robert3005 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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 0 before), out-of-range casts always report values 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.

Signed-off-by: Robert Kruszewski <github@robertk.io>
@robert3005 robert3005 added changelog/chore A trivial change quick-review Small, self-contained change that is quick to review labels Nov 19, 2026
## 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>
@codspeed

codspeed Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 80.43%

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚡ 8 improved benchmarks
✅ 1974 untouched benchmarks
🆕 3 new benchmarks
⏩ 57 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation cast_i32_to_u32[8192] 403.4 µs 76.7 µs ×5.3
⚡ CPU Simulation copy_nullable[16384] 422.7 µs 176.9 µs ×2.4
⚡ CPU Simulation copy_non_nullable[16384] 286.1 µs 162.6 µs +75.93%
⚡ CPU Simulation cast_i32_to_decimal9_scale2[8192] 191.5 µs 109.8 µs +74.45%
⚡ CPU Simulation in_place_non_nullable[16384] 39 µs 28.2 µs +38.13%
⚡ CPU Simulation cast_i64_to_decimal38_scale2[8192] 511.3 µs 388.3 µs +31.66%
⚡ CPU Simulation ArrowExportVTable_execute_arrow 162.8 µs 128 µs +27.2%
⚡ CPU Simulation cast_i32_to_decimal9_scale0[8192] 85.6 µs 68 µs +25.94%
🆕 CPU Simulation narrow_i64_to_i32_nullable[16384] N/A 290.6 µs N/A
🆕 CPU Simulation to_i32_nullable[8192] N/A 313.2 µs N/A
🆕 CPU Simulation cast_f32_to_u16[8192] N/A 130.2 µs N/A

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing rk/cast-kernels-bench (ebfad2c) with develop (c84406e)

Open in CodSpeed

Footnotes

  1. 57 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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>
@robert3005 robert3005 changed the title Add benchmarks for checked decimal and float-to-integer casts Shrink compiled cast kernels and speed up validated casts Nov 19, 2026
@robert3005

Copy link
Copy Markdown
Contributor Author

I didn't notice the automerge when I was restacking it. I have addressed your comments @connortsui20

@robert3005
robert3005 enabled auto-merge (squash) November 20, 2026 16:40
@robert3005
robert3005 merged commit 52111e5 into develop Nov 20, 2026
115 of 117 checks passed
@robert3005
robert3005 deleted the rk/cast-kernels-bench branch November 20, 2026 23:09
lwwmanning added a commit that referenced this pull request Nov 21, 2026
#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/chore A trivial change quick-review Small, self-contained change that is quick to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants