From 6d415ae79a294a182444d40defadb047a3f07df3 Mon Sep 17 00:00:00 2001 From: "weixie.cui" Date: Sun, 4 Jan 2026 21:06:16 +0800 Subject: [PATCH 1/2] core/types: reduce alloc in hot code path --- core/types/transaction.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 6af960b8c3ea..534e68ab4988 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -317,11 +317,17 @@ 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()) + // BlobFeeCap is uint256.Int; ToBig allocates, but blob txs are rarer and + // this still removes several other allocations compared to the old code. + tmp.Mul(tmp, blobtx.BlobFeeCap.ToBig()) + total.Add(total, tmp) } - total.Add(total, tx.Value()) + total.Add(total, tx.inner.value()) return total } From e252f442e60279a25f3b33dcc3fab05b0ec0e066 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Fri, 6 Mar 2026 22:00:37 +0100 Subject: [PATCH 2/2] Apply suggestion from @s1na --- core/types/transaction.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 534e68ab4988..d513d3233cab 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -322,8 +322,6 @@ func (tx *Transaction) Cost() *big.Int { total.Mul(total, tx.inner.gasPrice()) if blobtx, ok := tx.inner.(*BlobTx); ok { tmp := new(big.Int).SetUint64(blobtx.blobGas()) - // BlobFeeCap is uint256.Int; ToBig allocates, but blob txs are rarer and - // this still removes several other allocations compared to the old code. tmp.Mul(tmp, blobtx.BlobFeeCap.ToBig()) total.Add(total, tmp) }