Skip to content
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

Add internal constructors from_data_ptr(...).with_strides_dim(...) and use everywhere #908

Merged
merged 3 commits into from
Feb 4, 2021

Conversation

bluss
Copy link
Member

@bluss bluss commented Feb 3, 2021

Use array builder from_data_ptr(...).with_strides_dim(...) where possible

This avoids using the naked ArrayBase { .. } and similar struct
constructors, which should make it easier for us to change
representation in the future if needed.

The constructor methods are unsafe since they rely on the caller to
assert certain invariants; this means that this change increases the
number of unsafe code blocks in ndarray, but that's only correct since
every ArrayBase construction is safety-critical w.r.t the validity of
the pointer, dimensions and strides taken together.

The resulting code is often easier to read - especially when we are only
updating the strides and dimensions of an existing array or view.

The .with_strides_dim() method requires the Dimension trait so that
appropriate debug assertions can be added in this method if desired.
This precludes us from using this method in the Clone impl, but that's
just a minor flaw and the only exception.

…es_dim

These methods will be used instead of the ArrayBase struct constructor
expression.
@bluss
Copy link
Member Author

bluss commented Feb 3, 2021

It is now using an empty one-dimensional array for the return value from ArrayBase::from_data_ptr. In the first draft it was a zero-dim array which is a smaller struct, but unfortunately those always have length 1, and thus there would be some arrays that were invalid. It wouldn't matter so much since it's temporary and nothing's accessing the array, but it's best to avoid anyway.

Copy link
Member

@jturner314 jturner314 left a comment

Choose a reason for hiding this comment

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

I like the new with_strides_dim method. I made a few minor comments.

src/impl_internal_constructors.rs Show resolved Hide resolved
src/impl_methods.rs Outdated Show resolved Hide resolved
src/impl_internal_constructors.rs Show resolved Hide resolved
src/impl_methods.rs Show resolved Hide resolved
src/impl_methods.rs Show resolved Hide resolved
bluss and others added 2 commits February 4, 2021 00:28
…sible

This avoids using the naked ArrayBase { .. } and similar struct
constructors, which should make it easier for us to change
representation in the future if needed.

The constructor methods are unsafe since they rely on the caller to
assert certain invariants; this means that this change increases the
number of `unsafe` code blocks in ndarray, but that's only correct since
every ArrayBase construction is safety-critical w.r.t the validity of
the pointer, dimensions and strides taken together.

The resulting code is often easier to read - especially when we are only
updating the strides and dimensions of an existing array or view.

The .with_strides_dim() method requires the Dimension trait so that
appropriate debug assertions can be added in this method if desired.
This precludes us from using this method in the `Clone` impl, but that's
just a minor flaw and the only exception.
Co-authored-by: Jim Turner <github@turner.link>
@@ -1498,22 +1490,15 @@ where
return Err(error::incompatible_shapes(&self.dim, &shape));
}
// Check if contiguous, if not => copy all, else just adapt strides
Copy link
Member Author

Choose a reason for hiding this comment

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

I see this comment is entirely incorrect (copied from reshape?) but the coming change that will fix into_shape should take care of that..

@bluss bluss merged commit a66f364 into master Feb 4, 2021
@bluss bluss deleted the internal-constructors branch February 4, 2021 18:45
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