# Precompile all the functions within a function

**URL:** <https://discourse.julialang.org/t/precompile-all-the-functions-within-a-function/23695>\
**Category:** General Usage\
**Created:** [April 30, 2019, 12:55pm UTC](https://discourse.julialang.org/t/precompile-all-the-functions-within-a-function/23695 "2019-04-30T12:55:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [April 30, 2019, 12:55pm UTC](https://discourse.julialang.org/t/precompile-all-the-functions-within-a-function/23695/1 "2019-04-30T12:55:22Z")

</div>

Hello everyone,  
I was playing around with the `precompile` function, and I noticed that it does not precompile all the functions that are called within the precompiled function. For example, consider:

```julia
@time a = 100 #make sure the @time macro is compiled

struct myStruct
a::Float64
b::Int64
c::Bool
end

function test_calc(input::myStruct)
    if(input.c)
        sine_val = sin(input.a * 2pi)
        sine_val2 = cos(input.b * 2pi)
        final_val = mod(sine_val, sine_val2)
        println(final_val)
        return final_val
    end
    return 0
end

function actual_calc(input::myStruct)
    final_val = test_calc(input)
    return final_val
end

#precompile the upper most function
precompile(actual_calc, (myStruct,))

input = myStruct(0.1, 1, true)

@time actual_calc(input)

```

The result of the `@time actual_calc(input)` calculation is:

```julia
0.054569 seconds (101.86 k allocations: 4.889 MiB)

```

As the allocation size hints, the `actual_calc` function was not compiled in all its inner calls with the `precompile` statement. I assume that the functions called `test_calc` function were not precompiled when precompiling `actual_calc`, even though I was expecting they would. Running `@time actual_calc(input)` again gives the expected result of no allocations for the compilation, and fast runtime, since it has been called once before. The compilation of all the inner functions happened in the previous call, and not in the `precompile` call:

```julia
0.000054 seconds (11 allocations: 288 bytes)

```

If I precompile `test_calc` aswell, the results remain the same, hinting me that the precompilation for the `sin`, `mod` and `println` functions that are called within it did not go through. Am I correct in making these assumptions? Is there a way to make sure to precompile the whole function graph for a specified set of arguments without having to call the function explicitly?

Thank you and have a great day!

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [May 1, 2019, 10:31am UTC](https://discourse.julialang.org/t/precompile-all-the-functions-within-a-function/23695/2 "2019-05-01T10:31:52Z")

</div>

The problem is that the precompile mechanism doesn’t know where to “store” the precompiled method instances. For example, `sin` belongs to `Base.Math` but that module doesn’t store the precompiled instance. Consequently any inference performed for the types you passed into `sin` basically get dropped. Documented (and steps on the road towards a fix) at [https://github.com/JuliaLang/julia/pull/31466](https://github.com/JuliaLang/julia/pull/31466).

---

<div class="post-metadata">

**Author:** ![vitreo12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitreo12/32/4028_2.png) [@vitreo12](https://discourse.julialang.org/u/vitreo12)\
**Post date:** [May 1, 2019, 2:42pm UTC](https://discourse.julialang.org/t/precompile-all-the-functions-within-a-function/23695/3 "2019-05-01T14:42:35Z")

</div>

Thanks! That looks like the problem here. I will look in the PR you linked for further developments
