NNPDF / pineappl

PineAPPL is not an extension of APPLgrid
https://nnpdf.github.io/pineappl/
GNU General Public License v3.0
12 stars 3 forks source link

Avoid copies in Python interface #196

Closed cschwan closed 1 year ago

cschwan commented 1 year ago

This PR changes the Rust interface to use ArrayView5 instead of Array5, which has the advantage of avoiding a full copy of the operator in PyGrid::evolve.

alecandido commented 1 year ago

I agree it would be desirable, and, for FkTable in general, you want only to read them. But are you sure it is supported by rust-numpy?

alecandido commented 1 year ago

I.e.: are you sure that rust-numpy won't generate an actual Python object with its own memory anyhow?

cschwan commented 1 year ago

I'm certain that in PyGrid::evolve the method calls to into_owned() and to_vec() make copies, and we can easily avoid them.

alecandido commented 1 year ago

Oh yes, I was reading right now the code, before I stopped to the comment. Essentially, it is the "opposite" problem of what I thought: you really have Python arrays, and don't want the copy to be done in Rust.

It might be that is also the third copy, if a copy is already done when passing the object from Python down to Rust, I'm not sure of this, but for sure you can avoid at least this (and I would expect to be the only one, but we can always profile it).

However, I started reading rust-numpy docs in the meanwhile: https://docs.rs/numpy/latest/numpy/borrow/index.html and we are already using PyReadonlyArray, so this supports my hypothesis that there is no further copy.