# Improving speed of runtime dispatch detector

**URL:** <https://discourse.julialang.org/t/improving-speed-of-runtime-dispatch-detector/114697>\
**Category:** Performance\
**Tags:** question\
**Created:** [May 24, 2024, 6:45pm UTC](https://discourse.julialang.org/t/improving-speed-of-runtime-dispatch-detector/114697 "2024-05-24T18:45:59Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 25, 2024, 4:39pm UTC](https://discourse.julialang.org/t/improving-speed-of-runtime-dispatch-detector/114697/20 "2024-05-25T16:39:58Z")

</div>

Thanks.

Btw, I found a weird case of Julia’s specialization rules interfering with this interface:

```julia
using DispatchDoctor

@stable f(a, t::Type{T}) where {T} = sum(a; init=zero(T))

f([1f0, 1f0], Float32)

```

Despite the normal function being type stable, this actually fails the type specialization test

```julia
ERROR: TypeInstabilityError: Instability detected in function `f`
with arguments `(Vector{Float32}, DataType)`. Inferred to be 
`Any`, which is not a concrete type.
Stacktrace:
 [1] #_stable_wrap#1
   @ ~/PermaDocuments/DispatchDoctor.jl/src/DispatchDoctor.jl:25 [inlined]

```

because of Julia’s [type specialization rules](https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing):

> As a heuristic, Julia avoids automatically [specializing](https://docs.julialang.org/en/v1/manual/methods/#man-method-specializations) on argument type parameters in three specific cases: `Type`, `Function`, and `Vararg`.

Even if I modify `_stable_wrap` to be

```julia
_stable_wrap(f::F, caller::G, args::Vararg{Any,N}; kwargs...) where {F,G,N}

```

it _still_ fails, because now there are multiple non-specializing cases (`Vararg` and `Type`) – it seems like Julia lacks logic to deal with this situation.

I started a thread about this issue a while back:

> [@Specialization on vararg of types](https://discourse.julialang.org/t/specialization-on-vararg-of-types/108251/10):
>
> Is it possible that this unorthodox feature is also a bug because of this? Would be a shame considering we don’t have alternatives this deep into the type system.

but seems like that solution doesn’t work here.

Is there any way to force Julia to specialize no matter what?

---

_[View the full topic](https://discourse.julialang.org/t/improving-speed-of-runtime-dispatch-detector/114697)._
