Skip to content

Commit

Permalink
Improve performance of custom-iterator __getitem__ (Qiskit#1096) (Q…
Browse files Browse the repository at this point in the history
…iskit#1100)

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.

(cherry picked from commit d4f28e3)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
  • Loading branch information
mergify[bot] and jakelishman authored Feb 21, 2024
1 parent 8984d9a commit d1f0a1c
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 d1f0a1c

Please sign in to comment.