# Unsolvable dynamic dispatch in a \`for\` loop

**URL:** <https://discourse.julialang.org/t/unsolvable-dynamic-dispatch-in-a-for-loop/50643>\
**Category:** Performance\
**Tags:** question\
**Created:** [November 23, 2020, 6:20pm UTC](https://discourse.julialang.org/t/unsolvable-dynamic-dispatch-in-a-for-loop/50643 "2020-11-23T18:20:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [November 23, 2020, 6:20pm UTC](https://discourse.julialang.org/t/unsolvable-dynamic-dispatch-in-a-for-loop/50643/1 "2020-11-23T18:20:34Z")

</div>

Hello,

I posted about this problem last week. I think now I have the most simple example to illustrate it:

```julia
function my2outputs(a::Float64, g::Function)
    myRange = LinRange(-10.0,10.0,100)
    myMax, myLoc = findmax(map(x -> a^x / g(x), myRange))

    return myMax, myRange[myLoc]
end

function my3outputs(g::Function)
    auxRange = LinRange(1.0,3.0,100)

    Vs = Vector{Float64}(undef, 100)
    Xs = Vector{Float64}(undef, 100)
    for (ind, a) in enumerate(auxRange)
        Vs[ind], Xs[ind] = my2outputs(a, g)
    end

    auxMax, auxLoc = findmax(Vs)

    return auxMax, auxRange[auxLoc], Xs[auxLoc]
end

```

Juno’s compiler flags the following line as incurring in dynamic dispatch:

```julia
Vs[ind], Xs[ind] = my2outputs(a, g)

```

I can’t figure out why and how to fix it. Does anybody have a clue?

Thank you!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 23, 2020, 6:26pm UTC](https://discourse.julialang.org/t/unsolvable-dynamic-dispatch-in-a-for-loop/50643/2 "2020-11-23T18:26:11Z")

</div>

You want `g<:Function` this is important because all functions have their own type for performance reasons.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 23, 2020, 6:49pm UTC](https://discourse.julialang.org/t/unsolvable-dynamic-dispatch-in-a-for-loop/50643/3 "2020-11-23T18:49:23Z")

</div>

```julia
julia> using BenchmarkTools

julia> function my2outputs(a::Float64, g::F) where {F<:Function}
           myRange = LinRange(-10.0,10.0,100)
           myMax = -Inf; myx = zero(eltype(myRange));
           loga = log(a)
           for i ∈ eachindex(myRange)
               x = myRange[i]
               y = exp(x*loga) / g(x)
               newMax = y > myMax
               myMax = newMax ? y : myMax
               myx = newMax ? x : myx
           end

           return myMax, myx
       end
my2outputs (generic function with 1 method)

julia> function my3outputs(g::F) where {F<:Function}
           auxRange = LinRange(1.0,3.0,100)

           Vs = Vector{Float64}(undef, 100)
           Xs = Vector{Float64}(undef, 100)
           for (ind, a) in enumerate(auxRange)
               Vs[ind], Xs[ind] = my2outputs(a, g)
           end

           auxMax, auxLoc = findmax(Vs)

           return auxMax, auxRange[auxLoc], Xs[auxLoc]
       end
my3outputs (generic function with 1 method)

julia> @btime my3outputs(abs2)
  110.051 μs (2 allocations: 1.75 KiB)
(590.4900000000008, 3.0, 10.0)

```

Versus the original:

```julia
julia> @btime my3outputs(abs2)
  190.881 μs (503 allocations: 97.09 KiB)
(590.49, 3.0, 10.0)

```

For some reason, Julia master is around 40% slower than 1.5 when running the above. If I add `@inline` to `my2outputs`, it is about 40% faster than Julia 1.5 (which remains the same fast).

---

<div class="post-metadata">

**Author:** ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)\
**Post date:** [November 23, 2020, 9:11pm UTC](https://discourse.julialang.org/t/unsolvable-dynamic-dispatch-in-a-for-loop/50643/4 "2020-11-23T21:11:34Z")

</div>

> [@drarnau](#):
>
> Juno’s compiler flags the following line as incurring in dynamic dispatch:
> 
> ```julia
> Vs[ind], Xs[ind] = my2outputs(a, g)
> 
> ```
> 
> I can’t figure out why and how to fix it. Does anybody have a clue?

I think it has to do with [this](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing).
