Skip to content

RFC: reshape that never fails #19

@grothesque

Description

@grothesque

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions