-
Notifications
You must be signed in to change notification settings - Fork 5
Description
For the work @pwulles and I do with mdarray, we need a reshape operation that never fails. This means that in the cases where mdarray’s reshape panics, the array will have to be copied.
We’re thinking of implementing a function that outputs something like
pub enum CowArray<'a, T, S: md::Shape = md::DynRank> {
Dense(md::View<'a, T, S, md::Dense>),
Strided(md::View<'a, T, S, md::Strided>),
Owned(md::Tensor<T, S>),
}The above type would have a method into_owned. It could also have a method (as_strided?) that returns a strided view. These method would cover common use cases, but for best performance users could also directly match the three variants.
Do you have any comments on the above idea? Do you think that (eventually) something along these lines could be added to mdarray itself? Unless any users rely on catching the panics of the current reshape, I believe that it would be a pure improvement, without any performance cost.
There’s also the question what to do with reshape_mut. It could perhaps return a different enum with the variants Dense, Strided, and Incompatible. A ViewMut could be recovered for the first two, but the type would also have an assign method that works for any variant.