# Does compile time depend on type stability

**URL:** <https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548>\
**Category:** General Usage\
**Tags:** compilation, gtk, type-stability\
**Created:** [October 19, 2017, 7:26am UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548 "2017-10-19T07:26:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 7:26am UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/1 "2017-10-19T07:26:02Z")

</div>

Hi, I have a larger Gtk.jl application that requires more than one minute to start up. If I press some buttons, the initial compile time is also quite high. However, if I run the application a second time everything is totally snappy. So runtime is absolutely ok.

I was wondering if it actually helps when I try to make code type stable. So is type stable code faster to compile?

---

<div class="post-metadata">

**Author:** ![aleksip](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@aleksip](https://discourse.julialang.org/u/aleksip)\
**Post date:** [October 19, 2017, 9:04am UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/2 "2017-10-19T09:04:50Z")

</div>

Have you tried precompiling your application? There are some comments regarding that in: [GitHub - JuliaGraphics/Gtk.jl: Julia interface to Gtk windowing toolkit.](https://github.com/JuliaGraphics/Gtk.jl)

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 9:11am UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/3 "2017-10-19T09:11:36Z")

</div>

Gtk.jl is precompiled (as a package).

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 20, 2017, 7:25pm UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/4 "2017-10-20T19:25:11Z")

</div>

Would be great to have an answer on this @jeff.bezanson maybe?  
I am fine with the runtime speed of my application but initial “compilation” cost is so high. Profiling tells me that inference is the hot spot, so will an attempt to reduce type instabilities actually reduce the compilation/inference overhead?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 20, 2017, 10:19pm UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/5 "2017-10-20T22:19:24Z")

</div>

For functions that are not runtime performance-critical, annotating arguments as `@nospecialize x` (or `x::ANY` on 0.6) might be worth a try. (In other words, sort of the opposite of the direction you were asking about.) HTH.

---

<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:** [October 21, 2017, 12:04am UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/6 "2017-10-21T00:04:27Z")

</div>

> [@tobias.knopp](#):
>
> So is type stable code faster to compile?

I’m not a dev, but one factor to consider is that if `foo(x)` contains a call to `f(y)`, and the type of `y` is inferred, then the compiler will recursively perform inference (but not specialization) on `f`. As long as `f(y)` is going to be called, that’s fine, but if `f(y)` is in a never-executed branch of an `if`, then that’s wasted computation. Thus: type stability might (at least theoretically) lengthen compilation time, especially if you have deep parametric types.

Or perhaps that’s a marginal concern. I don’t know. We’re lacking tools to measure where compilation time is happening. My code takes minutes to compile 😒 so I share your pain. I’m currently working on a package to expose `Base.Profile`’s data. Presumably, we could take the difference between a warm run and a cold run to produce a report on compilation time, but the details are tricky. Any suggestion welcome.

---

<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:** [October 21, 2017, 12:32am UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/7 "2017-10-21T00:32:38Z")

</div>

Also: [GitHub - timholy/SnoopCompile.jl: Making packages work faster with more extensive precompilation](https://github.com/timholy/SnoopCompile.jl). I haven’t tried it, but it’s been recommended.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 21, 2017, 12:57pm UTC](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/8 "2017-10-21T12:57:39Z")

</div>

SnoopCompile is in principle great but it currently does not work (on 0.6) and more importantly the issue is that precompile between different modules does not work. But its certainly the path forward.
