neo icon indicating copy to clipboard operation
neo copied to clipboard

Optimize VM performance with pre-compiled jump table

Open Jim8y opened this issue 8 months ago • 2 comments

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:

  1. Replaced runtime reflection with compile-time method assignments
  2. Eliminated expensive and calls
  3. 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.

Jim8y avatar Jun 21 '25 04:06 Jim8y

Good job !

Wi1l-B0t avatar Jun 21 '25 05:06 Wi1l-B0t

Where ar pre-compiled jump tables saved? Or need to compile every time?

Wi1l-B0t avatar Jun 26 '25 12:06 Wi1l-B0t

no longer needed, close to clean up the pr list, such that we can focus on active prs.

Jim8y avatar Sep 22 '25 09:09 Jim8y