From 0128e805c5dfafa37078f7e00b5bfc8e465546b9 Mon Sep 17 00:00:00 2001 From: Valentin Staykov <79150443+V-Staykov@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:04:04 +0200 Subject: [PATCH] perf: remove unneded mem alloc in combine collectors (#1546) (#1547) --- core/vm/zk_batch_counters.go | 5 ++++- core/vm/zk_counters.go | 6 ++++++ core/vm/zk_transaction_counters.go | 12 ++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/vm/zk_batch_counters.go b/core/vm/zk_batch_counters.go index 228d6000335..ea5f708ef44 100644 --- a/core/vm/zk_batch_counters.go +++ b/core/vm/zk_batch_counters.go @@ -293,12 +293,15 @@ func (bcc *BatchCounterCollector) CombineCollectorsNoChanges() Counters { } } + txCounters := NewCounters() for _, tx := range bcc.transactions { - txCounters := tx.CombineCounters() + _ = tx.CombineCountersInto(&txCounters) for k, v := range txCounters { combined[k].used += v.used combined[k].remaining -= v.used } + + txCounters.NullateUsed() } return combined diff --git a/core/vm/zk_counters.go b/core/vm/zk_counters.go index 258a911b413..2096d1b72a1 100644 --- a/core/vm/zk_counters.go +++ b/core/vm/zk_counters.go @@ -83,6 +83,12 @@ func NewCountersFromUsedArray(used []int) *Counters { return &res } +func (c Counters) NullateUsed() { + for _, v := range c { + v.used = 0 + } +} + func (c Counters) UsedAsString() string { res := fmt.Sprintf("[%s: %v]", CounterKeyNames[SHA], c[SHA].used) res += fmt.Sprintf("[%s: %v]", CounterKeyNames[A], c[A].used) diff --git a/core/vm/zk_transaction_counters.go b/core/vm/zk_transaction_counters.go index 5ac7d228e9a..f6abee5f9f0 100644 --- a/core/vm/zk_transaction_counters.go +++ b/core/vm/zk_transaction_counters.go @@ -52,11 +52,19 @@ func NewTransactionCounter(transaction types.Transaction, smtMaxLevel int, forkI func (tc *TransactionCounter) CombineCounters() Counters { combined := NewCounters() + + _ = tc.CombineCountersInto(&combined) + + return combined +} + +func (tc *TransactionCounter) CombineCountersInto(combined *Counters) *Counters { for k := range tc.rlpCounters.counters { val := tc.rlpCounters.counters[k].used + tc.executionCounters.counters[k].used + tc.processingCounters.counters[k].used - combined[k] = &Counter{ - used: val, + if (*combined)[k] == nil { + (*combined)[k] = &Counter{used: 0} } + (*combined)[k].used = val } return combined