From 02ada79aa3f89df6fb06608d925ee3850c9dff68 Mon Sep 17 00:00:00 2001 From: Martin Holters <martin.holters@hsu-hh.de> Date: Fri, 1 Mar 2024 13:31:15 +0100 Subject: [PATCH] minor conv optimization --- src/dspbase.jl | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/dspbase.jl b/src/dspbase.jl index 696d73ed4..6cfad3554 100644 --- a/src/dspbase.jl +++ b/src/dspbase.jl @@ -642,8 +642,14 @@ function _conv_td!(out, output_indices, u::AbstractArray{<:Number, N}, v::Abstra index_offset = first(CartesianIndices(u)) + first(CartesianIndices(v)) - first(output_indices) checkbounds(out, output_indices) fill!(out, zero(eltype(out))) - for m in CartesianIndices(u), n in CartesianIndices(v) - @inbounds out[n+m - index_offset] = muladd(u[m], v[n], out[n+m - index_offset]) + if length(u) ≤ length(v) # choose more efficient iteration order + for m in CartesianIndices(u), n in CartesianIndices(v) + @inbounds out[n+m - index_offset] = muladd(u[m], v[n], out[n+m - index_offset]) + end + else + for n in CartesianIndices(v), m in CartesianIndices(u) + @inbounds out[n+m - index_offset] = muladd(u[m], v[n], out[n+m - index_offset]) + end end return out end @@ -662,9 +668,11 @@ function conv!( calc_index_offset(ao, au::Base.OneTo, av::Base.OneTo) = # 2 throw(ArgumentError("output must not have offset axes if none of the inputs has")) calc_index_offset(ao, au, av) = 0 - output_indices = CartesianIndices(map(axes(out), axes(u), axes(v)) do ao, au, av - return (first(au)+first(av) : last(au)+last(av)) .- calc_index_offset(ao, au, av) - end) + output_indices = let calc_index_offset = calc_index_offset # prevent boxing + CartesianIndices(map(axes(out), axes(u), axes(v)) do ao, au, av + return (first(au)+first(av) : last(au)+last(av)) .- calc_index_offset(ao, au, av) + end) + end if algorithm===:auto algorithm = T <: FFTTypes ? :fast : :direct