Compilation Modes
Just-in-Time (JIT) Compilation
JIT compilation compiles code to machine code during runtime. The Dart VM includes a sophisticated JIT compiler with adaptive optimization. Key Features:- Compiles functions when first called
- Collects runtime profiling data
- Recompiles hot functions with optimizations
- Supports speculative optimization and deoptimization
- Enables hot reload for development
- Development and debugging
- Server applications with long-running processes
- Applications benefiting from adaptive optimization
Ahead-of-Time (AOT) Compilation
AOT compilation compiles code to machine code before execution. This produces standalone native executables or snapshots. Key Features:- All code compiled upfront
- No runtime compilation overhead
- Global static analysis and optimization
- Smaller runtime footprint (no compiler)
- Predictable performance
- Mobile applications (iOS, Android)
- Desktop applications
- Production deployments requiring fast startup
- Platforms prohibiting JIT compilation
Performance Comparison
- Startup Time
- Peak Performance
- Code Size
- Warmup Time
Winner: AOTAOT starts faster because:
- No compilation during startup
- Code ready to execute immediately
- Smaller memory footprint
How JIT Works
Compilation Pipeline
Adaptive Optimization
- Unoptimized Compilation - Fast compilation when function first called
- Profiling - Collect type feedback via inline caches
- Optimization - Compile hot functions with speculative optimizations
- Deoptimization - Fall back to unoptimized code if assumptions violated
Inline Caching
JIT tracks receiver types at call sites:How AOT Works
Compilation Pipeline
Type Flow Analysis (TFA)
AOT uses global static analysis to determine:- Which functions are reachable from
main() - Which classes are instantiated
- How types flow through the program
Catis never instantiated → exclude from snapshotCat.purris unreachable → exclude from snapshotanimal.makeSound()always callsDog.makeSound→ devirtualize
Optimization Levels
Both AOT and JIT support optimization levels:- O0 - Unoptimized
- O1 - Size Optimized
- O2 - Balanced (Default)
- O3 - Speed Optimized
Debug builds:
- Fast compilation
- No optimizations
- Best for debugging
- Largest code size
Snapshots
Both JIT and AOT use snapshots to serialize VM state:AppJIT Snapshots
JIT snapshots include compiled code and heap state:- Skip compilation warmup
- Faster startup than pure JIT
- Can still JIT compile new code
AppAOT Snapshots
AOT snapshots contain pre-compiled machine code:- Fastest startup
- No JIT compiler needed
- Smallest runtime
Choosing Between JIT and AOT
Use JIT When:
- Development - Hot reload, fast iteration
- Long-running servers - Adaptive optimization benefits
- Unpredictable workloads - JIT adapts to actual usage
- Peak performance critical - JIT often faster after warmup
Use AOT When:
- Mobile apps - Fast startup, iOS requirement
- Fast startup required - No warmup period
- Predictable workloads - Static analysis effective
- Small runtime needed - No JIT compiler overhead
- Consistent performance - No warmup variance
Command Line Usage
JIT Mode
AOT Mode
Debugging Compilation
JIT Debugging
Inspect JIT compilation:AOT Debugging
Analyze AOT compilation:Performance Tips
For JIT
-
Warmup - Allow time for optimization:
-
Avoid polymorphism in hot paths:
-
Use AppJIT for servers:
For AOT
-
Help tree-shaking:
-
Use final and const:
-
Avoid reflection: