From 8f6295ab58bb24b643c51b4fbfa8ab261509ef5c Mon Sep 17 00:00:00 2001 From: ahamlat Date: Fri, 19 Jul 2024 11:48:56 +0200 Subject: [PATCH] perf: parallelize refundedGas for big transactions (#793) Signed-off-by: Ameziane H --- .../linea/zktracer/module/hub/TxTrace.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java index 65f541db29..b4b313023b 100644 --- a/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java +++ b/arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/TxTrace.java @@ -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(); } @@ -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