8000 Add internal constructors from_data_ptr(...).with_strides_dim(...) and use everywhere by bluss · Pull Request #908 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
bluss merged 3 commits intomasterfrom
internal-constructors
Feb 4, 2021
Merged

Add internal constructors from_data_ptr(...).with_strides_dim(...) and use everywhere#908
bluss merged 3 commits intomasterfrom
internal-constructors

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 bluss force-pushed the internal-constructors branch from 0577c31 to 3c84693 Compare February 3, 2021 15:39
@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.

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>
@bluss bluss force-pushed the internal-constructors branch from bf760c6 to 0d4272f Compare February 3, 2021 23:28
@@ -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

0