Skip to content

Commit

Permalink
perf: remove unneded mem alloc in combine collectors (#1546) (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Staykov authored and hexoscott committed Jan 24, 2025
1 parent f7d1b0c commit 0128e80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion core/vm/zk_batch_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions core/vm/zk_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions core/vm/zk_transaction_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0128e80

Please sign in to comment.