rust-numpy icon indicating copy to clipboard operation
rust-numpy copied to clipboard

Apply numpy ufunc to `&mut Vec` in rust heap

Open ritchie46 opened this issue 3 years ago • 3 comments

Would it be possible to accept any numpy ufunc as &PyAny and apply that to a mutable vec allocated in Rust?

ritchie46 avatar Nov 15 '21 07:11 ritchie46

I guess you are asking specifically if this can be done without copying the values into a NumPy array and then back into the Vec? Is the Vec assumed to represent one-dimensional data?

adamreichold avatar Nov 15 '21 10:11 adamreichold

I guess you are asking specifically if this can be done without copying the values into a NumPy array and then back into the Vec?

Indeed. :)

Yes all 1D contiguous data.

ritchie46 avatar Nov 15 '21 10:11 ritchie46

I thought about this and I am sceptical that it is possible with a safe API. While we could mem::take that &mut Vec and turn the Vec into a &PyArray1 without copying, but it will be difficult to recover ownership of the Vec. We do stash it in the arrays base object which we could inspect and downcast. But the array itself almost surely has an elevated reference count (just passing it to a function elevates it to two) so that we cannot ensure that there are no more aliasing references to the array we are trying to destructure even if the base object (an instance of our PySliceContainer) has a reference count of one.

adamreichold avatar Jan 24 '22 18:01 adamreichold