Repository navigation
PRF: Faster encoders thanks to F-order layout - #34392
Merged
Merged
Conversation
Member
|
Please add a changelog entry for this PR. |
cakedev0
marked this pull request as ready for review
July 1, 2026 14:25
ogrisel
approved these changes
Jul 1, 2026
ogrisel
left a comment
Member
There was a problem hiding this comment.
I ran the benchmarks and can reproduce similar speed-ups on Apple M4 under macOS.
The code change does not add too much extra complexity and the speed-up is significant enough to justify this change.
OmarManzoor
approved these changes
Jul 2, 2026
OmarManzoor
left a comment
Contributor
There was a problem hiding this comment.
LGTM. Thank you @cakedev0
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.
Reference Issues/PRs
Inspired by the flamegraph of this script which then led me to profile encoders and found out that for integers input, some of the bottlenecks were in simple numpy operations that should be very fast, and then I realized it was doing col-wise things on C-order arrays.
I used benchmark from this PR #34394 for measurements below.
What does this implement/fix? Explain your changes.
This optimizes memory layout used by encoders (
TargetEncoder,OneHotEncoder,OrdinalEncoder) by using F-order output/intermediate arrays instead of default C-order.AI usage disclosure
I used AI assistance for: Adapting the Cython code to use
X_indices. And that's all.Benchmarks
I ran
python benchmarks/bench_encoding.py -f integer(added in PR #34394) on main and this branch.TLDR:
Note: when inside a
ColumnTransformer(..., remainder="passthrough"), speed-up stays the same expect for one-hot where the cost of sparse h-stack is dominant and makes the speed-up less visible.Main:
This branch:
Note: the speed-up for
ordinal/onehot.fit_transformis much more important when I compare locally the new fast path for pandas/polars categorical with or without the optimization made in this PR. In this case, it's around 2x.