From 9ecb6c4ae644dfd07e55efdb2644a28125dd34dd Mon Sep 17 00:00:00 2001 From: cui Date: Tue, 24 Feb 2026 22:40:01 +0800 Subject: [PATCH] core: reduce alloc (#33576) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit inside tx.GasPrice()/GasFeeCap()/GasTipCap() already new a big.Int. bench result: ``` goos: darwin goarch: arm64 pkg: github.com/ethereum/go-ethereum/core cpu: Apple M4 │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ TransactionToMessage-10 240.1n ± 7% 175.1n ± 7% -27.09% (p=0.000 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ TransactionToMessage-10 544.0 ± 0% 424.0 ± 0% -22.06% (p=0.000 n=10) │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ TransactionToMessage-10 17.00 ± 0% 11.00 ± 0% -35.29% (p=0.000 n=10) ``` benchmark code: ``` // Copyright 2025 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . package core import ( "math/big" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/params" ) // BenchmarkTransactionToMessage benchmarks the TransactionToMessage function. func BenchmarkTransactionToMessage(b *testing.B) { key, _ := crypto.GenerateKey() signer := types.LatestSigner(params.TestChainConfig) to := common.HexToAddress("0x000000000000000000000000000000000000dead") // Create a DynamicFeeTx transaction txdata := &types.DynamicFeeTx{ ChainID: big.NewInt(1), Nonce: 42, GasTipCap: big.NewInt(1000000000), // 1 gwei GasFeeCap: big.NewInt(2000000000), // 2 gwei Gas: 21000, To: &to, Value: big.NewInt(1000000000000000000), // 1 ether Data: []byte{0x12, 0x34, 0x56, 0x78}, AccessList: types.AccessList{ types.AccessTuple{ Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{ common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"), }, }, }, } tx, _ := types.SignNewTx(key, signer, txdata) baseFee := big.NewInt(1500000000) // 1.5 gwei b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { _, err := TransactionToMessage(tx, signer, baseFee) if err != nil { b.Fatal(err) } } } l ``` --- core/state_transition.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index bf5ac07636..62474b5f5b 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -177,9 +177,9 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In msg := &Message{ Nonce: tx.Nonce(), GasLimit: tx.Gas(), - GasPrice: new(big.Int).Set(tx.GasPrice()), - GasFeeCap: new(big.Int).Set(tx.GasFeeCap()), - GasTipCap: new(big.Int).Set(tx.GasTipCap()), + GasPrice: tx.GasPrice(), + GasFeeCap: tx.GasFeeCap(), + GasTipCap: tx.GasTipCap(), To: tx.To(), Value: tx.Value(), Data: tx.Data(),