Skip to content
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

Throw less expensive exceptions from Evm #6406

Merged
merged 7 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ref byte PopBytesByRef()
{
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 readonly void Swap(int depth)
{
Trace(depth);
}

return true;
}

private readonly void Trace(int depth)
Expand Down
Loading
Loading