Skip to content

Conversation

@JamesWrigley
Copy link
Contributor

This improves performance in tight loops, for me it pretty much fixes the performance hit compared to Array (#182 (comment)):

x_py = PyArray(np.random.rand(1000, 1000).T);
x = Array(x_py);

@benchmark sum($x)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min  max):  122.804 μs  440.813 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     124.024 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   125.858 μs ±   6.590 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▂▆██▆▃▁        ▁     ▁▁▁▁▂▁▁▂▁▁                               ▂
  ██████████▇▇▆▆▇██▇▇█████████████████▇▇▇▆▆▇▆▆▆▆▄▅▅▆▅▄▄▅▄▅▄▅▂▅▅ █
  123 μs        Histogram: log(frequency) by time        146 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

@benchmark sum($x_py)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (min  max):  128.664 μs  447.404 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     129.694 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   131.126 μs ±   4.813 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

   ▁▆██▆▂▁                            ▁▁▁▁▁▁▁▁▁                 ▂
  ▃████████▇▆▆▅▅▆▆▅▅▅▆▄▅▇█▇▅▃▄▄▆▆▇▇█████████████████▇▇█▇▇▇▇▆▆▆▆ █
  129 μs        Histogram: log(frequency) by time        143 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

This improves performance in tight loops.
@cjdoris
Copy link
Member

cjdoris commented Nov 11, 2025

Unfortunately this implementation is incorrect because PyArrays can be strided, even when linearly indexable.

Even more unfortunately, there is no room to encode the fact that the array might be contiguous in the type parameters, which means this cannot be fixed without a braking change.

Fortunately, the parameterisation has been changed in the v1 branch (

) and there is already a :contiguous flag implemented which does just this, so fingers crossed the performance is as you'd hope. Please give it a try (pkg> add PythonCall#v1) and let me know!

@JamesWrigley
Copy link
Contributor Author

Ah, interesting. Indeed performance seems fixed on v1 :)

@JamesWrigley JamesWrigley deleted the pyarray-offset branch November 11, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants