How do I precompile a callable struct?

First session without precompile:

julia> struct S end; (::S)() = true

julia> methods(S())[1].specializations
svec()

julia> s1=S(); @time s1()
  0.000261 seconds (306 allocations: 22.656 KiB, 85.21% compilation time)
true

julia> methods(S())[1].specializations
MethodInstance for (::S)()

julia> s2=S(); @time s2()
  0.000008 seconds
true

Second session with precompile

julia> struct S end; (::S)() = true

julia> methods(S())[1].specializations
svec()

julia> precompile(S(), ())
true

julia> methods(S())[1].specializations
MethodInstance for (::S)()

julia> s1=S(); @time s1()
  0.000009 seconds
true

@timeing in a let block however makes the compilation time report go away; I think that’s the let block being compiled at top-level before execution because the docstring-suggested @time @eval recovers the report, not sure though.

1 Like