Skip to content

Commit

Permalink
Optimize EvmStack (#7843)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Dec 1, 2024
1 parent 6adf106 commit c874877
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 143 deletions.
10 changes: 7 additions & 3 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public static unsafe partial class Bytes
public static readonly IEqualityComparer<byte[]> EqualityComparer = new BytesEqualityComparer();
public static readonly IEqualityComparer<byte[]?> NullableEqualityComparer = new NullableBytesEqualityComparer();
public static readonly BytesComparer Comparer = new();
public static readonly ReadOnlyMemory<byte> ZeroByte = new byte[] { 0 };
public static readonly ReadOnlyMemory<byte> OneByte = new byte[] { 1 };
// The ReadOnlyMemory<byte> needs to be initialized = or it will be created each time.
public static ReadOnlyMemory<byte> ZeroByte = new byte[] { 0 };
public static ReadOnlyMemory<byte> OneByte = new byte[] { 1 };
// The Jit converts a ReadOnlySpan<byte> => new byte[] to a data section load, no allocation.
public static ReadOnlySpan<byte> ZeroByteSpan => new byte[] { 0 };
public static ReadOnlySpan<byte> OneByteSpan => new byte[] { 1 };

public const string ZeroHexValue = "0x0";
public const string ZeroValue = "0";
Expand Down Expand Up @@ -210,7 +214,7 @@ public static ReadOnlySpan<byte> WithoutLeadingZeros(this Span<byte> bytes)

public static ReadOnlySpan<byte> WithoutLeadingZeros(this ReadOnlySpan<byte> bytes)
{
if (bytes.Length == 0) return ZeroByte.Span;
if (bytes.Length == 0) return ZeroByteSpan;

int nonZeroIndex = bytes.IndexOfAnyExcept((byte)0);
// Keep one or it will be interpreted as null
Expand Down
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.Evm.Benchmark/EvmStackBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void GlobalSetup()
[ArgumentsSource(nameof(ValueSource))]
public UInt256 Uint256(UInt256 v)
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 0, NullTxTracer.Instance);
EvmStack<VirtualMachine.NotTracing> stack = new(0, NullTxTracer.Instance, _stack.AsSpan());

stack.PushUInt256(in v);
stack.PopUInt256(out UInt256 value);
Expand All @@ -50,7 +50,7 @@ public UInt256 Uint256(UInt256 v)
[Benchmark(OperationsPerInvoke = 4)]
public byte Byte()
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 0, NullTxTracer.Instance);
EvmStack<VirtualMachine.NotTracing> stack = new(0, NullTxTracer.Instance, _stack.AsSpan());

byte b = 1;

Expand All @@ -72,7 +72,7 @@ public byte Byte()
[Benchmark(OperationsPerInvoke = 4)]
public void PushZero()
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 0, NullTxTracer.Instance);
EvmStack<VirtualMachine.NotTracing> stack = new(0, NullTxTracer.Instance, _stack.AsSpan());

stack.PushZero();
stack.PushZero();
Expand All @@ -83,7 +83,7 @@ public void PushZero()
[Benchmark(OperationsPerInvoke = 4)]
public void PushOne()
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 0, NullTxTracer.Instance);
EvmStack<VirtualMachine.NotTracing> stack = new(0, NullTxTracer.Instance, _stack.AsSpan());

stack.PushOne();
stack.PushOne();
Expand All @@ -94,7 +94,7 @@ public void PushOne()
[Benchmark(OperationsPerInvoke = 4)]
public void Swap()
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 2, NullTxTracer.Instance);
EvmStack<VirtualMachine.NotTracing> stack = new(0, NullTxTracer.Instance, _stack.AsSpan());

stack.Swap(2);
stack.Swap(2);
Expand All @@ -105,7 +105,7 @@ public void Swap()
[Benchmark(OperationsPerInvoke = 4)]
public void Dup()
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 1, NullTxTracer.Instance);
EvmStack<VirtualMachine.NotTracing> stack = new(1, NullTxTracer.Instance, _stack.AsSpan());

stack.Dup(1);
stack.Dup(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void Debugger_Can_Alter_Data_Stack(string bytecodeHex)
if (tracer.CanReadState)
{
// we pop the condition and overwrite it with a false to force breaking out of the loop
EvmStack<VirtualMachine.IsTracing> stack = new(tracer.CurrentState.DataStack, tracer.CurrentState.DataStackHead, tracer);
EvmStack<VirtualMachine.IsTracing> stack = new(tracer.CurrentState.DataStackHead, tracer, tracer.CurrentState.DataStack);
stack.PopLimbo();
stack.PushByte(0x00);

Expand Down
Loading

0 comments on commit c874877

Please sign in to comment.