Getting started with PrecompileTools (Julia 1.9.2) - how to save precompiled code to disk?

The documentation for PrecompileTools states “particularly with Julia 1.9 and higher, the precompiled code can be saved to disk, so that it doesn’t need to be compiled freshly in each Julia session.” It is unclear to me what has to be done in the setup to ensure that precompiled code is saved to disk. Is it saved just by using @compile_workload in the module? If so, where is it saved and what needs to be done in a new Julia session such that it doesn’t precompile again?

1 Like

Thanks for asking! This is one of those questions where it would be great if you could propose better phrasing; it’s hard for those who wrote the code to understand how others approach it.

To answer your question, there is literally nothing you have to do, the code gets automatically saved, and it is automatically loaded when you say using MyPackage. The only complexities are the rules for what gets saved:

  • any compiled code for methods defined in your package get saved, regardless of whether you use PrecompileTools. So will at least some of the code that your methods need (to be specific but technical, anything that type-inference can see that you need).
  • PrecompileTools.@compile_workload ensures that absolutely everything in the block that is freshly compiled (because you’re exercising it for the first time) gets cached automatically, regardless of ownership.
5 Likes

Thank you for your quick answer!
I think if there is nothing the user has to do, changing the wording to “particularly with Julia 1.9 and higher, the precompiled code is saved to disk…” would be helpful!

I guess my new question is what exactly is being saved and automatically loaded? My example scenario is

module MyModule

using PrecompileTools

export TV

@compile_workload begin
     include("foo.jl") #Defines TV
end 

end

where foo.jl contains a function that produces TV. Normally, just doing include("foo.jl") not in a module takes a very long time. My (perhaps incorrect) understanding of using @compile_workload is that TV would be cached after using MyModule so that in a new session, it would be very quick to load. Am I understanding this package correctly?

One can’t say one way or another: it depends on the contents of foo.jl.

Some things, like method definitions and consts:

function bar(x)
    return x + 1
end

const threshold = 1e-3

have been cached automatically since time immemorial. If that’s all that’s in foo.jl, you don’t need PrecompileTools.

Compilation: what happens the first time you execute bar(17.5) (for complex functions you’ll note the lag the first time you execute it). Some compiled code was cached since before Julia 1.0, but we’ve only systematically cached everything starting with Julia 1.9.

1 Like