-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
perf: Add fast paths for series.arg_sort and dataframe.sort #19872
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
edb956d
First commit
d0b059c
commit
40e07fa
Add tests
f259378
commit
e59e9e4
Fix clippy error
0c97c53
limit
0484596
limit
61ec922
changes
siddharth-vi ee8c2a4
rebase changes
siddharth-vi f5191ce
Remove print
siddharth-vi e99cbda
rebase fix + fmt
siddharth-vi c38636c
remove double alloc
ritchie46 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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -1999,6 +1999,7 @@ impl DataFrame { | |
Ok(self.clone()) | ||
}; | ||
} | ||
|
||
// note that the by_column argument also contains evaluated expression from | ||
// polars-lazy that may not even be present in this dataframe. therefore | ||
// when we try to set the first columns as sorted, we ignore the error as | ||
|
@@ -2035,6 +2036,39 @@ impl DataFrame { | |
return self.bottom_k_impl(k, by_column, sort_options); | ||
} | ||
} | ||
// Check if the required column is already sorted; if so we can exit early | ||
// We can do so when there is only one column to sort by, for multiple columns | ||
// it will be complicated to do so | ||
#[cfg(feature = "dtype-categorical")] | ||
let is_not_categorical_enum = | ||
!(matches!(by_column[0].dtype(), DataType::Categorical(_, _)) | ||
|| matches!(by_column[0].dtype(), DataType::Enum(_, _))); | ||
|
||
#[cfg(not(feature = "dtype-categorical"))] | ||
#[allow(non_upper_case_globals)] | ||
const is_not_categorical_enum: bool = true; | ||
|
||
if by_column.len() == 1 && is_not_categorical_enum { | ||
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. Disabled fast path for categorical columns due to #19900 |
||
let required_sorting = if sort_options.descending[0] { | ||
IsSorted::Descending | ||
} else { | ||
IsSorted::Ascending | ||
}; | ||
// If null count is 0 then nulls_last doesnt matter | ||
// Safe to get value at last position since the dataframe is not empty (taken care above) | ||
let no_sorting_required = (by_column[0].is_sorted_flag() == required_sorting) | ||
&& ((by_column[0].null_count() == 0) | ||
|| by_column[0].get(by_column[0].len() - 1).unwrap().is_null() | ||
== sort_options.nulls_last[0]); | ||
|
||
if no_sorting_required { | ||
return if let Some((offset, len)) = slice { | ||
Ok(self.slice(offset, len)) | ||
} else { | ||
Ok(self.clone()) | ||
}; | ||
} | ||
} | ||
|
||
#[cfg(feature = "dtype-struct")] | ||
let has_struct = by_column | ||
|
Oops, something went wrong.
Oops, something went wrong.
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.
No blocker for this PR, but I rather see this in a generic function. Could be a follow up.