# Removing precompilation leads to lower allocations!?

**URL:** <https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621>\
**Category:** Performance\
**Tags:** memory-allocation, precompilation\
**Created:** [December 25, 2021, 6:15pm UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621 "2021-12-25T18:15:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [December 25, 2021, 6:15pm UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621/1 "2021-12-25T18:15:25Z")

</div>

In one of my packages I tried to lower `import` times, so I added a couple of typical usage examples in a “precompile” function. Something like

```julia
function _my_precompile()
    # my "typical use case" code
    ...
end
function _precompile_()
    ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
    _my_precompile()
    # precompile directives from SnoopCompile
    ...
end
_precompile_()

```

However, in the presence of the `_my_precompile`, benchmarks of my code show thousands of allocations in code that should not be allocating. On the other hand, after I remove `_my_precompile`, all these spurious allocations disappear, as they should.

I am deeply confused. **How can precompilation functions that do not change the code lead to different allocation behavior during use of the library?**

I do not have a MWE yet, but if you want to verify this behavior for yourself, here is the commit which removes the precompilation. Before it the benchmarks show allocations, after it most allocations disappear. The benchmark itself is part of the commit message [https://github.com/Krastanov/QuantumClifford.jl/commit/27a5dadc953b8b10781a88ebd720d21a23db4caf](https://github.com/Krastanov/QuantumClifford.jl/commit/27a5dadc953b8b10781a88ebd720d21a23db4caf)

```julia
julia> versioninfo()
Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: AMD Ryzen 7 1700 Eight-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, znver1)

```

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [December 28, 2021, 2:20pm UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621/2 "2021-12-28T14:20:41Z")

</div>

I’m bumping this since I am also interested in the answer.  
A while ago I have posted a similar question to this:

> [@A few basic questions about Precompilation](https://discourse.julialang.org/t/a-few-basic-questions-about-precompilation/71545):
>
> Hi everyone, I have recently tried with little success to add a precompilation step to my own package project and hope someone can help me clarify a few problems. In this post it is explained that one can precompile methods by calling precompile(method, (types...), however making it work in practice seems quite cumbersome. In particular, it is pointed out that the compiler cannot really precompile functions called within method without further steps. If I have a lot of functions is it rea…

I think it ultimately has to do with the fact that precompilation needs more than to just call a typical use case function, apparently you also have to explicitly specify input types (?).  
At least that was my takeaway so far, hopefully someone who knows more on this can enlighten us here 🙂

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [January 21, 2022, 10:10pm UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621/3 "2022-01-21T22:10:41Z")

</div>

Discussion in [Taking TTFX seriously: Can we make common packages faster to load and use - #9 by jlapeyre](https://discourse.julialang.org/t/taking-ttfx-seriously-can-we-make-common-packages-faster-to-load-and-use/74949/9) seems to answer some of my questions.

Julia bugs to track if you have such problems:

> <https://github.com/JuliaLang/julia/issues/35800>
>
> Using Julia master (newer than 301db971daaeeb627ba768375538e6e7ff36d215), the fo…llowing does not infer the correct type (see also the discussion in #34048):
> \`\`\`julia
> using Test, LinearAlgebra
> \# @inferred mapreduce(norm, +, \[rand(1)\]);
> @inferred mapreduce(norm, +, \[rand(1)\]; init = 0.);
> \`\`\`
> Note that the third line works if you comment out the second line (start from a fresh Julia session), which makes testing this issue difficult.

[https://github.com/JuliaLang/julia/issues/35972](https://github.com/JuliaLang/julia/issues/35972)

Also check out the bugs in other packages that link to the ones above.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [January 22, 2022, 5:22pm UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621/4 "2022-01-22T17:22:21Z")

</div>

A few more issues with useful comments on the topic

[https://github.com/JuliaLang/julia/issues/34055](https://github.com/JuliaLang/julia/issues/34055)

[https://github.com/JuliaGraphics/Colors.jl/issues/496](https://github.com/JuliaGraphics/Colors.jl/issues/496)

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [March 9, 2022, 9:35am UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621/5 "2022-03-09T09:35:12Z")

</div>

I also see pretty weird allocations reports when using precompile statements (`round` and `log10` allocating).  
We hit this in [https://github.com/JuliaPlots/PlotUtils.jl/pull/136](https://github.com/JuliaPlots/PlotUtils.jl/pull/136).

@tim.holy might be interested in this precompilation scenario (which affects `Plots.jl`).

---

<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:** [March 10, 2022, 12:52pm UTC](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621/6 "2022-03-10T12:52:27Z")

</div>

Answer: just use nightly or the upcoming 1.8.0-beta2, it’s fixed.
