|
| 1 | +# Neo Smart Contract Optimizer |
| 2 | + |
| 3 | +The Neo Compiler includes a powerful bytecode optimizer that reduces contract size and gas consumption. This document describes the available optimization strategies. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The optimizer runs after initial compilation and applies various strategies to improve the generated NeoVM bytecode. Each strategy can be enabled/disabled independently. |
| 8 | + |
| 9 | +## Optimization Types |
| 10 | + |
| 11 | +The compiler supports different optimization levels through the `OptimizationType` enum: |
| 12 | + |
| 13 | +```csharp |
| 14 | +[Flags] |
| 15 | +public enum OptimizationType : byte |
| 16 | +{ |
| 17 | + None = 0, |
| 18 | + Basic = 1, |
| 19 | + Experimental = 2, |
| 20 | + All = Basic | Experimental |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +### Available Options |
| 25 | + |
| 26 | +| Option | Value | Description | |
| 27 | +|--------|-------|-------------| |
| 28 | +| `None` | 0 | No optimization. Outputs raw compiled bytecode without any optimization passes. | |
| 29 | +| `Basic` | 1 | Safe, well-tested optimizations. Recommended for production use. Includes peephole optimization, jump compression, and dead code elimination. | |
| 30 | +| `Experimental` | 2 | Advanced optimizations that may be more aggressive. Use with caution and thorough testing. | |
| 31 | +| `All` | 3 | Enables all available optimizations (Basic + Experimental). Maximum optimization level. | |
| 32 | + |
| 33 | +### Usage Examples |
| 34 | + |
| 35 | +```csharp |
| 36 | +// Programmatic usage |
| 37 | +var options = new CompilationOptions |
| 38 | +{ |
| 39 | + Optimize = OptimizationType.Basic // Safe optimizations only |
| 40 | +}; |
| 41 | + |
| 42 | +var options = new CompilationOptions |
| 43 | +{ |
| 44 | + Optimize = OptimizationType.All // Maximum optimization |
| 45 | +}; |
| 46 | + |
| 47 | +var options = new CompilationOptions |
| 48 | +{ |
| 49 | + Optimize = OptimizationType.None // No optimization |
| 50 | +}; |
| 51 | +``` |
| 52 | + |
| 53 | +```bash |
| 54 | +# Command line usage |
| 55 | +nccs MyContract.csproj --optimize none # No optimization |
| 56 | +nccs MyContract.csproj --optimize basic # Basic optimizations (default) |
| 57 | +nccs MyContract.csproj --optimize experimental # Experimental only |
| 58 | +nccs MyContract.csproj --optimize all # All optimizations |
| 59 | +``` |
| 60 | + |
| 61 | +## Optimization Strategies |
| 62 | + |
| 63 | +### 1. Peephole Optimization (`Peephole.cs`) |
| 64 | + |
| 65 | +**Purpose**: Pattern-based local optimizations that replace inefficient instruction sequences with more efficient equivalents. |
| 66 | + |
| 67 | +**Key Optimizations**: |
| 68 | +- **Dead code elimination**: Removes unreachable instructions |
| 69 | +- **Redundant operation removal**: Eliminates unnecessary DUP/DROP pairs |
| 70 | +- **Constant folding**: Pre-computes constant expressions at compile time |
| 71 | +- **Instruction simplification**: Replaces complex sequences with simpler equivalents |
| 72 | + |
| 73 | +**Example**: |
| 74 | +``` |
| 75 | +Before: PUSH1 PUSH2 ADD |
| 76 | +After: PUSH3 |
| 77 | +``` |
| 78 | + |
| 79 | +### 2. Jump Compression (`JumpCompresser.cs`) |
| 80 | + |
| 81 | +**Purpose**: Optimizes jump instructions by using shorter jump variants when possible. |
| 82 | + |
| 83 | +**Key Optimizations**: |
| 84 | +- Converts `JMP_L` (5 bytes) to `JMP` (2 bytes) when target is within range |
| 85 | +- Removes redundant jumps (jump to next instruction) |
| 86 | +- Chains consecutive jumps |
| 87 | + |
| 88 | +**Benefits**: Significant size reduction in contracts with many branches. |
| 89 | + |
| 90 | +### 3. Reachability Analysis (`Reachability.cs`) |
| 91 | + |
| 92 | +**Purpose**: Identifies and removes unreachable code paths. |
| 93 | + |
| 94 | +**Key Optimizations**: |
| 95 | +- Detects dead code after unconditional jumps/returns |
| 96 | +- Removes unused exception handlers |
| 97 | +- Eliminates unreachable branches |
| 98 | + |
| 99 | +### 4. Miscellaneous Optimizations (`Miscellaneous.cs`) |
| 100 | + |
| 101 | +**Purpose**: Various small optimizations that don't fit other categories. |
| 102 | + |
| 103 | +**Key Optimizations**: |
| 104 | +- NOP removal |
| 105 | +- Redundant type conversion elimination |
| 106 | +- Stack operation simplification |
| 107 | + |
| 108 | +## Usage |
| 109 | + |
| 110 | +Optimization is enabled by default. Control it via compiler options: |
| 111 | + |
| 112 | +```bash |
| 113 | +# Full optimization (default) |
| 114 | +nccs MyContract.csproj |
| 115 | + |
| 116 | +# Disable optimization |
| 117 | +nccs MyContract.csproj --no-optimize |
| 118 | + |
| 119 | +# Specific optimization level |
| 120 | +nccs MyContract.csproj --optimize basic |
| 121 | +``` |
| 122 | + |
| 123 | +## Architecture |
| 124 | + |
| 125 | +``` |
| 126 | +Optimizer/ |
| 127 | +├── Analysers/ # Code analysis utilities |
| 128 | +│ ├── InstructionCoverage.cs |
| 129 | +│ └── TryCatchFinallyCoverage.cs |
| 130 | +├── AssetBuilder/ # Output generation |
| 131 | +│ ├── DebugInfoBuilder.cs |
| 132 | +│ └── OptimizedScriptBuilder.cs |
| 133 | +├── Strategies/ # Optimization strategies |
| 134 | +│ ├── Peephole.cs |
| 135 | +│ ├── JumpCompresser.cs |
| 136 | +│ ├── Reachability.cs |
| 137 | +│ └── Miscellaneous.cs |
| 138 | +├── BasicOptimizer.cs # Main optimizer entry point |
| 139 | +└── DumpNef.cs # NEF file utilities |
| 140 | +``` |
| 141 | + |
| 142 | +## Adding New Strategies |
| 143 | + |
| 144 | +1. Create a new class in `Strategies/` |
| 145 | +2. Implement the optimization logic |
| 146 | +3. Add the `[Strategy]` attribute |
| 147 | +4. Register in `BasicOptimizer.cs` |
| 148 | + |
| 149 | +## Performance Impact |
| 150 | + |
| 151 | +Typical optimization results: |
| 152 | +- **Size reduction**: 10-30% smaller bytecode |
| 153 | +- **Gas savings**: 5-20% lower execution cost |
| 154 | +- **Compilation time**: Minimal overhead (~100ms) |
| 155 | + |
| 156 | +## See Also |
| 157 | + |
| 158 | +- [Neo Compiler Documentation](../../docs/) |
| 159 | +- [NeoVM Instruction Reference](https://docs.neo.org/docs/n3/reference/neo_vm) |
0 commit comments