mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-16 04:56:36 +00:00
core/types: reduce alloc in hot code path (#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>
This commit is contained in:
parent
0d043d071e
commit
e15d4ccc01
1 changed files with 8 additions and 4 deletions
|
|
@ -317,11 +317,15 @@ func (tx *Transaction) To() *common.Address {
|
||||||
|
|
||||||
// Cost returns (gas * gasPrice) + (blobGas * blobGasPrice) + value.
|
// Cost returns (gas * gasPrice) + (blobGas * blobGasPrice) + value.
|
||||||
func (tx *Transaction) Cost() *big.Int {
|
func (tx *Transaction) Cost() *big.Int {
|
||||||
total := new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))
|
// Avoid allocating copies via tx.GasPrice()/tx.Value(); use inner values directly.
|
||||||
if tx.Type() == BlobTxType {
|
total := new(big.Int).SetUint64(tx.inner.gas())
|
||||||
total.Add(total, new(big.Int).Mul(tx.BlobGasFeeCap(), new(big.Int).SetUint64(tx.BlobGas())))
|
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
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue