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

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

merged 3 commits into from
Jan 15, 2021

Conversation

SparrowLii
Copy link
Contributor

From FIXME in to_shared. The current implementation simply calls to_owned().into_shared(), so cloning cannot be avoided when the array is already ArcArray.
So use to_owned().shared() in a method of Data trait (just like into_owned()) and add a default implementation to it, which will not affect the existing structure. Then modify its implementation in OwnedArcRepr, which implements the Data trait, to avoid cloning when the array is already ArcArray.

@@ -96,6 +96,15 @@ pub unsafe trait Data: RawData {
where
Self::Elem: Clone,
D: Dimension;

fn to_share<D>(self_: &ArrayBase<Self, D>) -> ArrayBase<OwnedArcRepr<Self::Elem>, D>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, let's name this method to_shared IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, they should be consistent

D: Dimension,
{
// to shared using clone of OwnedArcRepr without clone of raw data.
ArrayBase {
Copy link
Member

@bluss bluss Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this method body just be self_.clone()? We should avoid writing out the ArrayBase constructor just to keep maintainability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. Thanks for pointing that out.

@@ -96,6 +96,15 @@ pub unsafe trait Data: RawData {
where
Self::Elem: Clone,
D: Dimension;

fn to_share<D>(self_: &ArrayBase<Self, D>) -> ArrayBase<OwnedArcRepr<Self::Elem>, D>
Copy link
Member

@bluss bluss Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn to_share<D>(self_: &ArrayBase<Self, D>) -> ArrayBase<OwnedArcRepr<Self::Elem>, D>
#[doc(hidden)]
fn to_shared<D>(self_: &ArrayBase<Self, D>) -> ArrayBase<OwnedArcRepr<Self::Elem>, D>

Like the rest of the methods. And it should have a doc comment here still - as documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the corresponding modifications have been added.

@bluss
Copy link
Member

bluss commented Jan 15, 2021

I prefer merging with a merge commit, makes it clear what every unit of contribution has been. This PR contains a commit called "back consist" which seems messy. I'll prefer to squash merge so that that one is not visible.

@bluss
Copy link
Member

bluss commented Jan 15, 2021

Thanks for this fix!

@bluss bluss merged commit c4482eb into rust-ndarray:master Jan 15, 2021
@SparrowLii
Copy link
Contributor Author

SparrowLii commented Jan 15, 2021

This PR contains a commit called "back consist" which seems messy. I'll prefer to squash merge so that that one is not visible.

That is my mistake. I made some unnecessary changes on my master branch, so I used reset to eliminate it. Squash merge is all right.

@SparrowLii SparrowLii deleted the to_shared branch January 16, 2021 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants