# Testing for type stability regressions

**URL:** <https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629>\
**Category:** Internals & Design\
**Tags:** testing, type-stability\
**Created:** [April 30, 2018, 11:31pm UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629 "2018-04-30T23:31:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [April 30, 2018, 11:31pm UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/1 "2018-04-30T23:31:58Z")

</div>

Is there a standard way of testing for regressions in type stability?

Currently I manually check there are no unions etc in @code\_warntype, but it would be preferable if that was a test.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 30, 2018, 11:43pm UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/2 "2018-04-30T23:43:37Z")

</div>

`Base.Test.@inferred`? It returns the value of the expression, so you can sprinkle it in your `runtests.jl` file, like `@test @inferred(f(12)) == 12`

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [May 1, 2018, 2:28am UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/3 "2018-05-01T02:28:57Z")

</div>

I’ve been using that, but it seems that it doesn’t really check that all the code generated is type stable, just that the result is?

@inferred might pass just fine but @code\_warntype can still show a whole lot of Union types. Maybe I need to be more specific in how I’m using it. It just seemed like there could be a general test of stability for a whole block of code, not just the result.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 1, 2018, 2:46am UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/4 "2018-05-01T02:46:45Z")

</div>

Perhaps [https://github.com/MikeInnes/Traceur.jl](https://github.com/MikeInnes/Traceur.jl) can help? In its current form, it just prints out issues like type-instability, but I’m sure it could be adapted to act as a unit test.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 1, 2018, 4:25am UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/5 "2018-05-01T04:25:34Z")

</div>

There is an open PR

[https://github.com/JuliaLang/julia/pull/23426](https://github.com/JuliaLang/julia/pull/23426)

and a package

[https://github.com/afniedermayer/InferenceUtilities.jl](https://github.com/afniedermayer/InferenceUtilities.jl)

which I asked the author to register, but there was no response, so it may be abandonned.

Perhaps the PR could be dusted off.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 1, 2018, 10:22am UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/6 "2018-05-01T10:22:11Z")

</div>

You can also use [TraceCalls.jl](http://nbviewer.jupyter.org/github/cstjean/TraceCalls.jl/blob/master/README.ipynb#Type-stability) to check for type-stability of all subcalls. Something like

```julia
trace = @trace YourModule f(x)
@test all(is_inferred, collect(trace))

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 1, 2018, 1:49pm UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/7 "2018-05-01T13:49:35Z")

</div>

Given that the end-result of a type-instability is unexpected allocations, you can also test the behavior with `@allocated` to ensure Julia isn’t allocating more than is necessary.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [May 2, 2018, 11:20am UTC](https://discourse.julialang.org/t/testing-for-type-stability-regressions/10629/8 "2018-05-02T11:20:42Z")

</div>

Thanks for all the suggestions! I didn’t know about any of those modules, I’ll look into them. The allocated trick also sounds good, would simultaneously test allocation regressions, which is also useful
