fastblur icon indicating copy to clipboard operation
fastblur copied to clipboard

create_box_gauss() allocates needlessly

Open Shnatsel opened this issue 6 years ago • 5 comments

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.

Shnatsel avatar Jul 08 '19 19:07 Shnatsel

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.

fschutt avatar Jul 08 '19 19:07 fschutt

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

https://github.com/fschutt/fastblur/blob/3abecde49b3abdd73fe950e01eab9bf7b6e16dc9/src/blur.rs#L32

Shnatsel avatar Jul 08 '19 19:07 Shnatsel

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.

fschutt avatar Jul 08 '19 19:07 fschutt

.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 avatar Jul 08 '19 20:07 Shnatsel

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.

Shnatsel avatar Jul 08 '19 20:07 Shnatsel