/aa-jl:precompiling-julia-packagefrom aa-jl
Guides measuring and reducing Julia package load latency (TTFL) and first-call latency (TTFX) using PrecompileTools.jl. Includes measurement commands and workload setup.
Ships in aa-jl. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Julia compiles methods on demand, so the first time a package is loaded
(TTFL, time to first load) or a function is called (TTFX, time to first
execution) can dominate interactive latency. PrecompileTools.jl lets a package
declare a small, representative workload that Julia runs during precompilation
and caches, so that work is paid once when the package is installed or updated
instead of every time a fresh process starts.
Treat latency thresholds as prompts to measure, not pass/fail rules. Julia 1.13 reduces package precompile time substantially compared with Julia 1.12, so measure on the Julia version and hardware that matter to users. See the Julia 1.13 release highlights.
Useful initial signals are:
using MyPkg is slow.A repeated first call in a latency-sensitive REPL or service can justify work below those thresholds. A one-shot batch script that runs for hours may not benefit from precompiling its own code.
Never add a workload based on a guess. Record the current numbers first so you can prove the change helped. Always use a clean, up-to-date precompile cache and skip the user's startup file:
julia --project -e 'using Pkg; Pkg.precompile()'
Run the load several times in fresh processes and take the minimum — the first run may just be cold disk cache:
for i in 1 2 3; do
julia --startup-file=no --project -e 't=@elapsed using MyPkg; println(round(t, digits=3), " s")'
done
Time the first call of each representative function, in a fresh process. Do not call the function before the timed call, or you will measure the already compiled second call:
julia --startup-file=no --project -e '
using MyPkg
t = @elapsed MyPkg.foo(1, 2)
println("TTFX foo: ", round(t, digits=3), " s")
t = @elapsed MyPkg.bar([1.0, 2.0])
println("TTFX bar: ", round(t, digits=3), " s")
'
Each call in the same process can share compilation with earlier calls, so when isolating one function, measure it alone in its own process.
To see what is being compiled, add --trace-compile=stderr; each line is a
method compiled at runtime and is a candidate for the workload:
julia --startup-file=no --project --trace-compile=stderr -e 'using MyPkg'
PrecompileTools.jl must be a normal dependency of the package (add it to
[deps]). Put the workload in the package module, after the code it exercises:
module MyPkg
using PrecompileTools: @setup_workload, @compile_workload
# ... your package code ...
@setup_workload begin
# Setup runs ONLY during precompilation, never at load time.
# Build minimal but representative inputs here.
x = [1.0, 2.0, 3.0]
y = 2.0
@compile_workload begin
# Exercise the hot paths you want cached.
result = compute(x, y)
summarize(result)
end
end
end # module
The workload is package code that runs automatically during precompilation
(Pkg.precompile(), or when the package is installed or updated), so
precompiling a package executes that code. Only precompile code you trust.
Key properties that make this work:
@setup_workload wraps the whole block so it runs during precompilation and
is skipped at runtime. It also catches errors, so a broken workload cannot
make the package unloadable — but a silently failing workload also does
nothing, so check that precompilation actually exercises your code.@compile_workload marks the methods hit inside it for caching (bytecode
from Julia 1.9+, and native code via pkgimages on 1.9+).The goal is to cover the methods a user will realistically hit, at the lowest precompilation cost:
Any or toy types. Dispatch on
concrete types is what gets compiled, so foo(1, 2) does nothing for a later
foo(1.0, 2.0).Pkg.precompile() at install/update time
and enlarges the package image. Precompile the top TTFX offenders, not
everything.Sometimes load time is dominated not by missing precompiled code but by
invalidations: loading your package makes previously compiled methods from
other packages stale, so they must be recompiled. If TTFL stays high after
adding a workload, add @recompile_invalidations around your package code:
using PrecompileTools
PrecompileTools.@recompile_invalidations begin
include("api.jl")
include("backend.jl")
end
This detects methods invalidated by loading the package and re-precompiles them during the package's own precompilation. It is most useful for packages that extend or interact heavily with other packages' methods. Place it so it wraps the code responsible for the invalidations.
Re-run the Step 1 measurements with a fresh cache:
julia --project -e 'using Pkg; Pkg.precompile()'
Report the before/after numbers and whether they meet the package's real latency
goal; do not treat the initial signals above as universal limits. If nothing improved, check that the
workload actually ran (a swallowed error in @setup_workload will leave you
with a no-op) and that the types you exercised match the types callers use.
When you cannot tell which methods are worth precompiling, SnoopCompile.jl
records the full inference and can emit code you can fold into a workload:
using SnoopCompile
tinf = @snoopi_deep begin
MyPkg.foo(1, 2)
MyPkg.bar([1.0, 2.0])
end
Use the results to discover hot methods, then write a hand-tuned
@setup_workload — a curated workload is usually smaller and faster to
precompile than the raw output. @snoopr similarly reports runtime
invalidations. Reach for these only when measurement plus --trace-compile
is not enough to locate the cost.
Float64 inputs does not help a
user whose first call uses Int or Matrix{Float64}.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub atelierarith/atelier-arith-julia-development-skills --plugin aa-jl14 skills in this plugin
/auditing-julia-package-practicesUse when you review an existing or in-progress Julia package against this repository's development practices and want prioritized, evidence-based findings and concrete remediation suggestions/creating-julia-appUse when you create a CLI written in Julia packages or MCP servers/debugging-juliaUse when Julia code errors, throws an exception, produces wrong results, or crashes and you need to find the cause from the command line: capturing and reading stack traces, locating the failing method with @which/@code_warntype/@code_typed, finding potential runtime errors with JET.jl, and narrowing to a minimal reproduction/developing-julia-packageUse when you implement or edit code inside an existing Julia package: module/`include` layout, multiple dispatch, type annotations and stability, formatting, and docstrings/developing-julia-rust-bindingsUse when implementing or debugging Julia bindings to a Rust library with RustCall.jl or a native ABI. Covers memory ownership, Julia dispatch, reproducible builds, and isolating upstream failures with executable reproductions./documenting-julia-packageUse when you add, build, or publish documentation for a Julia package with Documenter.jl: the `docs/` layout, `docs/Project.toml`, `makedocs`/`deploydocs`, `@docs` and docstring blocks, doctests, and GitHub Pages deployment