# Can't figure out why dynamic dispatch is occuring

**URL:** <https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089>\
**Category:** Performance\
**Created:** [June 9, 2020, 6:57pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089 "2020-06-09T18:57:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [June 9, 2020, 6:57pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/1 "2020-06-09T18:57:11Z")

</div>

I’m trying to figure out why the profiler and `@code_warntype` are picking up dynamic dispatch in the following MWE. The basic function applies matrix multiplications to each column of a matrix array and accumulates the result to another matrix array.

```julia
u = rand(4,100)
v = rand(4,100)
A = rand(4,4)
U = (u,v)

function init(A)
    mxm_accum!(X,x,j) = X[:,j] += 2 * (A*x)
    return mxm_accum!
end
mxm_accum! = init(A)

function foo(A,U)
    out = (x->A*x).(U)
    for j = 1:size(U[1],2)
        Uj = getindex.(U,:,j)
        mxm_accum!.(out,Uj,j)
    end
    return out
end

```

However, when I run profiler on it, it shows that the broadcasted function `mxm_accum!` is being dynamically dispatched.

 ![Screen Shot 2020-06-09 at 1.54.33 PM](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e7066eb4dd153c8f23babcb2b473f1977547470.png)

`@code_warntype` shows that the broadcast in this step seems to be type unstable, but I can’t figure out why this would be the case.

 ![Screen Shot 2020-06-09 at 1.55.46 PM](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae02e7ce71ec0094d166a53cfb71b5c77af44565.png)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 9, 2020, 7:08pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/2 "2020-06-09T19:08:25Z")

</div>

`mxm_accum!` is a global non constant.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [June 9, 2020, 7:23pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/3 "2020-06-09T19:23:57Z")

</div>

Passing it in and specializing fixes things, but now I’m confused about my “draft” MWEs. I moved the function definition outside b/c I didn’t understand what was going on.

If I define `mxm_accum!` inside the function, it’s no longer a global non-constant, but I sometimes get dynamic dispatch. The profiler shows that this MWE experiences dynamic dispatch

```julia
u = rand(4,100)
v = rand(4,100)
A = rand(4,4)
U = (u,v)

function foo(A,U)
    out = (x->A*x).(U)
    for j = 1:size(U[1],2)
        Uj = getindex.(U,:,j)
        mxm_accum!(X,x,j) = X[:,j] += 2 * (A*x)
        mxm_accum!.(out,Uj,j)
    end
    return out
end

```

while this MWE does not

```julia
u = rand(4,100)
v = rand(4,100)
A = rand(4,4)
U = (u,v)

function foo(A,U)
    mxm_accum!(X,x,j) = X[:,j] += 2 * (A*x)
    out = (x->A*x).(U)
    for j = 1:size(U[1],2)
        Uj = getindex.(U,:,j)
        mxm_accum!.(out,Uj,j)
    end
    return out
end

```

The difference is where I define `mxm_accum!` (inside/outside the loop), but I don’t know why dynamic dispatch happens in one and not the other.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 9, 2020, 7:28pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/4 "2020-06-09T19:28:51Z")

</div>

~~It’s the _global_ part that’s important. In the first case, `mxm_accum!` is a non-constant global variable (which is bad). In the second case, it’s a local variable (which is fine).~~ Edit: nope, misread the question. Sorry!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 9, 2020, 7:29pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/5 "2020-06-09T19:29:41Z")

</div>

Oh, sorry, I misread the question. Edited…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 9, 2020, 7:32pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/7 "2020-06-09T19:32:28Z")

</div>

After correctly reading the question (sorry), I can’t reproduce the result. I don’t see any type-instability in either of your MWE with `mxm_accum!` as local variables in Julia 1.4.0. What Julia version are you using?

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [June 9, 2020, 7:34pm UTC](https://discourse.julialang.org/t/cant-figure-out-why-dynamic-dispatch-is-occuring/41089/8 "2020-06-09T19:34:42Z")

</div>

Odd - I restarted Atom and now @profiler doesn’t show dynamic dispatch either.

These Heisenbugs will be the death of me. Sorry for the red herring.
