From 2de99b592225fde83de0a5f469df037c60fe568a Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Tue, 11 Nov 2025 09:56:29 +0100 Subject: [PATCH] Specialize pyarray_offset to be more amenable to constprop This improves performance in tight loops. --- src/Wrap/PyArray.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Wrap/PyArray.jl b/src/Wrap/PyArray.jl index 7b1dc8d3..4e0bdc56 100644 --- a/src/Wrap/PyArray.jl +++ b/src/Wrap/PyArray.jl @@ -669,6 +669,10 @@ end x end +# Specialization for the common case where T == R, we can use the +# constant-propagatable sizeof instead of looking up the stride. +pyarray_offset(x::PyArray{T,N,M,true,T}, i::Int) where {T,N,M} = + N == 0 ? 0 : (i - 1) * sizeof(T) pyarray_offset(x::PyArray{T,N,M,true}, i::Int) where {T,N,M} = N == 0 ? 0 : (i - 1) * x.strides[1] pyarray_offset(x::PyArray{T,1,M,true}, i::Int) where {T,M} = (i - 1) .* x.strides[1]