From e15d4ccc0195d0725926614c2fec4396976f259e Mon Sep 17 00:00:00 2001 From: cui Date: Sat, 7 Mar 2026 21:31:36 +0800 Subject: [PATCH] core/types: reduce alloc in hot code path (#33523) Reduce allocations in calculation of tx cost. --------- Co-authored-by: weixie.cui Co-authored-by: Sina M <1591639+s1na@users.noreply.github.com> --- core/types/transaction.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 21f858ecfa..e9bf08daef 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -317,11 +317,15 @@ func (tx *Transaction) To() *common.Address { // Cost returns (gas * gasPrice) + (blobGas * blobGasPrice) + value. func (tx *Transaction) Cost() *big.Int { - total := new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())) - if tx.Type() == BlobTxType { - total.Add(total, new(big.Int).Mul(tx.BlobGasFeeCap(), new(big.Int).SetUint64(tx.BlobGas()))) + // Avoid allocating copies via tx.GasPrice()/tx.Value(); use inner values directly. + total := new(big.Int).SetUint64(tx.inner.gas()) + total.Mul(total, tx.inner.gasPrice()) + if blobtx, ok := tx.inner.(*BlobTx); ok { + tmp := new(big.Int).SetUint64(blobtx.blobGas()) + tmp.Mul(tmp, blobtx.BlobFeeCap.ToBig()) + total.Add(total, tmp) } - total.Add(total, tx.Value()) + total.Add(total, tx.inner.value()) return total }