Removing precompilation leads to lower allocations!?

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

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

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)
1 Like

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

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 :slight_smile:

1 Like

Discussion in Taking TTFX seriously: Can we make common packages faster to load and use - #9 by jlapeyre seems to answer some of my questions.

Julia bugs to track if you have such problems:

https://github.com/JuliaLang/julia/issues/35972

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

A few more issues with useful comments on the topic

https://github.com/JuliaLang/julia/issues/34055

https://github.com/JuliaGraphics/Colors.jl/issues/496

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.

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

1 Like

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

3 Likes