# How Do You Actually Check for Type Stability in Julia?

**URL:** <https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 24, 2025, 1:09pm UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364 "2025-04-24T13:09:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sakura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sakura/32/216486_2.png) [@Sakura](https://discourse.julialang.org/u/Sakura)\
**Post date:** [April 24, 2025, 1:09pm UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/1 "2025-04-24T13:09:36Z")

</div>

Hey everyone,

I have been diving into Julia lately & loving the speed but I am still wrapping my head around some of the concepts— type stability. I have heard it is a big deal for performance but I am not totally sure how to tell if a function is type-stable or not.

Also I have tried using `@code_warntype`; but the output can be pretty intimidating. All those red and green highlights feel more such as a warning sign than something helpful 😂

What should I look for in the `@code_warntype` output?  
Any simple examples of bad vs good type stability?  
Are there tools or tricks to make this easier to understand?

Totally different note but I have been using Julia alongside Python lately while preparing some[Python interview questions & I am starting to appreciate both for different strengths.

Thank you…🙂

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 24, 2025, 1:13pm UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/2 "2025-04-24T13:13:49Z")

</div>

If all you want is a simple yes/no, `@inferred` is what you want. e.g.

```julia
import Test: @inferred
julia> f(x) = x>0 ? 1 : 1.0
f (generic function with 2 methods)

julia> @inferred f(1)
ERROR: return type Int64 does not match inferred return type Union{Float64, Int64}

```

If you then want to dig in, `@code_warntype` and `Cthulhu.jl` are the way to go. The way to read them is that red means you’ve found something type unstable. Green is type stable (and yellow is a small union which you can usually treat as green, but not always)

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 26, 2025, 8:19am UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/3 "2025-04-26T08:19:25Z")

</div>

It’s important to understand that type-stability is not a well-defined term. To quote from [https://arxiv.org/pdf/2109.01950](https://arxiv.org/pdf/2109.01950)

> Effectively, there are two competing, type-related properties of function bodies. To address this confusion, we define and refer to them using two distinct terms:  
> • _type stability_ is when a function’s return type depends only on its argument types, and  
> • _type groundedness_ is when every variable’s type depends only on the argument types.

The second term is not widely used in the Julia community but it’s a useful notion nonetheless. For each concept, there is a corresponding set of tools:

- Type stability: `Test.@inferred`, DispatchDoctor.jl
- Type groundedness: `@code_warntype` (only for one level of nesting), JET.jl and Cthulhu.jl (for arbitrary nested functions)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 26, 2025, 9:50am UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/4 "2025-04-26T09:50:44Z")

</div>

> [@gdalle](#):
>
> • _type stability_ is when a function’s return type depends only on its argument types, and  
> • _type groundedness_ is when every variable’s type depends only on the argument types.

I’ve heard many variations of the term “stability” to describe that paper’s definition of groundedness, and we want that more; in an extreme case, we can `convert` the return value to an argument type for trivial type stability, and we’d still suffer all the performance drawbacks of the poorly inferred local variables (and expressions, the paper is including those in “variable’s”). On top of that, type groundedness in a method call doesn’t imply groundedness of the callees; I’m not even sure if there’s any consistent term for that kind of recursive groundedness, but that’s the aim for eliminating all unnecessary type checking.

---

<div class="post-metadata">

**Author:** ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)\
**Post date:** [April 26, 2025, 3:50pm UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/5 "2025-04-26T15:50:09Z")

</div>

The link to the paper appears to be broken. Do you have a working link?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 26, 2025, 4:00pm UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/6 "2025-04-26T16:00:24Z")

</div>

> [@Benny](#):
>
> On top of that, type groundedness in a method call doesn’t imply groundedness of the callees; I’m not even sure if there’s any consistent term for that kind of recursive groundedness, but that’s the aim for eliminating all unnecessary type checking.

I agree that what we want is recursive type-groundedness.

> [@brianguenter](#):
>
> The link to the paper appears to be broken. Do you have a working link?

That was just Discourse including the subsequent colon by default into the link, so I removed it. The link should be working now.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [April 26, 2025, 4:59pm UTC](https://discourse.julialang.org/t/how-do-you-actually-check-for-type-stability-in-julia/128364/7 "2025-04-26T16:59:15Z")

</div>

> [@gdalle](#):
>
> _type groundedness_ is when every variable’s type depends only on the argument types

This also misses small union optimizations where the types can vary depending on values, but for each variable you at least know a small set of concrete type options.
