Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Fast Sync

### Additions and Improvements
- improve performance of OperandStack resizes for deep stacks (> 100 elements). Impacts general EVM performance while working with deep stacks [#8869](https://github.com/hyperledger/besu/pull/8869)

#### Fusaka devnets

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.vm.operations;

import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.internal.OperandStack;

import java.util.concurrent.TimeUnit;

import org.apache.tuweni.bytes.Bytes;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@State(Scope.Thread)
@Warmup(iterations = 6, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(value = TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
public class OperandStackBenchmark {
private static final int OPERATIONS_PER_INVOCATION = 1000;

@Param({"6", "15", "34", "100", "234", "500", "800", "1024"})
private int stackDepth;

private static final Bytes BYTES =
Bytes.fromHexString("0x3232323232323232323232323232323232323232323232323232323232323232");

@Benchmark
@OperationsPerInvocation(OPERATIONS_PER_INVOCATION)
public void fillUp() {
for (int i = 0; i < OPERATIONS_PER_INVOCATION; i++) {
OperandStack stack = new OperandStack(MessageFrame.DEFAULT_MAX_STACK_SIZE);
for (int j = 0; j < stackDepth; j++) {
stack.push(BYTES);
}
}
}
}
35 changes: 32 additions & 3 deletions evm/src/main/java/org/hyperledger/besu/evm/internal/FlexStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import static com.google.common.base.Preconditions.checkArgument;

import org.hyperledger.besu.evm.frame.MessageFrame;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
Expand All @@ -30,8 +32,25 @@
* @param <T> the type parameter
*/
public class FlexStack<T> {
/**
* Formula `x = round( y / ( (1 + 0,5)^n ) ) + 1`, computes the initial stack size, `x` that one
* has to start with to reach a maximum stack size, `y`, in `n` number of array resizes at a
* growth rate of 50%. Currently, for mainnet y=1024 and, if considering n=6 in the worst case,
* the start size is 91 which is reasonable for mainnet.
*/
private static final int INITIAL_SIZE =
(int) Math.round(MessageFrame.DEFAULT_MAX_STACK_SIZE / Math.pow(1.5D, 6D)) + 1;

private static final int INCREMENT = 32;
/**
* Soft limit imposed for growing arrays. JVMs do not allow to allocate arrays above certain
* length, and you will get the following exception if trying to do so:
*
* <p>java.lang.OutOfMemoryError: Requested array size exceeds VM limit
*
* <p>Therefore the maxSize of any stack is capped to this value. This max value is not arbitrary
* and was taken from OpenJDK.
*/
private static final int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8;

private T[] entries;

Expand All @@ -49,8 +68,9 @@ public class FlexStack<T> {
@SuppressWarnings("unchecked")
public FlexStack(final int maxSize, final Class<T> klass) {
checkArgument(maxSize > 0, "max size must be positive");
checkArgument(maxSize <= MAX_ARRAY_LENGTH, "max size is too large");

this.currentCapacity = Math.min(INCREMENT, maxSize);
this.currentCapacity = Math.min(INITIAL_SIZE, maxSize);
this.entries = (T[]) Array.newInstance(klass, currentCapacity);
this.maxSize = maxSize;
this.top = -1;
Expand Down Expand Up @@ -163,12 +183,21 @@ public void push(final T operand) {
throw new OverflowException();
}
if (nextTop >= currentCapacity) {
expandEntries(Math.min(currentCapacity + INCREMENT, maxSize));
final int newCapacity = newLength(currentCapacity, currentCapacity >> 1);
expandEntries(newCapacity);
}
entries[nextTop] = operand;
top = nextTop;
}

private int newLength(final int oldCapacity, final int prefGrowth) {
final int growth = Math.max(1, prefGrowth);
if (MAX_ARRAY_LENGTH - growth < oldCapacity) {
return maxSize;
}
return Math.min(oldCapacity + growth, maxSize);
}

/**
* Set operand.
*
Expand Down