JuliaImages / ImageFiltering.jl

Julia implementations of multidimensional array convolution and nonlinear stencil operations
Other
99 stars 49 forks source link

Add preallocated `imgradients!` #135

Closed stillyslalom closed 4 years ago

stillyslalom commented 4 years ago

I have a time-series of volumetric 'images' from which I need to calculate gradients, and each calculation spends a solid fraction of its runtime on GC:

julia> img = rand(128,128,1024);

julia> @time imgradients(img, KernelFactors.bickley)

  0.911337 seconds (688.35 k allocations: 814.017 MiB, 11.63% gc time)

I'd like to provide preallocated arrays for both output and padding.

timholy commented 4 years ago

Buffer allocation should be < a dozen individual allocations. This merits detailed investigation, though I don't have time right now. (Profiling does not reveal dynamic dispatch, which was my first guess.)

timholy commented 4 years ago

This seems mostly due to the fact that Julia can't create wrappers to heap-allocated objects on the stack: https://stackoverflow.com/questions/47590839/unexpected-memory-allocation-when-using-array-views-julia/47607539#47607539. I don't think there's a good way of dealing with this now, other than wait for Julia to fix this.

timholy commented 4 years ago

While it would be nice to avoid the GC, it's worth noting that the performance is quite good, around 2ns/array element. Given that your filters nominally have 27 elements (9 once you take advantage of separability) and thus it's a minimum of 9 adds and multiplies/array element, that's pretty awesome.