/aa-jl:profiling-julia-performancefrom aa-jl
Diagnoses slow Julia code using Profile, JET.jl, and AllocCheck.jl to identify type instability and unnecessary memory allocations before rewriting.
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.
When Julia code has insufficient runtime performance, do not start by rewriting it. Two root causes account for most slow Julia: type instability (the compiler cannot infer concrete types, so it falls back to dynamic dispatch) and unnecessary memory allocations (in hot loops, they trigger garbage collection and dominate the profile). Both are measurable, and fixing them usually matters far more than micro-tuning arithmetic.
This skill covers three tools:
Profile — Julia's built-in statistical CPU and allocation profiler
(standard library; there is no separate Profile.jl to install).JET.jl — static analysis of type instability and potential errors.AllocCheck.jl — static check that a function may allocate.First confirm the problem is runtime cost, not compilation latency. If the first call is slow but later calls are fast, the fix belongs in [[precompiling-julia-package]] instead.
Profile a function, not global scope, and feed it a representative workload. Code at global scope is not optimized the same way, and profiling it gives misleading results.
using Profile
function main()
# representative workload
end
main() # let compilation finish first
Profile.clear()
@profile main()
Profile.print(format=:flat, sortedby=:count)
Run it from a script so the workload is reproducible:
julia --startup-file=no --project -e '
using Profile
include("bench.jl")
main() # warm up
Profile.clear()
@profile main()
Profile.print(format=:flat, sortedby=:count)
'
Read the output by sample count. A large share in jl_apply_generic,
jl_gc_*, jl_gc_pool_alloc, or macro expansion/typeinf is a strong hint
of dynamic dispatch or allocation pressure. Add maxdepth=12 and
sortedby=:overhead when the tree is noisy, and noisefloor=2.0 to hide
sampling noise.
For a graphical flamegraph, ProfileView.jl provides
ProfileView.@profview main(); PProf.jl exports pprof-compatible profiles
(PProf.@profile main() then pprof -http=:8081 profile.pb.gz). These are
optional convenience layers over the same samples.
The built-in allocation profiler records the call stacks that allocate:
Profile.Allocs.clear()
Profile.Allocs.@profile sample_rate=1.0 main()
Profile.Allocs.print()
For a quick total, @time and @allocated report bytes and allocation count:
@time main()
@allocated main()
@time/@allocated tell you how much allocates; the allocation profiler
tells you where. Use them together.
JET reuses the compiler's inference to report where dispatch cannot be resolved statically. Point it at the hot function you found in Step 1, passing representative values (JET infers from their types):
using JET
@report_opt target_function(1, 2.0)
Look for lines like runtime dispatch detected: f(x::Any)::Any — those are the
dynamic dispatches to remove. @report_opt is noisy by design (it reports all
non-inferable calls, including benign ones in dependencies); focus on the
frames inside your own code. report_package(MyPkg; target_modules=(MyPkg,))
analyzes a whole package, and @report_call finds potential runtime errors
rather than performance issues.
Three practical caveats:
__init__. Only analyze code you trust.JET.JET_AVAILABLE after loading;
if it is false, JET is a no-op on that Julia version.Pkg.activate(; temp=true)) when dependency conflicts (notably with
JuliaInterpreter.jl via Revise.jl) prevent a working version. Do not make
JET a runtime dependency of the package under study.@code_warntype target_function(1, 2.0) remains a useful zero-dependency
spot check: red Any/Union types in the body mark instability.
Where JET finds type problems, AllocCheck.jl statically proves whether a
function may allocate by inspecting its LLVM IR. Annotate the function you
want to keep allocation-free:
using AllocCheck
@check_allocs hot_loop(x) = sum(x)
hot_loop(rand(100)) # passes: no allocation proven
A violating call raises AllocCheckFailure, whose errors field points to the
allocating site:
try
hot_loop(rand(3, 3))
catch err
err.errors[1]
end
Key details:
@check_allocs assumes no exception is thrown and ignores the
allocations involved in throwing. Add ignore_throw=false if throwing paths
must also be allocation-free.@check_allocs function behaves like a dynamic dispatch
and incurs small entry allocations. Wrap a top-level entry point or main
loop, not a function called per iteration, and treat allocations at the
entry as expected; the guarantee applies once the body starts running.Unlike @allocated (runtime bytes actually allocated) and the allocation
profiler (runtime stacks), AllocCheck is a static guarantee: it catches
allocation on paths your test inputs never exercised.
Typical fixes, in rough order of impact:
Any[] or [] forces boxing; prefer
Float64[] or Vector{Float64}(undef, n).const globals. Non-const globals are type-unstable by nature;
make them const or pass them as arguments.mul!, @views) operations, and avoid intermediate temporaries.::T assertions to document and enforce
the type at key boundaries, not everywhere.After each change, redo the measurement that exposed the problem:
@time main()
Profile.clear(); @profile main(); Profile.print(format=:flat, sortedby=:count)
Re-run @report_opt and @check_allocs on the changed functions and confirm
the reported dispatches/allocations are gone. Report before/after numbers
rather than claiming an improvement. If the hot spot merely moved, follow it —
optimization is a loop, not a single pass.
@time without a warm-up. The first execution includes
compilation; run the workload once before timing.@report_opt reports dispatch in Base and
dependencies too. Fix the frames in your code first.@check_allocs/::T add complexity and can
even slow execution; optimize the measured hot path only.BenchmarkTools.jl (@benchmark) rather than a single @time, which is
dominated by variance and one-time effects.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Other plugins' skills for the same job
14 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 deploymentnpx claudepluginhub atelierarith/atelier-arith-julia-development-skills --plugin aa-jl