From d1f0a1ce9c0fb6ed70f6e1b0964e3bdfc8c15b04 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 21 Feb 2024 07:05:51 -0500 Subject: [PATCH] Improve performance of custom-iterator `__getitem__` (#1096) (#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 d4f28e35f6ee5f76a26e16e0ffc357bb72bb89da) Co-authored-by: Jake Lishman --- src/iterators.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/iterators.rs b/src/iterators.rs index 86e894ff5..7217c9b63 100644 --- a/src/iterators.rs +++ b/src/iterators.rs @@ -411,8 +411,8 @@ trait PyGCProtocol { #[derive(FromPyObject)] enum SliceOrInt<'a> { - Slice(&'a PySlice), Int(isize), + Slice(&'a PySlice), } trait PyConvertToPyArray {