special::Type{MAGIC} where MAGIC

Is the foo(::Type{M}) where M trick documented anywhere in the docs? I looked around and couldn’t find anything. I keep feeling like I know what’s going on, but then I hit situations like this, on 0.6:

foo1(t::Type, x) = Nullable{t}(sin(x))
foo2(t::Type{T}, x) where T = Nullable{t}(sin(x))
foo3(t::Type{T}, x) where T = Nullable{T}(sin(x))

@btime foo1(Float64, 2)
  408.815 ns (2 allocations: 48 bytes)
@btime foo2(Float64, 2)
  17.359 ns (0 allocations: 0 bytes)
@btime foo3(Float64, 2)
  17.356 ns (0 allocations: 0 bytes)

@btime foo1($Float64, 2)
  499.789 ns (2 allocations: 48 bytes)
@btime foo2($Float64, 2)
  84.436 ns (1 allocation: 32 bytes)
@btime foo3($Float64, 2)
  77.956 ns (1 allocation: 32 bytes)

All three methods are @inferred correctly and their @code_warntype/llvm are AFAICT identical. Then why is foo1 slower? Why is splicing in the Float64 type worse?

3 Likes

https://github.com/JuliaCI/BenchmarkTools.jl/issues/50

1 Like