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> 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?