-O flag, similar to traditional compilers like GCC and Clang.
Optimization Levels
-O0: No Optimization
Disable all optimizations.When
-O0 is set, the optimizer immediately returns without performing any transformations.Use cases:- Debugging compiled output
- Fastest compilation times
- Testing unoptimized code paths
source/compiler/opt.js:8-O1: Basic Optimization (Default)
Enable basic optimizations. This is the default optimization level.Optimizations performed:Source reference:
- Peephole optimizations (local.get/local.set consolidation)
- Constant folding
- Dead code elimination (unused blocks)
- Redundant conversion removal (i32→f64→i32, etc.)
- Type conversion optimization
- Instruction combining (local.set + local.get → local.tee)
source/compiler/opt.js:1-248-O2: Advanced Optimization
Enable advanced optimizations. May be unstable.Includes all -O1 optimizations plus:
- Cyclone optimizer (partial constant evaluation)
- More aggressive transformations
-O2 is enabled, the Cyclone optimizer is automatically activated unless explicitly disabled.Source reference: source/compiler/index.js:88-91-O3: Maximum Optimization
Enable maximum optimizations (experimental).Currently has similar behavior to
-O2. Additional optimizations may be added in the future.Source reference: source/runtime/index.js:47Cyclone Optimizer
The Cyclone optimizer is Porffor’s partial constant evaluator - it’s fast and powerful (hence the name “cyclone”).Explicitly enable or disable the Cyclone optimizer.Cyclone performs:
- Constant propagation and folding
- Dead local elimination
- Stack-based partial evaluation
- Control flow simplification
- Type-based optimizations
source/compiler/cyclone.js:1-4Log Cyclone optimizer performance and results.Displays:
- Per-function optimization results (ops before → after)
- Total optimization time
- Size difference in bytes
source/compiler/index.js:132-147Profile-Guided Optimization
Enable profile-guided optimization.PGO runs the code to collect runtime profiling data, then uses that data to guide optimizations.Source reference:
source/compiler/index.js:93Log PGO performance and results.Source reference:
source/compiler/index.js:114-124Optimization Configuration
Wasm Optimization Runs
Number of optimization passes to run on each function.More runs can catch additional optimization opportunities but increase compilation time.Source reference:
source/compiler/opt.js:21Optimization Logging
Log individual optimizations as they’re applied.Useful for debugging optimizer behavior and understanding what transformations are applied.Source reference:
source/compiler/opt.js:55Specific Optimizations
Unused Code Elimination
Remove unused code and variables.
Fast Length
Non-compliant optimization to make This assumes
.length property access faster..length always returns a number without runtime type checking. This breaks spec compliance but can provide performance benefits.Source reference: source/compiler/codegen.js:1887Type Optimizations
Optimize based on type information.Enables type-guided optimizations throughout the compiler.
Remove unused type checking code.
Comparison Table
| Feature | -O0 | -O1 (default) | -O2 | -O3 |
|---|---|---|---|---|
| Peephole optimizations | ❌ | ✅ | ✅ | ✅ |
| Constant folding | ❌ | ✅ | ✅ | ✅ |
| Dead code elimination | ❌ | ✅ | ✅ | ✅ |
| Cyclone optimizer | ❌ | ❌ | ✅ | ✅ |
| Compile time | Fastest | Fast | Slower | Slower |
| Output size | Largest | Smaller | Smallest | Smallest |
| Stability | Stable | Stable | Experimental | Experimental |
Examples
Debug build (fast compilation)
Production build (balanced)
Aggressive optimization
Maximum optimization with logging
See Also
- Compiler Options - All compiler flags
- TypeScript Support - Type-guided optimizations