diff --git a/src/Nethermind/Nethermind.Evm/EvmState.cs b/src/Nethermind/Nethermind.Evm/EvmState.cs index cd2fa29cb3a..d90b1b6532e 100644 --- a/src/Nethermind/Nethermind.Evm/EvmState.cs +++ b/src/Nethermind/Nethermind.Evm/EvmState.cs @@ -23,6 +23,11 @@ public class EvmState : IDisposable // TODO: rename to CallState private class StackPool { private readonly int _maxCallStackDepth; + private readonly struct StackItem(byte[] dataStack, int[] returnStack) + { + public readonly byte[] DataStack = dataStack; + public readonly int[] ReturnStack = returnStack; + } // TODO: we have wrong call depth calculation somewhere public StackPool(int maxCallStackDepth = VirtualMachine.MaxCallDepth * 2) @@ -30,11 +35,9 @@ public StackPool(int maxCallStackDepth = VirtualMachine.MaxCallDepth * 2) _maxCallStackDepth = maxCallStackDepth; } - private readonly Stack _dataStackPool = new(32); - private readonly Stack _returnStackPool = new(32); + private readonly Stack _stackPool = new(32); - private int _dataStackPoolDepth; - private int _returnStackPoolDepth; + private int _stackPoolDepth; /// /// The word 'return' acts here once as a verb 'to return stack to the pool' and once as a part of the @@ -44,45 +47,27 @@ public StackPool(int maxCallStackDepth = VirtualMachine.MaxCallDepth * 2) /// public void ReturnStacks(byte[] dataStack, int[] returnStack) { - _dataStackPool.Push(dataStack); - _returnStackPool.Push(returnStack); + _stackPool.Push(new(dataStack, returnStack)); } - private byte[] RentDataStack() - { - if (_dataStackPool.TryPop(out byte[] result)) - { - return result; - } - - _dataStackPoolDepth++; - if (_dataStackPoolDepth > _maxCallStackDepth) - { - EvmStack.ThrowEvmStackOverflowException(); - } - - return new byte[(EvmStack.MaxStackSize + EvmStack.RegisterLength) * 32]; - } - - private int[] RentReturnStack() + public (byte[], int[]) RentStacks() { - if (_returnStackPool.TryPop(out int[] result)) + if (_stackPool.TryPop(out StackItem result)) { - return result; + return (result.DataStack, result.ReturnStack); } - _returnStackPoolDepth++; - if (_returnStackPoolDepth > _maxCallStackDepth) + _stackPoolDepth++; + if (_stackPoolDepth > _maxCallStackDepth) { EvmStack.ThrowEvmStackOverflowException(); } - return new int[EvmStack.ReturnStackSize]; - } - - public (byte[], int[]) RentStacks() - { - return (RentDataStack(), RentReturnStack()); + return + ( + new byte[(EvmStack.MaxStackSize + EvmStack.RegisterLength) * 32], + new int[EvmStack.ReturnStackSize] + ); } } private static readonly ThreadLocal _stackPool = new(() => new StackPool());