# How to track dynamic dispatch

**URL:** <https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826>\
**Category:** Performance\
**Tags:** question\
**Created:** [September 18, 2020, 7:34am UTC](https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826 "2020-09-18T07:34:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tisztamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tisztamo/32/16200_2.png) [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Post date:** [September 18, 2020, 7:34am UTC](https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826/1 "2020-09-18T07:34:09Z")

</div>

Similar to `--track-allocation`, I would like to see where in my code dynamic dispatch happened during a specific run.

Is there a tool for that?

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [September 18, 2020, 8:51am UTC](https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826/2 "2020-09-18T08:51:38Z")

</div>

You may be interested in some combination of the following two tools:

1. Traceur.jl
2. Cthulhu.jl

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 18, 2020, 9:02am UTC](https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826/3 "2020-09-18T09:02:16Z")

</div>

The juno profiler can give you this information, it’s available in the profile trace and I belive the code to extract it lives in Atom.jl

I used this function to thst effect  
[https://github.com/JunoLab/Atom.jl/blob/42599cbd62d730db2c6dcb2bf99d4c524130064c/src/profiler/profiler.jl#L7](https://github.com/JunoLab/Atom.jl/blob/42599cbd62d730db2c6dcb2bf99d4c524130064c/src/profiler/profiler.jl#L7)

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 18, 2020, 9:04am UTC](https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826/4 "2020-09-18T09:04:01Z")

</div>

[ProfileView](https://github.com/timholy/ProfileView.jl) also highlights dynamic dispatch calls. From the docs:

> It is also worth noting that red is (by default) a special color: it is reserved for function calls that have to be resolved at run-time. Because run-time dispatch (aka, dynamic dispatch, run-time method lookup, or a virtual call) often has a significant impact on performance, ProfileView highlights the problematic call in red. It’s worth noting that some red is unavoidable; for example, the REPL can’t predict in advance the return types from what users type at the prompt, and so the bottom `eval` call is red. Red bars are problematic only when they account for a sizable fraction of the top of a call stack, as only in such cases are they likely to be the source of a significant performance bottleneck.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 18, 2020, 9:07am UTC](https://discourse.julialang.org/t/how-to-track-dynamic-dispatch/46826/5 "2020-09-18T09:07:29Z")

</div>

Here’s a snippet to get you started

```julia
using Main.Atom.Profiler
using Main.Atom.Profiler.FlameGraphs
using AbstractTrees
AbstractTrees.children(d::Dict{Symbol,Any}) = d[:children] # To make the json dict iterable

#run profiler here......... 

data = Profile.fetch()
    @info "Analyzing"
    graph = FlameGraphs.flamegraph(data)
    Main.Atom.Profiler.pruneinternal!(graph)
    Main.Atom.Profiler.prunetask!(graph)
    js = Main.Atom.Profiler.tojson(graph)

    dynamic_dispatches = count(PostOrderDFS(js)) do l
        d = "dynamic-dispatch" ∈ l[:classes]
    end

```
