-
Notifications
You must be signed in to change notification settings - Fork 1.7k
perf: improve performance of vectorized_equal_to
for PrimitiveGroupValueBuilder
in multi group by aggregation
#17977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rluvaton
wants to merge
12
commits into
apache:main
Choose a base branch
from
rluvaton:optimize-primitive-multi-group-by-to-use-simd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−26
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1eb2d6a
change to boolean buffer builder and make primitive simd
rluvaton e5b65bd
update bench to test primitive
rluvaton 01dd5c9
update bench
rluvaton cd99e29
remove boolean buffer builder
rluvaton 0c6295c
Merge branch 'main' into optimize-primitive-multi-group-by-to-use-simd
rluvaton 825b8c7
update bench
rluvaton f19d439
Merge branch 'main' into optimize-primitive-multi-group-by-to-use-simd
rluvaton f31b5c0
fix not setting correctly equal to results
rluvaton dd73823
Merge branch 'main' into optimize-primitive-multi-group-by-to-use-simd
rluvaton 5b2704f
format and port `as_chunks`
rluvaton 27d8026
avoid chunks at the moment
rluvaton d5b5caa
rename function and add comment
rluvaton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,85 @@ where | |
nulls: MaybeNullBufferBuilder::new(), | ||
} | ||
} | ||
|
||
fn vectorized_equal_to_non_nullable( | ||
&self, | ||
lhs_rows: &[usize], | ||
array: &ArrayRef, | ||
rhs_rows: &[usize], | ||
equal_to_results: &mut [bool], | ||
) { | ||
assert!( | ||
!NULLABLE || (array.null_count() == 0 && !self.nulls.might_have_nulls()), | ||
"called with nullable input" | ||
); | ||
let array_values = array.as_primitive::<T>().values(); | ||
|
||
let iter = izip!( | ||
lhs_rows.iter(), | ||
rhs_rows.iter(), | ||
equal_to_results.iter_mut(), | ||
); | ||
|
||
for (&lhs_row, &rhs_row, equal_to_result) in iter { | ||
let result = { | ||
// Getting unchecked not only for bound checks but because the bound checks are | ||
// what prevents auto-vectorization | ||
let left = if cfg!(debug_assertions) { | ||
self.group_values[lhs_row] | ||
} else { | ||
// SAFETY: indices are guaranteed to be in bounds | ||
unsafe { *self.group_values.get_unchecked(lhs_row) } | ||
}; | ||
let right = if cfg!(debug_assertions) { | ||
array_values[rhs_row] | ||
} else { | ||
// SAFETY: indices are guaranteed to be in bounds | ||
unsafe { *array_values.get_unchecked(rhs_row) } | ||
}; | ||
|
||
// Always evaluate, to allow for auto-vectorization | ||
left.is_eq(right) | ||
}; | ||
|
||
*equal_to_result = result && *equal_to_result; | ||
} | ||
} | ||
|
||
pub fn vectorized_equal_nullable( | ||
&self, | ||
lhs_rows: &[usize], | ||
array: &ArrayRef, | ||
rhs_rows: &[usize], | ||
equal_to_results: &mut [bool], | ||
) { | ||
assert!(NULLABLE, "called with non-nullable input"); | ||
let array = array.as_primitive::<T>(); | ||
|
||
let iter = izip!( | ||
lhs_rows.iter(), | ||
rhs_rows.iter(), | ||
equal_to_results.iter_mut(), | ||
); | ||
|
||
for (&lhs_row, &rhs_row, equal_to_result) in iter { | ||
// Has found not equal to in previous column, don't need to check | ||
if !*equal_to_result { | ||
continue; | ||
} | ||
|
||
// Perf: skip null check (by short circuit) if input is not nullable | ||
let exist_null = self.nulls.is_null(lhs_row); | ||
let input_null = array.is_null(rhs_row); | ||
if let Some(result) = nulls_equal_to(exist_null, input_null) { | ||
*equal_to_result = result; | ||
continue; | ||
} | ||
|
||
// Otherwise, we need to check their values | ||
*equal_to_result = self.group_values[lhs_row].is_eq(array.value(rhs_row)); | ||
} | ||
} | ||
Comment on lines
+112
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved the code from |
||
} | ||
|
||
impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn | ||
|
@@ -97,32 +176,15 @@ impl<T: ArrowPrimitiveType, const NULLABLE: bool> GroupColumn | |
rhs_rows: &[usize], | ||
equal_to_results: &mut [bool], | ||
) { | ||
let array = array.as_primitive::<T>(); | ||
|
||
let iter = izip!( | ||
lhs_rows.iter(), | ||
rhs_rows.iter(), | ||
equal_to_results.iter_mut(), | ||
); | ||
|
||
for (&lhs_row, &rhs_row, equal_to_result) in iter { | ||
// Has found not equal to in previous column, don't need to check | ||
if !*equal_to_result { | ||
continue; | ||
} | ||
|
||
// Perf: skip null check (by short circuit) if input is not nullable | ||
if NULLABLE { | ||
let exist_null = self.nulls.is_null(lhs_row); | ||
let input_null = array.is_null(rhs_row); | ||
if let Some(result) = nulls_equal_to(exist_null, input_null) { | ||
*equal_to_result = result; | ||
continue; | ||
} | ||
// Otherwise, we need to check their values | ||
} | ||
|
||
*equal_to_result = self.group_values[lhs_row].is_eq(array.value(rhs_row)); | ||
if !NULLABLE || (array.null_count() == 0 && !self.nulls.might_have_nulls()) { | ||
self.vectorized_equal_to_non_nullable( | ||
lhs_rows, | ||
array, | ||
rhs_rows, | ||
equal_to_results, | ||
); | ||
} else { | ||
self.vectorized_equal_nullable(lhs_rows, array, rhs_rows, equal_to_results); | ||
} | ||
} | ||
|
||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As
lhs_row
is not checked here te be in bounds, this method would need to be marked unsafe as well.