Skip to content

Commit

Permalink
Throw less expensive exceptions from Evm (#6406)
Browse files Browse the repository at this point in the history
* Reduce Evm eceptions

* Less exceptions

* Less exceptions

* Whitespace

* Remove signed benchmark

* Moar
  • Loading branch information
benaadams committed Dec 22, 2023
1 parent b86383f commit bb1840f
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 182 deletions.
21 changes: 0 additions & 21 deletions src/Nethermind/Nethermind.Evm.Benchmark/EvmStackBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,6 @@ public UInt256 Uint256(UInt256 v)
return value;
}

[Benchmark(OperationsPerInvoke = 4)]
[ArgumentsSource(nameof(ValueSource))]
public Int256.Int256 Int256(UInt256 v)
{
EvmStack<VirtualMachine.NotTracing> stack = new(_stack.AsSpan(), 0, NullTxTracer.Instance);

stack.PushSignedInt256(new Int256.Int256(v));
stack.PopSignedInt256(out Int256.Int256 value);

stack.PushSignedInt256(value);
stack.PopSignedInt256(out value);

stack.PushSignedInt256(value);
stack.PopSignedInt256(out value);

stack.PushSignedInt256(value);
stack.PopSignedInt256(out value);

return value;
}

[Benchmark(OperationsPerInvoke = 4)]
public byte Byte()
{
Expand Down
35 changes: 19 additions & 16 deletions src/Nethermind/Nethermind.Evm/EvmStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,6 @@ public void PopLimbo()
}
}

public void PopSignedInt256(out Int256.Int256 result)
{
// tail call into UInt256
Unsafe.SkipInit(out result);
PopUInt256(out Unsafe.As<Int256.Int256, UInt256>(ref result));
}

/// <summary>
/// Pops an Uint256 written in big endian.
/// </summary>
Expand All @@ -233,9 +226,11 @@ public void PopSignedInt256(out Int256.Int256 result)
/// All it does is <see cref="Unsafe.ReadUnaligned{T}(ref byte)"/> and then reverse endianness if needed. Then it creates <paramref name="result"/>.
/// </remarks>
/// <param name="result">The returned value.</param>
public void PopUInt256(out UInt256 result)
public bool PopUInt256(out UInt256 result)
{
Unsafe.SkipInit(out result);
ref byte bytes = ref PopBytesByRef();
if (Unsafe.IsNullRef(ref bytes)) return false;

if (Avx2.IsSupported)
{
Expand Down Expand Up @@ -271,6 +266,8 @@ public void PopUInt256(out UInt256 result)

result = new UInt256(u0, u1, u2, u3);
}

return true;
}

public readonly bool PeekUInt256IsZero()
Expand Down Expand Up @@ -300,7 +297,7 @@ public Address PopAddress()
{
if (Head-- == 0)
{
EvmStack.ThrowEvmStackUnderflowException();
return null;
}

return new Address(_bytes.Slice(Head * WordSize + WordSize - AddressSize, AddressSize).ToArray());
Expand All @@ -310,7 +307,7 @@ public Address PopAddress()
{
if (Head-- == 0)
{
EvmStack.ThrowEvmStackUnderflowException();
return ref Unsafe.NullRef<byte>();
}

return ref _bytes[Head * WordSize];
Expand Down Expand Up @@ -356,9 +353,9 @@ public void PushLeftPaddedBytes(Span<byte> value, int paddingLength)
}
}

public void Dup(in int depth)
public bool Dup(in int depth)
{
EnsureDepth(depth);
if (!EnsureDepth(depth)) return false;

ref byte bytes = ref MemoryMarshal.GetReference(_bytes);

Expand All @@ -376,19 +373,23 @@ public void Dup(in int depth)
{
EvmStack.ThrowEvmStackOverflowException();
}

return true;
}

public readonly void EnsureDepth(int depth)
public readonly bool EnsureDepth(int depth)
{
if (Head < depth)
{
EvmStack.ThrowEvmStackUnderflowException();
return false;
}

return true;
}

public readonly void Swap(int depth)
public readonly bool Swap(int depth)
{
EnsureDepth(depth);
if (!EnsureDepth(depth)) return false;

ref byte bytes = ref MemoryMarshal.GetReference(_bytes);

Expand All @@ -403,6 +404,8 @@ public void Dup(in int depth)
{
Trace(depth);
}

return true;
}

private readonly void Trace(int depth)
Expand Down

0 comments on commit bb1840f

Please sign in to comment.