Perf: Vectorize check_bounds(2x speedup)#8966
Merged
Dandandan merged 1 commit intoapache:mainfrom Dec 10, 2025
Merged
Conversation
Contributor
Author
gstvg
commented
Dec 8, 2025
| "Array index out of bounds, cannot get item at index {ix} from {len} entries" | ||
| ))); | ||
| let in_bounds = indices.values().iter().fold(true, |in_bounds, &i| { | ||
| in_bounds & (i >= T::Native::ZERO) & (i < len) |
Contributor
Author
There was a problem hiding this comment.
Even if today check_bounds is only called with unsigned indices, ArrowPrimitiveType includes signed types, which can be used here in the future, so we have to check this too. Happily, LLVM will optimize this out. If this function get's restricted to a hypothetical ArrowUnsignedInteger or ArrowIndexType, this can be removed
gstvg
commented
Dec 8, 2025
| "Array index out of bounds, cannot get item at index {index} from {len} entries" | ||
| ))); | ||
| } | ||
| } |
Contributor
Author
There was a problem hiding this comment.
This heavily optimizes for the happy path, and make the error-path slow. If a more balanced approach is desired we can use something like this:
pub fn check_bounds<T: ArrowPrimitiveType>(
len: usize,
indices: &PrimitiveArray<T>,
) -> Result<(), ArrowError>
where
T::Native: Display,
{
// omitted
if indices.null_count() > 0 {
// omitted
} else {
let chunks = indices.values().chunks_exact(64);
let remainder = chunks.remainder();
for chunk in chunks {
let chunk: &[T::Native; 64] = chunk.try_into().unwrap(); // unwrap is optimized out
let in_bounds = chunk.iter().fold(true, |in_bounds, &i| {
in_bounds & (i >= T::Native::ZERO) & (i < len)
});
if !in_bounds {
return Err(out_of_bounds_index(chunk, len));
}
}
for &index in remainder {
if index < T::Native::ZERO || index >= len {
return Err(ArrowError::ComputeError(format!(
"Array index out of bounds, cannot get item at index {index} from {len} entries"
)));
}
}
Ok(())
}
}
#[inline(never)]
fn out_of_bounds_index<T: ArrowNativeType>(indices: &[T], len: T) -> ArrowError {
todo!()
}
Dandandan
approved these changes
Dec 8, 2025
Contributor
|
Thanks @gstvg |
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?
Part of #279, related to #8879
Rationale for this change
What changes are included in this PR?
Instead of checking index by index, and error-branching individually on them, check all indices for the presence of any out-of-bounds index, and only then, in the slow-path, find the exact out-of-bounds index
Note how LLVM vectorizes the happy path into a very tight loop:

https://rust.godbolt.org/z/MhY4cP6WG
Are these changes tested?
Existing tests already cover the changed function
Are there any user-facing changes?
No