-
Notifications
You must be signed in to change notification settings - Fork 311
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 reserve method for owned arrays #1268
Merged
+214
−7
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6933dd8
Add reserve method for owned arrays
ssande7 aa077bf
Add benchmark for reserve()
ssande7 63062d3
Add convenience functions for reserving space for rows/columns in Array2
ssande7 a16707a
Add bounds checking and error documentation
ssande7 beb1439
Check for usize overflow of new capacity
ssande7 4b022eb
Make sure added capacity doesn't overflow usize::MAX
ssande7 8e030d1
Error on overflow when reserving
ssande7 25d2d04
reserve: Add test with inverted axis
bluss 7cc7141
reserve: Update tests to use into_raw_vec_and_offset
bluss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
use test::Bencher; | ||
|
||
use ndarray::prelude::*; | ||
|
||
#[bench] | ||
fn push_reserve(bench: &mut Bencher) | ||
{ | ||
let ones: Array<f32, _> = array![1f32]; | ||
bench.iter(|| { | ||
let mut a: Array<f32, Ix1> = array![]; | ||
a.reserve(Axis(0), 100).unwrap(); | ||
for _ in 0..100 { | ||
a.append(Axis(0), ones.view()).unwrap(); | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn push_no_reserve(bench: &mut Bencher) | ||
{ | ||
let ones: Array<f32, _> = array![1f32]; | ||
bench.iter(|| { | ||
let mut a: Array<f32, Ix1> = array![]; | ||
for _ in 0..100 { | ||
a.append(Axis(0), ones.view()).unwrap(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use ndarray::prelude::*; | ||
|
||
fn into_raw_vec_capacity<T, D: Dimension>(a: Array<T, D>) -> usize | ||
{ | ||
a.into_raw_vec_and_offset().0.capacity() | ||
} | ||
|
||
#[test] | ||
fn reserve_1d() | ||
{ | ||
let mut a = Array1::<i32>::zeros((4,)); | ||
a.reserve(Axis(0), 1000).unwrap(); | ||
assert_eq!(a.shape(), &[4]); | ||
assert!(into_raw_vec_capacity(a) >= 1004); | ||
} | ||
|
||
#[test] | ||
fn reserve_3d() | ||
{ | ||
let mut a = Array3::<i32>::zeros((0, 4, 8)); | ||
a.reserve(Axis(0), 10).unwrap(); | ||
assert_eq!(a.shape(), &[0, 4, 8]); | ||
assert!(into_raw_vec_capacity(a) >= 4 * 8 * 10); | ||
} | ||
|
||
#[test] | ||
fn reserve_empty_3d() | ||
{ | ||
let mut a = Array3::<i32>::zeros((0, 0, 0)); | ||
a.reserve(Axis(0), 10).unwrap(); | ||
} | ||
|
||
#[test] | ||
fn reserve_3d_axis1() | ||
{ | ||
let mut a = Array3::<i32>::zeros((2, 4, 8)); | ||
a.reserve(Axis(1), 10).unwrap(); | ||
assert!(into_raw_vec_capacity(a) >= 2 * 8 * 10); | ||
} | ||
|
||
#[test] | ||
fn reserve_3d_repeat() | ||
{ | ||
let mut a = Array3::<i32>::zeros((2, 4, 8)); | ||
a.reserve(Axis(1), 10).unwrap(); | ||
a.reserve(Axis(2), 30).unwrap(); | ||
assert!(into_raw_vec_capacity(a) >= 2 * 4 * 30); | ||
} | ||
|
||
#[test] | ||
fn reserve_2d_with_data() | ||
{ | ||
let mut a = array![[1, 2], [3, 4], [5, 6]]; | ||
a.reserve(Axis(1), 100).unwrap(); | ||
assert_eq!(a, array![[1, 2], [3, 4], [5, 6]]); | ||
assert!(into_raw_vec_capacity(a) >= 3 * 100); | ||
} | ||
|
||
#[test] | ||
fn reserve_2d_inverted_with_data() | ||
{ | ||
let mut a = array![[1, 2], [3, 4], [5, 6]]; | ||
a.invert_axis(Axis(1)); | ||
assert_eq!(a, array![[2, 1], [4, 3], [6, 5]]); | ||
a.reserve(Axis(1), 100).unwrap(); | ||
assert_eq!(a, array![[2, 1], [4, 3], [6, 5]]); | ||
let (raw_vec, offset) = a.into_raw_vec_and_offset(); | ||
assert!(raw_vec.capacity() >= 3 * 100); | ||
assert_eq!(offset, Some(1)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was the branch that returned early on additional == 0 removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason to hold up the PR on this question I think 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that was because it was faster (at least on my machine) without it, but it's been a while so could be worth re-testing with it in.