# How do I precompile a callable struct?

**URL:** https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037
**Category:** New to Julia
**Tags:** question, precompilation
**Created:** [May 31, 2024, 11:51pm UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037 "2024-05-31T23:51:42Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [May 31, 2024, 11:51pm UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/1 "2024-05-31T23:51:42Z")

</div>

I have something like:

```julia
struct S end
(::S)() = true

```

I think `precompile(A, ())` would precompile the consstructor `A`, right?

How do I precompile `(::S)()`?

I there a way to verify whether a function has been precompiled? This is what I have come across:

```julia
using MethodAnalysis: methodinstance
methodinstance(S, ())

```

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [June 1, 2024, 12:43am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/2 "2024-06-01T00:43:20Z")

</div>

Haven’t tried it, but I would assume `precompile(A(), ())` would do it!

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [June 1, 2024, 1:48am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/3 "2024-06-01T01:48:37Z")

</div>

I’ve tried that. But `MethodAnalysis.methodinstance(A(), ())` returns `nothing`.

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [June 1, 2024, 1:56am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/4 "2024-06-01T01:56:43Z")

</div>

Can get to my computer right now, but what about `A.instance`?

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [June 1, 2024, 2:04am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/5 "2024-06-01T02:04:22Z")

</div>

```julia
julia> precompile(A.instance, ())
true

julia> methodinstance(A.instance, ()) isa Nothing
true

```

---

<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: [June 1, 2024, 2:45am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/6 "2024-06-01T02:45:10Z")

</div>

It works when the type subtypes `Function`:

```julia-repl
julia> using MethodAnalysis

julia> struct S <: Function end

julia> (::S)() = 7

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

julia> methodinstance(S(), ())
MethodInstance for (::S)()

```

I notice that MethodAnalysis.jl dispatches on `Base.Callable` (`Union{Function,Type}`) in some places. Maybe make a PR?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 1, 2024, 3:41am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/7 "2024-06-01T03:41:44Z")

</div>

> [@mrufsvold](#):
>
> I would assume `precompile(A(), ())` would do it!

First session without `precompile`:

```julia
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
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

```

`@time`ing 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.

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [June 1, 2024, 4:02am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/8 "2024-06-01T04:02:14Z")

</div>

Does this mean that `S` must be a subtype of `Function` in order to precompile? Or is that just to make `MethodAnalysis.methodinstances` work?

`precompile(S(), ())` was always returning true. It’s just I didn’t know what I was precompiling and was looking for a way to check.

---

<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: [June 1, 2024, 4:08am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/9 "2024-06-01T04:08:53Z")

</div>

> [@sadish-d](#):
>
> Does this mean that `S` must be a subtype of `Function` in order to precompile? Or is that just to make `MethodAnalysis.methodinstances` work?

The latter.

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [June 1, 2024, 4:19am UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/10 "2024-06-01T04:19:00Z")

</div>

Got it. I also just saw @Benny’s solution.

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [June 2, 2024, 1:45pm UTC](https://discourse.julialang.org/t/how-do-i-precompile-a-callable-struct/115037/11 "2024-06-02T13:45:49Z")

</div>

MasonProtter has opened an issue:

> <https://github.com/timholy/MethodAnalysis.jl/issues/50>
>
> This was surfaced in https://discourse.julialang.org/t/how-do-i-precompile-a-cal…lable-struct/115037/5, this package is assuming that only subtypes of \`Base.Callable\` have methods, but that's not true.
> \`\`\`julia
> julia\> struct Foo end
> 
> julia\> (::Foo)() = 1
> 
> julia\> Foo()()
> 1
> 
> julia\> Foo() isa Base.Callable
> false
> 
> julia\> methodinstances((Foo(),))
> Core.MethodInstance\[\]
> \`\`\`
> versus
> \`\`\`julia
> julia\> struct Bar \<: Function end
> 
> julia\> (::Bar)() = 1
> 
> julia\> Bar()()
> 1
> 
> julia\> methodinstances((Bar(),))
> 1-element Vector{Core.MethodInstance}:
> MethodInstance for (::Bar)()
> \`\`\`
