Performance
| Benchmark | Interpreter | JIT | Speedup |
|---|---|---|---|
| Sum loop (10K × sum(0..1000)) | 0.68s | 0.01s | 68x |
Building with JIT support
The JIT compiler requires thejit feature flag:
Running with JIT
Enable JIT compilation at runtime:Hot-spot detection
The VM profiles execution to identify “hot” code regions suitable for JIT compilation (src/jit/hotspot.rs):
- Loop threshold: 1000 iterations
- Function threshold: 100 calls
- Type stability: Monomorphic operations only
How detection works
- The compiler registers loops during bytecode generation
- The VM tracks iteration counts at loop headers
- When count exceeds threshold, mark as “hot”
- Analyze bytecode for JIT compatibility
- Compile to native code if suitable
JIT-compatible patterns
The JIT compiler currently supports integer range loops with specific patterns:Sum accumulation
Count iterations
Simple printing
Combined patterns
Not JIT-compatible
The following patterns fall back to the interpreter:Function calls
Multiple prints per iteration
Complex operations
String operations
Compilation architecture
The JIT compilation pipeline (src/jit/compiler.rs):
1. Bytecode analysis
Analyze the loop body to determine the computation pattern:- Accumulator variable
- Arithmetic operations (add, subtract, multiply)
- Print/println operations
- Invalid operations (function calls, etc.)
2. Cranelift IR generation
Translate bytecode to Cranelift intermediate representation:3. Native code compilation
Cranelift compiles the IR to machine code optimized for the target CPU:4. Execution
The VM calls the JIT-compiled function directly:External callbacks
Print operations in JIT-compiled code call back to Rust:Type profiling
The VM tracks runtime types at key program points (src/jit/types.rs):
JIT statistics
With--jit-stats, the VM prints compilation statistics:
Optimizations
The JIT compiler applies several optimizations:Register allocation
Cranelift’s register allocator keeps loop variables in CPU registers, avoiding memory loads.Loop unrolling
Cranelift may unroll small loop bodies for better instruction-level parallelism.Constant folding
Constants in the loop are folded at compile time.Inlining
External calls to print functions are inlined when beneficial.Limitations
Current JIT implementation supports:- Integer range loops only (
for i in start..end) - Integer arithmetic (add, subtract, multiply)
- Integer comparisons
- Print/println with integers
- Monomorphic types (single type per operation)
- Iterator-based loops (
for x in list) - Floating-point operations
- String operations
- Function calls
- Method calls
- Polymorphic loops (mixed types)
- Nested loops (outer loop JIT only)
Future enhancements
- Polymorphic inlining: Generate specialized code for top-N types
- Escape analysis: Allocate short-lived objects on stack
- Function inlining: Inline small function calls
- SIMD: Use vector instructions for data-parallel operations
- Floating-point support: JIT compile float loops
- Multi-tier compilation: Quick compile hot code, optimize later
Source references
- JIT compiler:
src/jit/compiler.rs - Hot-spot detector:
src/jit/hotspot.rs - Type profiling:
src/jit/types.rs - VM integration:
src/vm/mod.rs:595(try_jit_range_loop)