Skip to content

Fix to_shared to avoid cloning when it is already ArcArray #893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ pub unsafe trait Data: RawData {
where
Self::Elem: Clone,
D: Dimension;

/// Return a shared ownership (copy on write) array based on the existing one,
/// cloning elements if necessary.
#[doc(hidden)]
fn to_shared<D>(self_: &ArrayBase<Self, D>) -> ArrayBase<OwnedArcRepr<Self::Elem>, D>
where
Self::Elem: Clone,
D: Dimension,
{
// clone to shared
self_.to_owned().into_shared()
}
}

/// Array representation trait.
Expand Down Expand Up @@ -238,6 +250,15 @@ unsafe impl<A> Data for OwnedArcRepr<A> {
strides: self_.strides,
}
}

fn to_shared<D>(self_: &ArrayBase<Self, D>) -> ArrayBase<OwnedArcRepr<Self::Elem>, D>
where
Self::Elem: Clone,
D: Dimension,
{
// to shared using clone of OwnedArcRepr without clone of raw data.
self_.clone()
}
}

unsafe impl<A> DataMut for OwnedArcRepr<A> where A: Clone {}
Expand Down
6 changes: 3 additions & 3 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ where
}
}

/// Return a shared ownership (copy on write) array.
/// Return a shared ownership (copy on write) array, cloning the array
/// elements if necessary.
pub fn to_shared(&self) -> ArcArray<A, D>
where
A: Clone,
S: Data,
{
// FIXME: Avoid copying if it’s already an ArcArray.
self.to_owned().into_shared()
S::to_shared(self)
}

/// Turn the array into a uniquely owned array, cloning the array elements
Expand Down