From d4f28e35f6ee5f76a26e16e0ffc357bb72bb89da Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Tue, 20 Feb 2024 19:19:47 +0000 Subject: [PATCH] Improve performance of custom-iterator `__getitem__` (#1096) 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. --- 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 {