-
Notifications
You must be signed in to change notification settings - Fork 103
docs: Add optimizer documentation #1481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jim8y
merged 2 commits into
neo-project:master-n3
from
Jim8y:docs/optimizer-documentation
Jan 27, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Neo Smart Contract Optimizer | ||
|
|
||
| The Neo Compiler includes a powerful bytecode optimizer that reduces contract size and gas consumption. This document describes the available optimization strategies. | ||
|
|
||
| ## Overview | ||
|
|
||
| The optimizer runs after initial compilation and applies various strategies to improve the generated NeoVM bytecode. Each strategy can be enabled/disabled independently. | ||
|
|
||
| ## Optimization Strategies | ||
|
|
||
| ### 1. Peephole Optimization (`Peephole.cs`) | ||
|
|
||
| **Purpose**: Pattern-based local optimizations that replace inefficient instruction sequences with more efficient equivalents. | ||
|
|
||
| **Key Optimizations**: | ||
| - **Dead code elimination**: Removes unreachable instructions | ||
| - **Redundant operation removal**: Eliminates unnecessary DUP/DROP pairs | ||
| - **Constant folding**: Pre-computes constant expressions at compile time | ||
| - **Instruction simplification**: Replaces complex sequences with simpler equivalents | ||
|
|
||
| **Example**: | ||
| ``` | ||
| Before: PUSH1 PUSH2 ADD | ||
| After: PUSH3 | ||
| ``` | ||
|
|
||
| ### 2. Jump Compression (`JumpCompresser.cs`) | ||
|
|
||
| **Purpose**: Optimizes jump instructions by using shorter jump variants when possible. | ||
|
|
||
| **Key Optimizations**: | ||
| - Converts `JMP_L` (5 bytes) to `JMP` (2 bytes) when target is within range | ||
| - Removes redundant jumps (jump to next instruction) | ||
| - Chains consecutive jumps | ||
|
|
||
| **Benefits**: Significant size reduction in contracts with many branches. | ||
|
|
||
| ### 3. Reachability Analysis (`Reachability.cs`) | ||
|
|
||
| **Purpose**: Identifies and removes unreachable code paths. | ||
|
|
||
| **Key Optimizations**: | ||
| - Detects dead code after unconditional jumps/returns | ||
| - Removes unused exception handlers | ||
| - Eliminates unreachable branches | ||
|
|
||
| ### 4. Miscellaneous Optimizations (`Miscellaneous.cs`) | ||
|
|
||
| **Purpose**: Various small optimizations that don't fit other categories. | ||
|
|
||
| **Key Optimizations**: | ||
| - NOP removal | ||
| - Redundant type conversion elimination | ||
| - Stack operation simplification | ||
|
|
||
| ## Usage | ||
|
|
||
| Optimization is enabled by default. Control it via compiler options: | ||
|
|
||
| ```bash | ||
| # Full optimization (default) | ||
| nccs MyContract.csproj | ||
|
|
||
| # Disable optimization | ||
| nccs MyContract.csproj --no-optimize | ||
|
|
||
| # Specific optimization level | ||
| nccs MyContract.csproj --optimize basic | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| Optimizer/ | ||
| ├── Analysers/ # Code analysis utilities | ||
| │ ├── InstructionCoverage.cs | ||
| │ └── TryCatchFinallyCoverage.cs | ||
| ├── AssetBuilder/ # Output generation | ||
| │ ├── DebugInfoBuilder.cs | ||
| │ └── OptimizedScriptBuilder.cs | ||
| ├── Strategies/ # Optimization strategies | ||
| │ ├── Peephole.cs | ||
| │ ├── JumpCompresser.cs | ||
| │ ├── Reachability.cs | ||
| │ └── Miscellaneous.cs | ||
| ├── BasicOptimizer.cs # Main optimizer entry point | ||
| └── DumpNef.cs # NEF file utilities | ||
| ``` | ||
|
|
||
| ## Adding New Strategies | ||
|
|
||
| 1. Create a new class in `Strategies/` | ||
| 2. Implement the optimization logic | ||
| 3. Add the `[Strategy]` attribute | ||
| 4. Register in `BasicOptimizer.cs` | ||
|
|
||
| ## Performance Impact | ||
|
|
||
| Typical optimization results: | ||
| - **Size reduction**: 10-30% smaller bytecode | ||
| - **Gas savings**: 5-20% lower execution cost | ||
| - **Compilation time**: Minimal overhead (~100ms) | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Neo Compiler Documentation](../../docs/) | ||
| - [NeoVM Instruction Reference](https://docs.neo.org/docs/n3/reference/neo_vm) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before I remember there was something like "--optimize all", was it removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added