# List functions that are not type stable?

**URL:** <https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817>\
**Category:** Performance\
**Tags:** type-stability\
**Created:** [December 9, 2021, 10:24am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817 "2021-12-09T10:24:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![touste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touste/32/10758_2.png) [@touste](https://discourse.julialang.org/u/touste)\
**Post date:** [December 9, 2021, 10:24am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/1 "2021-12-09T10:24:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 9, 2021, 10:37am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/2 "2021-12-09T10:37:32Z")

</div>

Not really answering your question but this recent paper on [“Type Stability in Julia”](https://arxiv.org/abs/2109.01950) 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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 9, 2021, 10:40am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/3 "2021-12-09T10:40:00Z")

</div>

> [@carstenbauer](#):
>
> I would assume that the tools / the code with which they did it is accessible somewhere.

> **[Type Stability in Julia: Avoiding Performance Pathologies in JIT Compilation...](https://zenodo.org/records/5500548)**
>
> These code and data (artifact) support the claims of the OOPSLA '21 paper: Type Stability in Julia: Avoiding Performance Pathologies in JIT Compilation ACM DL: DOI: 10.1145/385527 Extended version: arXiv:2109.01950 The way to approach the artifact is...

> **[GitHub - ulysses4ever/julia-type-stability-overview](https://github.com/ulysses4ever/julia-type-stability-overview)**
>
> Contribute to ulysses4ever/julia-type-stability-overview development by creating an account on GitHub.

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 9, 2021, 10:53am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/4 "2021-12-09T10:53:02Z")

</div>

For analysis of a toplevel call, I usually use [JET.jl](https://juliahub.com/ui/Packages/JET/CNGCM/0.5.2) and its `@report_call` macro.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [December 9, 2021, 1:51pm UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/5 "2021-12-09T13:51:22Z")

</div>

Hm…

```julia
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 !

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 9, 2021, 2:14pm UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/6 "2021-12-09T14:14:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ulysses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ulysses/32/665_2.png) [@ulysses](https://discourse.julialang.org/u/ulysses)\
**Post date:** [December 10, 2021, 3:33am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/7 "2021-12-10T03:33:45Z")

</div>

@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 [https://github.com/prl-julia/julia-type-stability](https://github.com/prl-julia/julia-type-stability)

---

<div class="post-metadata">

**Author:** ![touste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touste/32/10758_2.png) [@touste](https://discourse.julialang.org/u/touste)\
**Post date:** [December 13, 2021, 10:53am UTC](https://discourse.julialang.org/t/list-functions-that-are-not-type-stable/72817/8 "2021-12-13T10:53:16Z")

</div>

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`…
