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 reserve method for owned arrays #1268

Merged
merged 9 commits into from
Apr 6, 2024
Merged

Conversation

ssande7
Copy link
Contributor

@ssande7 ssande7 commented Mar 11, 2023

This PR adds an array.reserve(axis, additional) method for use when appending multiple times to an array.
It abstracts over the code in append that was calling OwnedRepr::reserve in order to expose it.

Benchmark results (appending 100 times to an empty array):

test push_no_reserve ... bench:         900 ns/iter (+/- 52)                                                                                                                          
test push_reserve    ... bench:         739 ns/iter (+/- 50) 

Note I'm new to the crate so I may have missed something, but happy to iterate if there's more to do.

@bluss
Copy link
Member

bluss commented Apr 6, 2024

Rebased, resolved conflict, added test for nonzero alloc to first element pointer

@bluss
Copy link
Member

bluss commented Apr 6, 2024

Thanks a lot for this

pub fn reserve(&mut self, axis: Axis, additional: usize) -> Result<(), ShapeError>
where D: RemoveAxis
{
debug_assert!(axis.index() < self.ndim());
Copy link
Member

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?

Copy link
Member

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 🙂

Copy link
Contributor Author

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?

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.

@bluss bluss added this pull request to the merge queue Apr 6, 2024
Merged via the queue into rust-ndarray:master with commit d664f3d Apr 6, 2024
10 checks passed
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