# How to check for unwanted specialization and how to prevent specialization?

**URL:** <https://discourse.julialang.org/t/how-to-check-for-unwanted-specialization-and-how-to-prevent-specialization/117117>\
**Category:** Internals & Design\
**Tags:** compilation, latency\
**Created:** [July 17, 2024, 1:04am UTC](https://discourse.julialang.org/t/how-to-check-for-unwanted-specialization-and-how-to-prevent-specialization/117117 "2024-07-17T01:04:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 17, 2024, 1:04am UTC](https://discourse.julialang.org/t/how-to-check-for-unwanted-specialization-and-how-to-prevent-specialization/117117/1 "2024-07-17T01:04:47Z")

</div>

Take this simple function: `f(::Any) = 7`. Its only argument is never used, so ideally Julia wouldn’t ever have to specialize for it, to prevent unnecessary compilation latency. The manual suggests using `@nospecialize` and `Base.@nospecializeinfer` to prevent unnecessary specialization, however I’m not sure if these macros actually have any effect. It seems like a specialization is being computed for each new argument type despite using these macros:

```julia-repl
julia> Base.@nospecializeinfer f(@nospecialize _) = 7
f (generic function with 1 method)

julia> f(:r)
7

julia> f(3)
7

julia> f(false)
7

julia> collect(only(methods(f)).specializations)
7-element Vector{Any}:
 MethodInstance for f(::Symbol)
 MethodInstance for f(::Int64)
 MethodInstance for f(::Bool)
 nothing
 nothing
 nothing
 nothing

```

How to prevent this unnecessary specialization, and how to be able to tell that there’s no unnecessary specialization?

---

<div class="post-metadata">

**Author:** ![KnutAM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/knutam/32/37720_2.png) [@KnutAM](https://discourse.julialang.org/u/KnutAM)\
**Post date:** [July 17, 2024, 8:44am UTC](https://discourse.julialang.org/t/how-to-check-for-unwanted-specialization-and-how-to-prevent-specialization/117117/2 "2024-07-17T08:44:42Z")

</div>

Replacing `_` with `x` avoided specialization for me (and only `@nospecialize` was required) Seems like a bug?

```julia
julia> f(@nospecialize _) = 7
f (generic function with 1 method)

julia> Base.specializations(@which f(1))
Base.MethodSpecializations(MethodInstance for f(::Int64))

julia> h(@nospecialize x) = 7
h (generic function with 1 method)

julia> Base.specializations(@which h(1))
Base.MethodSpecializations(MethodInstance for h(::Any))

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 17, 2024, 11:25am UTC](https://discourse.julialang.org/t/how-to-check-for-unwanted-specialization-and-how-to-prevent-specialization/117117/3 "2024-07-17T11:25:21Z")

</div>

See [optimizer: `@nospecialize` annotation doesn't work when applied to unnamed argument · Issue #44428 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/44428). I also think there is an issue/feature request about having `_` variables be `@nospecialize`d by default, but can’t find it now.
