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

create_box_gauss() allocates needlessly #9

Open
Shnatsel opened this issue Jul 8, 2019 · 5 comments
Open

create_box_gauss() allocates needlessly #9

Shnatsel opened this issue Jul 8, 2019 · 5 comments

Comments

@Shnatsel
Copy link
Contributor

Shnatsel commented Jul 8, 2019

create_box_gauss() currently returns a Vec<i32> with length specified by the input parameter n. Vectors are always allocated on the heap, which is costly. Even the tiny vectors of 3 elements add 3% to the total runtime, or 6% in asymmetric case that calculates the box twice.

Sadly const generics are not yet implemented in rustc so we cannot abstract it over the length of a slice, but we can work around this e.g. by returning an iterator.

@fschutt
Copy link
Owner

fschutt commented Jul 8, 2019

I'd wait for const generics. Const generics are coming pretty soon, I'm not sure how Iterators would solve the problem - you need at least one allocation (for the new, blurred image) and one allocation for the backbuffer.

@Shnatsel
Copy link
Contributor Author

Shnatsel commented Jul 8, 2019

Oh, the backbuffer is still needed. I'm talking about this allocation:

let mut sizes = Vec::<i32>::new();

@fschutt
Copy link
Owner

fschutt commented Jul 8, 2019

yeah, that could just be:

0..n.map(|i| if i < m { wl } else { wu }).collect();

I haven't worked on this crate in a long time, I just did this as a hobby at first.

@Shnatsel
Copy link
Contributor Author

Shnatsel commented Jul 8, 2019

.collect() would still allocate, but you can avoid collecting by returning the iterator directly. This is available since Rust 1.26: https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/impl-trait-for-returning-complex-types-with-ease.html

The trait you want to return in this case is Iterator

@Shnatsel
Copy link
Contributor Author

Shnatsel commented Jul 8, 2019

Well, for Impl Iterator instead of allocations is actually slightly slower than allocating a Vec (well, my attempt is), so I guess arrays are the way to go.

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

No branches or pull requests

2 participants