Skip to content

Commit

Permalink
Improve performance of custom-iterator __getitem__ (#1096)
Browse files Browse the repository at this point in the history
Flipping the order of `Slice` and `Int` in `SliceOrInt` so that `Int`
comes first means that the `FromPyObject` derivation will then try `Int`
first, which is the correct variant in like 99.9% of uses of the struct.
This has the impact of improving int `__getitem__` times in the custom
iterators by about 3x (from 205ns to 61ns on my machine), which has
knock-on effects for the implicit iterators Python defines for these
classes.
  • Loading branch information
jakelishman committed Feb 20, 2024
1 parent f5b34bb commit d4f28e3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ trait PyGCProtocol {

#[derive(FromPyObject)]
enum SliceOrInt<'a> {
Slice(&'a PySlice),
Int(isize),
Slice(&'a PySlice),
}

trait PyConvertToPyArray {
Expand Down

0 comments on commit d4f28e3

Please sign in to comment.