-
Notifications
You must be signed in to change notification settings - Fork 307
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
As standard layout method #616
As standard layout method #616
Conversation
Merge main rep master to fork master
This is quite needed to simplify the story around manipulation of strides, nice.
Another important piece is guarantees, to be added in the method documentation: what happens if I pass in an array that is already owned and arranged with standard layout? Do you still create a copy or do you return the same array? |
I think this functionality is important, but I'd like to refine the return type.
Based on the current signature of the method ( fn as_standard_layout(&self) -> ArrayCow<'_, A, D> to avoid copying the data when the array is already in the correct layout.
|
I've reworked the implementation to use the copy-on-write strategy. It is still a draft, but could you please take a look and tell me whether I am on a right track? Thanks a lot. |
Yes, it looks like you're on the right track. I would change the wording a little (rename |
Moved all `as_standard_layout`-related tests to the separate module
Seems like I should also write some test for the new |
The old implementation incorrectly handled the strides when the array was not in standard layout. (It created a `Vec` corresponding to an array in standard layout but didn't update the strides accordingly.) The new implementation calls `.to_owned()`, which handles the problem of efficiently creating an owned array, and then moves all the field values. Additionally, the `ensure_is_owned` function has been removed because it's equivalent to `DataMut::ensure_unique`.
These functions were used only in the implementation of `CowRepr::to_owned`. They may turn out to be useful in the future, but until there's a clear use case that is not served by `.view()` and `.into_owned()`, let's remove them.
The old implementation panicked when the variants were different; the new implementation performs the necessary clone instead of panicking. The variant of `self` after calling `clone_from_with_ptr` should match the variant of `other`. If the variants are initially different, the data from `other` needs to be cloned, and `self` needs to be changed to that variant.
This is analogous to `impl_owned_array`, `impl_views`, and `impl_raw_views`.
In the vast majority of cases, `A` will implement `Clone`. However, `ArrayCow` may still be useful for cases where `A` does not implement `Clone`.
The compiler should have a slightly easier time making this efficient because there are fewer type conversions this way.
I created andrei-papou/ndarray#4 with some refinements. I'd like to test the following for
These tests should be sufficient to check the behavior of Other than tests, the only remaining piece is documentation (normal doc comments as well as mentioning Would you be willing to move the |
Refine Cow stuff and as_standard_layout impl
@jturner314 Thanks for your refinements and guidance. As you suggested, I've moved all the |
Update fork master
@jturner314 I've updated the PR according to your comments. Any more suggestions on this? |
I added a little more to the docs and merged the the PR. Thanks for working on this and the previous PR! This is very useful functionality that's been necessary for quite a while. |
PR for #532
Added
as_contiguous
method toArrayBase
which accepts array by reference returns a copy of it in standard layout.