You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
random number generators perform unnecessary copy #1351
The result of ascontinguousarray can share memory with the input array yet the if array_1d.data != array.data: check can fail resulting in an unnecessary copy.
The issue can be illustrated in the following code:
importnumpyasnparray=np.empty((10, 20, 30), dtype=float)
array_1d=np.ascontiguousarray(array.ravel(),dtype=float)
# data is not the sameassertarray.data!=array_1d.data# because array_1d is a view of arrayassertarray.data==array_1d.base.data# note the use of base here# arrays share memoryassertnp.shares_memory(array, array_1d)
# double checkarray_1d[:] =2assertnp.all(array==2)
Replacing if array_1d.data != array.data: with if not np.shares_memory(array, array_1d): should fix the issue.
Activity
rmjarvis commentedon Mar 6, 2026
Sure. I'm not sure it's an "issue" exactly. But I'll admit the change would be marginally more efficient. Feel free to submit a PR.