Skip to content

Commit e15d4cc

Browse files
cuiweixieweixie.cuis1na
authored
core/types: reduce alloc in hot code path (ethereum#33523)
Reduce allocations in calculation of tx cost. --------- Co-authored-by: weixie.cui <weixie.cui@okg.com> Co-authored-by: Sina M <1591639+s1na@users.noreply.github.com>
1 parent 0d043d0 commit e15d4cc

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

core/types/transaction.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,15 @@ func (tx *Transaction) To() *common.Address {
317317

318318
// Cost returns (gas * gasPrice) + (blobGas * blobGasPrice) + value.
319319
func (tx *Transaction) Cost() *big.Int {
320-
total := new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))
321-
if tx.Type() == BlobTxType {
322-
total.Add(total, new(big.Int).Mul(tx.BlobGasFeeCap(), new(big.Int).SetUint64(tx.BlobGas())))
320+
// Avoid allocating copies via tx.GasPrice()/tx.Value(); use inner values directly.
321+
total := new(big.Int).SetUint64(tx.inner.gas())
322+
total.Mul(total, tx.inner.gasPrice())
323+
if blobtx, ok := tx.inner.(*BlobTx); ok {
324+
tmp := new(big.Int).SetUint64(blobtx.blobGas())
325+
tmp.Mul(tmp, blobtx.BlobFeeCap.ToBig())
326+
total.Add(total, tmp)
323327
}
324-
total.Add(total, tx.Value())
328+
total.Add(total, tx.inner.value())
325329
return total
326330
}
327331

0 commit comments

Comments
 (0)