perf: in-place fast path for ScalarValue::sub / sub_checked - #23203
Closed
Dandandan wants to merge 1 commit into
Closed
perf: in-place fast path for ScalarValue::sub / sub_checked#23203Dandandan wants to merge 1 commit into
ScalarValue::sub / sub_checked#23203Dandandan wants to merge 1 commit into
Conversation
`ScalarValue::add`/`add_checked` already short-circuit the common
primitive/decimal cases through an in-place `try_add_*_in_place` path,
avoiding the `to_scalar() -> arrow kernel -> try_from_array` round-trip
(three allocations + a kernel dispatch per call). `sub`/`sub_checked`
did not, so RANGE-mode window frame boundary computation
(`WindowFrameStateRange::calculate_index_of_row`), which calls
`ScalarValue::sub`/`sub_checked` once per row for PRECEDING-on-ascending
/ FOLLOWING-on-descending offsets, paid that cost on every row.
This mirrors the existing add fast path for sub: `sub_optional`,
`sub_decimal_values`, and `try_sub_{wrapping,checked}_in_place`, gated by
the (renamed) `can_use_direct_arithmetic` whitelist, with the kernel path
retained as the fallback. The decimal path stays always-checked
(`mul_checked` + `sub_checked`) to match the Arrow decimal kernels
exactly.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
ScalarValue::add/add_checkedalready short-circuit the common primitive and decimal cases through an in-placetry_add_*_in_placepath, avoiding theto_scalar()→ Arrow kernel →try_from_arrayround-trip (≈3 heap allocations + a kernel dispatch per call).sub/sub_checkeddid not — they always took the slow kernel path.This matters for window functions:
WindowFrameStateRange::calculate_index_of_rowcallsScalarValue::sub/sub_checkedonce per row to compute RANGE frame boundaries for PRECEDING-on-ascending / FOLLOWING-on-descending offsets, so every row in such a partition paid the allocation + dispatch cost. This bringssubto parity with the already-optimizedadd.What changes are included in this PR?
sub_optional,sub_decimal_values, andtry_sub_{wrapping,checked}_in_place, mirroring the existingaddhelpers.sub/sub_checkedthrough the in-place fast path, gated by the whitelist (renamedcan_use_direct_add→can_use_direct_arithmeticsince it now serves both ops), with the Arrow kernel path retained as the fallback for non-whitelisted types.mul_checked+sub_checked) to match the Arrow decimal kernels exactly, regardless of the wrapping vs. checked variant.No public API or behavior change —
sub/sub_checkedproduce identical results (and identical overflow errors) to the kernel path.Are these changes tested?
Yes. Existing
subtests (including the exact overflow error messages) continue to pass — the fast path uses the sameArrowNativeTypeOp::sub_checkedthe Arrow kernel calls internally, so messages are unchanged. Added parity tests covering:Are there any user-facing changes?
No.