Skip to content

Commit

Permalink
perf: parallelize refundedGas for big transactions (#793)
Browse files Browse the repository at this point in the history
Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>
  • Loading branch information
ahamlat authored Jul 19, 2024
1 parent dcb6af3 commit 8f6295a
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class TxTrace implements PostTransactionDefer {
@Getter private long leftoverGas = -1;
@Getter private long gasRefundFinalCounter = 0; // TODO:

private static final int PARALLELIZATION_THRESHOLD = 10_000;

public int size() {
return this.trace.size();
}
Expand Down Expand Up @@ -72,16 +74,22 @@ public void add(TraceSection section) {
}

public long refundedGas() {
if (this.refundedGas == -1) {
this.refundedGas = 0;
for (TraceSection section : this.trace) {
if (!section.hasReverted()) {
this.refundedGas += section.refundDelta();
if (this.trace.size() >= PARALLELIZATION_THRESHOLD) {
return this.trace.parallelStream()
.filter(section -> !section.hasReverted())
.mapToLong(TraceSection::refundDelta)
.sum();
} else {
if (this.refundedGas == -1) {
this.refundedGas = 0;
for (TraceSection section : this.trace) {
if (!section.hasReverted()) {
this.refundedGas += section.refundDelta();
}
}
}
return this.refundedGas;
}

return this.refundedGas;
}

@Override
Expand Down

0 comments on commit 8f6295a

Please sign in to comment.