# PrecompileTools: precompilation causing allocations in Julia 1.6.7

**URL:** <https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039>\
**Category:** General Usage\
**Tags:** precompilation\
**Created:** [August 21, 2023, 4:18pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039 "2023-08-21T16:18:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 21, 2023, 4:18pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/1 "2023-08-21T16:18:24Z")

</div>

TL;DR: When using PrecompileTools.jl and loading a package in Julia 1.6.7, allocations appear that are not expected in some functions. How can that be?

The issue:

I know that precompilation makes sense for Julia \>=1.9. But still I would like to understand what happened in my code.

I added some precompilation directives in a package [[here]](https://github.com/m3g/CellListMap.jl/blob/main/src/precompile.jl) and I have a bunch of tests in that package that check for allocations in some critical functions that must not allocate.

Now, the precompilation runs only for `Julia >= 1.9`, but initially it was running for all versions, and to my surprise, many functions that **did not allocate** in Julia 1.6 started to allocate, **only after the precompilation directives where added**.

This can be reproduced with that package by running the tests in Julia 1.6 after removing the `if Base.VERSION >= v"1.9"` from that precompilation workload.

So, I would like to understand how adding precompilation directives can affect the allocations (or any behavior for what matters) of the functions of a package. Is that known and only affects some Julia version prior to 1.9 (or any other version)?

Since precompilation runs even for older Julia versions, that can bring important performance regressions, and understanding what is going seems important.

---

<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:** [August 21, 2023, 4:36pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/2 "2023-08-21T16:36:26Z")

</div>

Probably related to this:

> [@Removing precompilation leads to lower allocations!?](https://discourse.julialang.org/t/removing-precompilation-leads-to-lower-allocations/73621):
>
> 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 i…

I don’t know what the issue is but I think it got figured out and fixed for later Julia versions.  
(Of course it wasn’t precompile tools back then but maybe simply related to running code at precompile time)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 21, 2023, 4:47pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/3 "2023-08-21T16:47:52Z")

</div>

Maybe it would be reasonable for precompilation work (with PrecompileTools) to run only for Julia versions greater than those for which the issue is solved?

We can do that by hand, but not all packages have performance unit tests, and these things can pass if not properly warned.

---

<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:** [August 21, 2023, 4:58pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/4 "2023-08-21T16:58:11Z")

</div>

Maybe. Clearly it needs to be installable on a wide variety of Julia versions, but it could be a no-op. I just didn’t feel comfortable making that choice for others because this is an issue that only sometimes occurs; other times, Julia 1.6 precompiles just fine. Since you can wrap the entire precompilation block in `if Base.VERSION >= ...`, I’d recommend doing it that way rather than having me impose a somewhat arbitrary choice on everyone.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 21, 2023, 5:00pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/5 "2023-08-21T17:00:50Z")

</div>

I understand, and that’s what I did. But I wander if other packages that don’t have performance unit tests just didn’t notice the issue (I had one, and just added the conditional block).

---

<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:** [August 21, 2023, 5:06pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/6 "2023-08-21T17:06:47Z")

</div>

What do you suggest? From what I can tell, there isn’t a good option here. So which one is worse?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 21, 2023, 5:46pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/7 "2023-08-21T17:46:52Z")

</div>

From what _I experienced_, I would say that making the `PrecompileTools` macros do something by default only in Julia \> 1.9 (and be an opt-in for older versions) would be better.

Anyway, it would be nice to have a warning in the manual of `PrecompileTools` concerning this issue. I’m not sure if a warning should be in the sense that precompilation can _always_ have runtime performance impacts, or if that is restricted to Julia 1.8 or older, because of that resolved issue.

(that’s a parallel but related question: should every runtime change in behavior associated to precompilation be reported as bug? In Julia?)

---

<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:** [August 21, 2023, 9:42pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/8 "2023-08-21T21:42:54Z")

</div>

Right, my point is that Person A (you) saw a slowdown. But Person B might see no slowdown and get a nice improvement in TTFX, using the same Julia version, with different packages. If we disable it for all Julia \< 1.9, we take away Person B’s improvement to ensure that Person A doesn’t stumble into a runtime regression. I’m not say we certainly shouldn’t do it, but it makes me uneasy doing so.

Any runtime regression is (now) a bug. E.g., [Significant performance regression when precompiled · Issue #50749 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/50749) got fixed (by [Don't partition alwaysinline functions by pchintalapudi · Pull Request #50766 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/50766)) pronto. That’s been backported for 1.10 but seemingly not 1.9.

---

<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:** [August 21, 2023, 9:50pm UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/9 "2023-08-21T21:50:00Z")

</div>

> [@lmiq](#):
>
> Anyway, it would be nice to have a warning in the manual of `PrecompileTools` concerning this issue.

Want to try a pull request? I can edit it to fix any errors.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 22, 2023, 12:45am UTC](https://discourse.julialang.org/t/precompiletools-precompilation-causing-allocations-in-julia-1-6-7/103039/10 "2023-08-22T00:45:59Z")

</div>

Done it: [add note about possible performance regressions by lmiq · Pull Request #29 · JuliaLang/PrecompileTools.jl · GitHub](https://github.com/JuliaLang/PrecompileTools.jl/pull/29/commits/cd3a1a11e28e6618020e9f964a0cc0305fef7d63)

(actually I started it before my previous answer, but sincerely, concerning what I can actually say about the issue and even _where_ to put the note, probably it will give more work than less 😔 )

Concerning the general issue, I was under the impression that all this was 1.9+ oriented, and that any utility before that was a possible unwanted side-effect. Thus my suggestion that it could be disabled by default in previous Julia versions. But if that’s not the case, of course the decision become more complicated.
