Compilation, Latency & Deployment
Julia compiles methods on first call (JIT).
35.1 Understanding latency ("time to first X")
Julia compiles methods on first call (JIT). Since 1.9, packages cache native code in package images, greatly reducing first-call latency.
@time using Plots; @time plot(1:10) — measure load and first-call time.
PrecompileTools.jl — run representative workloads during precompilation:
Avoid invalidations (methods that force recompilation): diagnose with SnoopCompile.jl.
using PrecompileTools
@setup_workload begin
data = rand(10)
@compile_workload begin
process(data)
end
end35.2 PackageCompiler.jl
using PackageCompiler
create_sysimage([:Plots, :DataFrames]; sysimage_path="custom.so") # julia -J custom.so
create_app("MyApp", "MyAppCompiled") # standalone app bundle with executable
create_library("MyLib", "MyLibCompiled") # C-callable shared library35.3 juliac & static compilation (1.12+, experimental)
juliac --output-exe hello --trim hello.jl produces small standalone binaries for type-stable programs. StaticCompiler.jl compiles restricted Julia code to tiny executables.
35.4 Deployment options
Docker images (julia:1.11), with a precompiled environment or custom sysimage.
Web services with Oxygen.jl/Genie.jl/HTTP.jl behind a reverse proxy.
JuliaHub (cloud compute), Pluto notebooks as apps (PlutoSliderServer).
Calling Julia from Python (juliacall) or C (embedding API).
🧪 Part III Exercises
Take a type-unstable function, diagnose it with @code_warntype and JET, and fix it; benchmark before/after.
Write an allocation-free in-place stencil (1-D heat equation step) and verify 0 allocations with @btime.
Write a macro @repeat n expr that runs expr n times, and a macro that logs a function call's arguments and result.
Implement a trait IsSorted and dispatch a search function to binary vs linear search.
Build a producer/consumer pipeline with Channels that downloads 10 URLs concurrently.
Parallelize a Monte Carlo π estimate with threads (using per-task RNGs) and with Distributed.pmap; compare scaling.
Port a vector addition and a SAXPY to CUDA.jl (or KernelAbstractions on CPU if no GPU).
Call a C function from a shared library you compile yourself, including a callback with @cfunction.
Fit the Lotka–Volterra parameters to noisy data using DifferentialEquations.jl + Optimization.jl.
Train a small Flux MLP on the iris dataset; report test accuracy; then precompile a workload with PrecompileTools.