# When should we use \`@nospecialize\`?

**URL:** <https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 17, 2018, 3:49pm UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392 "2018-04-17T15:49:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 17, 2018, 3:49pm UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/1 "2018-04-17T15:49:38Z")

</div>

The manual is sketchy about the use of `@nospecialize` (maybe it is still WIP?). I gather it is a necessary optimisation to avoid overburdening the dispatch tables. Is the end user advised to use this macro in some specific circumstance or coding pattern?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 17, 2018, 3:54pm UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/2 "2018-04-17T15:54:26Z")

</div>

You can use it when a function is called multiple times with different argument types, compiling that function is expensive but specialising on argument types does not have a significant impact on run-time performance.

For an example in the wild: [https://github.com/ZacLN/CSTParser.jl/blob/20b6eac1ec7db62ced97eadb09f0d4e9653db97a/src/CSTParser.jl#L178](https://github.com/ZacLN/CSTParser.jl/blob/20b6eac1ec7db62ced97eadb09f0d4e9653db97a/src/CSTParser.jl#L178) (`::ANY` is the old `@nospecialize`).

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [October 16, 2019, 8:16pm UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/3 "2019-10-16T20:16:56Z")

</div>

> [@kristoffer.carlsson](#):
>
> function is called multiple times with different argument types, compiling that function is expensive but specialising on argument types does no

what does that really mean? @nospecialize makes it faster to dispatch if I don’t specialize the function according the its argument types?

Is that what you are trying to say?

Thanks

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 16, 2019, 8:19pm UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/4 "2019-10-16T20:19:03Z")

</div>

`@nospecialize` makes julia compile a generic version of the function that works for all input types (so it only has to be compiled once). Normally, Julia compiles a separate version of the function for any combination of input types. This code will be more specialized and faster since it uses all the type information but in some cases, you rather avoid the extra compilation.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [October 16, 2019, 8:20pm UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/5 "2019-10-16T20:20:13Z")

</div>

Thank you very much.! basically this is hint to the compiler to avoid compilation for specific types.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 17, 2020, 6:58am UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/6 "2020-01-17T06:58:16Z")

</div>

> [@kristoffer.carlsson](#):
>
> Normally, Julia compiles a separate version of the function for any combination of input types.

I’m confused… in my understanding, **only one** specified version of function would be compiled **on the first time being called** with concrete inputs. That said, all other possible combinations of input types should be **irrelevant** until being explicitly used in later calls.

To be more precise, I mean the method `f(x::AbstractFloat) = 2x` is **not compiled** to anything, **until** a call like `f(1.0)` is made, where the specified method `f(::Float64)` is **created and compiled**. That said, all other specified versions like `f(::Float32)` are **not yet** created and compiled.

Is it true? if it is, I don’t see the point of defining like `f(@nospecialize x::AbstractFloat) = 2x` (that means I must have something wrong)

Please correct me. thanks.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 17, 2020, 7:40am UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/7 "2020-01-17T07:40:26Z")

</div>

> [@tomtom](#):
>
> Is it true?

Yes it is true.

> [@tomtom](#):
>
> I don’t see the point of defining like `f(@nospecialize x::AbstractFloat) = 2x` (that means I must have something wrong)

It is for cases where you don’t want to compile different versions of `f(::Float64)`, `f(::Float32)` etc, but only have one version that works for all input types.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [January 17, 2020, 7:51am UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/8 "2020-01-17T07:51:15Z")

</div>

> [@kristoffer.carlsson](#):
>
> but only have one version that works for all input types.

how could it be possible? even for a simple method like `f(x::AbstractFloat) = 2x`, the **implementations** of `2x` would be different for various floating types of `x`. What is the nature of that “one version”? It could not be a compiled machine code… 😵

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 17, 2020, 8:04am UTC](https://discourse.julialang.org/t/when-should-we-use-nospecialize/10392/9 "2020-01-17T08:04:40Z")

</div>

> [@tomtom](#):
>
> What is the nature of that “one version”? It could not be a compiled machine code…

In the end, everything that runs is “machine code” but yes, it will be more dynamic code, getting help from the julia runtime.
