List functions that are not type stable?

Hi all,

Is there a quick way to list functions that are not type stable during execution in a given module? I’m aware of the @code_warntype macro, or the Cthulu package, but it can be quite cumbersome to descend into all functions and test them one by one.

Thanks!

Not really answering your question but this recent paper on “Type Stability in Julia” might interest you. Apart from the formal part (which, frankly, I didn’t try to understand), they also seem to have systematically analyzed the type stability of several Julia packages. I would assume that the tools / the code with which they did it is accessible somewhere.

1 Like

@ulysses (first author of the paper): First of all, cool analysis! Do you think that the tool that you used to analyze the type (in)stability of methods of a package could be made a Julia package? Ideally, this would make it easy for people to analyze their own packages.

2 Likes

For analysis of a toplevel call, I usually use JET.jl and its @report_call macro.

3 Likes

Hm…

julia> @code_warntype rand([1, 2.2, "string"])
MethodInstance for rand(::Vector{Any})
  from rand(X) in Random at /some/path/share/julia/stdlib/v1.7/Random/src/Random.jl:259
Arguments
  #self#::Core.Const(rand)
  X::Vector{Any}
Body::Any
1 ─ %1 = Random.default_rng()::Core.Const(Random.TaskLocalRNG())
│   %2 = Random.rand(%1, X)::Any
└──      return %2

julia> using JET

julia> @report_opt rand([1, 2.2, "string"])
No errors !
1 Like

That function seems perfectly stable to me - you put in a Vector{Any} after all. The output type is perfectly well defined by the input type.

3 Likes

@carstenbauer thanks for mentioning it! I do think that the tool can be useful for this scenario. One caveat is that we were aiming at bulk data processing so the interface is geared towards: we generate some CSV files and you go and figure it. But that’s may be fine, actually. Writing up how one would analyze a single package using the tool was always on my TODO list but I never got to it. Maybe I should.

Btw the right GitHub repo to reference is GitHub - prl-julia/julia-type-stability

4 Likes

Thank you all for your suggestions!

Jet.jl and @report_opt definitely looks like the right tool for my use case, however it is a bit verbose and doesn’t seem to catch all the issues with type-unstable functions in my package (I haven’t tried to narrow it down to a MWE though).

@ulysses impressive work! A user-friendly tool for analyzing packages based on this would be amazing!

So at the moment I’ll keep using Cthulhu