How to track dynamic dispatch

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?

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

  1. Traceur.jl
  2. Cthulhu.jl
1 Like

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

2 Likes

ProfileView 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.

2 Likes

Here’s a snippet to get you started

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
3 Likes