neo
neo copied to clipboard
Optimize VM performance with pre-compiled jump table
Description
This PR significantly improves Neo VM performance by replacing the reflection-based jump table initialization with a pre-compiled static approach.
Performance Improvements
Benchmark Results:
- Jump Table Construction: 91μs → 6.8μs (13.3x faster)
- VM Initialization: 88μs → 0.69μs (127x faster)
- Overall Performance Gain: 1,235% - 12,613%
Key Changes:
- Replaced runtime reflection with compile-time method assignments
- Eliminated expensive and calls
- Direct opcode-to-method mapping using static initialization
Technical Details
Before (Reflection-based):
foreach (var mi in GetType().GetMethods())
{
if (Enum.TryParse<OpCode>(mi.Name, true, out var opCode))
{
Table[(byte)opCode] = (DelAction)mi.CreateDelegate(typeof(DelAction), this);
}
}
After (Pre-compiled):
Table[(byte)OpCode.PUSH1] = Push1;
Table[(byte)OpCode.ADD] = Add;
Table[(byte)OpCode.MUL] = Mul;
// ... direct assignments for all opcodes
Testing
- ✅ All existing unit tests pass (1,599+ tests)
- ✅ Added comprehensive benchmarks
- ✅ No breaking changes to public API
- ✅ Full backward compatibility maintained
Benchmarks
Two benchmark suites included:
-
- Full BenchmarkDotNet suite
-
- Quick demonstration
Run benchmarks:
NEO_VM_JUMPTABLE_BENCHMARK=1 dotnet run -c Release -f net9.0
Impact
This optimization provides dramatic performance improvements for:
- Node startup times
- Smart contract execution initialization
- High-throughput scenarios with multiple VM instances
- Development and testing cycles
The changes are particularly beneficial for blockchain nodes that need to create many VM instances for transaction processing.
Good job !
Where ar pre-compiled jump tables saved? Or need to compile every time?
no longer needed, close to clean up the pr list, such that we can focus on active prs.