all: prefer new(big.Int) over big.NewInt(0) (#25087)

minor performance improvement: `big.NewInt(0).Xxx` -> `new(big.Int).Xxx`
This commit is contained in:
lmittmann 2022-06-14 14:09:48 +02:00 committed by GitHub
parent 8cfd1214b7
commit bc013bc42e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 22 additions and 24 deletions

View File

@ -255,7 +255,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
// lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type. // lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type.
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) { func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32]) bigOffsetEnd := new(big.Int).SetBytes(output[index : index+32])
bigOffsetEnd.Add(bigOffsetEnd, common.Big32) bigOffsetEnd.Add(bigOffsetEnd, common.Big32)
outputLength := big.NewInt(int64(len(output))) outputLength := big.NewInt(int64(len(output)))
@ -268,11 +268,9 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err
} }
offsetEnd := int(bigOffsetEnd.Uint64()) offsetEnd := int(bigOffsetEnd.Uint64())
lengthBig := big.NewInt(0).SetBytes(output[offsetEnd-32 : offsetEnd]) lengthBig := new(big.Int).SetBytes(output[offsetEnd-32 : offsetEnd])
totalSize := big.NewInt(0) totalSize := new(big.Int).Add(bigOffsetEnd, lengthBig)
totalSize.Add(totalSize, bigOffsetEnd)
totalSize.Add(totalSize, lengthBig)
if totalSize.BitLen() > 63 { if totalSize.BitLen() > 63 {
return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize) return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize)
} }
@ -287,7 +285,7 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err
// tuplePointsTo resolves the location reference for dynamic tuple. // tuplePointsTo resolves the location reference for dynamic tuple.
func tuplePointsTo(index int, output []byte) (start int, err error) { func tuplePointsTo(index int, output []byte) (start int, err error) {
offset := big.NewInt(0).SetBytes(output[index : index+32]) offset := new(big.Int).SetBytes(output[index : index+32])
outputLen := big.NewInt(int64(len(output))) outputLen := big.NewInt(int64(len(output)))
if offset.Cmp(outputLen) > 0 { if offset.Cmp(outputLen) > 0 {

View File

@ -424,7 +424,7 @@ func TestMultiReturnWithStringArray(t *testing.T) {
} }
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
temp, _ := big.NewInt(0).SetString("30000000000000000000", 10) temp, _ := new(big.Int).SetString("30000000000000000000", 10)
ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp} ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f") ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"} ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}

View File

@ -47,7 +47,7 @@ func (c *Chain) Len() int {
// TD calculates the total difficulty of the chain at the // TD calculates the total difficulty of the chain at the
// chain head. // chain head.
func (c *Chain) TD() *big.Int { func (c *Chain) TD() *big.Int {
sum := big.NewInt(0) sum := new(big.Int)
for _, block := range c.blocks[:c.Len()] { for _, block := range c.blocks[:c.Len()] {
sum.Add(sum, block.Difficulty()) sum.Add(sum, block.Difficulty())
} }
@ -57,7 +57,7 @@ func (c *Chain) TD() *big.Int {
// TotalDifficultyAt calculates the total difficulty of the chain // TotalDifficultyAt calculates the total difficulty of the chain
// at the given block height. // at the given block height.
func (c *Chain) TotalDifficultyAt(height int) *big.Int { func (c *Chain) TotalDifficultyAt(height int) *big.Int {
sum := big.NewInt(0) sum := new(big.Int)
if height >= c.Len() { if height >= c.Len() {
return sum return sum
} }

View File

@ -241,7 +241,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
minerReward.Add(minerReward, perOmmer) minerReward.Add(minerReward, perOmmer)
// Add (8-delta)/8 // Add (8-delta)/8
reward := big.NewInt(8) reward := big.NewInt(8)
reward.Sub(reward, big.NewInt(0).SetUint64(ommer.Delta)) reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
reward.Mul(reward, blockReward) reward.Mul(reward, blockReward)
reward.Div(reward, big.NewInt(8)) reward.Div(reward, big.NewInt(8))
statedb.AddBalance(ommer.Address, reward) statedb.AddBalance(ommer.Address, reward)

View File

@ -103,7 +103,7 @@ func TestDifficultyCalculators(t *testing.T) {
for i := 0; i < 5000; i++ { for i := 0; i < 5000; i++ {
// 1 to 300 seconds diff // 1 to 300 seconds diff
var timeDelta = uint64(1 + rand.Uint32()%3000) var timeDelta = uint64(1 + rand.Uint32()%3000)
diffBig := big.NewInt(0).SetBytes(randSlice(2, 10)) diffBig := new(big.Int).SetBytes(randSlice(2, 10))
if diffBig.Cmp(params.MinimumDifficulty) < 0 { if diffBig.Cmp(params.MinimumDifficulty) < 0 {
diffBig.Set(params.MinimumDifficulty) diffBig.Set(params.MinimumDifficulty)
} }

View File

@ -2764,7 +2764,7 @@ func BenchmarkBlockChain_1x1000ValueTransferToNonexisting(b *testing.B) {
numBlocks = 1 numBlocks = 1
) )
recipientFn := func(nonce uint64) common.Address { recipientFn := func(nonce uint64) common.Address {
return common.BigToAddress(big.NewInt(0).SetUint64(1337 + nonce)) return common.BigToAddress(new(big.Int).SetUint64(1337 + nonce))
} }
dataFn := func(nonce uint64) []byte { dataFn := func(nonce uint64) []byte {
return nil return nil
@ -2781,7 +2781,7 @@ func BenchmarkBlockChain_1x1000ValueTransferToExisting(b *testing.B) {
b.ResetTimer() b.ResetTimer()
recipientFn := func(nonce uint64) common.Address { recipientFn := func(nonce uint64) common.Address {
return common.BigToAddress(big.NewInt(0).SetUint64(1337)) return common.BigToAddress(new(big.Int).SetUint64(1337))
} }
dataFn := func(nonce uint64) []byte { dataFn := func(nonce uint64) []byte {
return nil return nil
@ -2798,7 +2798,7 @@ func BenchmarkBlockChain_1x1000Executions(b *testing.B) {
b.ResetTimer() b.ResetTimer()
recipientFn := func(nonce uint64) common.Address { recipientFn := func(nonce uint64) common.Address {
return common.BigToAddress(big.NewInt(0).SetUint64(0xc0de)) return common.BigToAddress(new(big.Int).SetUint64(0xc0de))
} }
dataFn := func(nonce uint64) []byte { dataFn := func(nonce uint64) []byte {
return nil return nil

View File

@ -1268,7 +1268,7 @@ type RPCTransaction struct {
// newRPCTransaction returns a transaction that will serialize to the RPC // newRPCTransaction returns a transaction that will serialize to the RPC
// representation, with the given location metadata set (if available). // representation, with the given location metadata set (if available).
func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64, baseFee *big.Int, config *params.ChainConfig) *RPCTransaction { func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64, baseFee *big.Int, config *params.ChainConfig) *RPCTransaction {
signer := types.MakeSigner(config, big.NewInt(0).SetUint64(blockNumber)) signer := types.MakeSigner(config, new(big.Int).SetUint64(blockNumber))
from, _ := types.Sender(signer, tx) from, _ := types.Sender(signer, tx)
v, r, s := tx.RawSignatureValues() v, r, s := tx.RawSignatureValues()
result := &RPCTransaction{ result := &RPCTransaction{

View File

@ -452,7 +452,7 @@ type ignoredField struct {
var ( var (
veryBigInt = new(big.Int).Add( veryBigInt = new(big.Int).Add(
big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16), new(big.Int).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
big.NewInt(0xFFFF), big.NewInt(0xFFFF),
) )
veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil) veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil)

View File

@ -119,15 +119,15 @@ var encTests = []encTest{
{val: big.NewInt(0xFFFFFFFFFFFF), output: "86FFFFFFFFFFFF"}, {val: big.NewInt(0xFFFFFFFFFFFF), output: "86FFFFFFFFFFFF"},
{val: big.NewInt(0xFFFFFFFFFFFFFF), output: "87FFFFFFFFFFFFFF"}, {val: big.NewInt(0xFFFFFFFFFFFFFF), output: "87FFFFFFFFFFFFFF"},
{ {
val: big.NewInt(0).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")), val: new(big.Int).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")),
output: "8F102030405060708090A0B0C0D0E0F2", output: "8F102030405060708090A0B0C0D0E0F2",
}, },
{ {
val: big.NewInt(0).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")), val: new(big.Int).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")),
output: "9C0100020003000400050006000700080009000A000B000C000D000E01", output: "9C0100020003000400050006000700080009000A000B000C000D000E01",
}, },
{ {
val: big.NewInt(0).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")), val: new(big.Int).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
output: "A1010000000000000000000000000000000000000000000000000000000000000000", output: "A1010000000000000000000000000000000000000000000000000000000000000000",
}, },
{ {

View File

@ -62,7 +62,7 @@ func (ui *headlessUi) ApproveTx(request *core.SignTxRequest) (core.SignTxRespons
case "M": // modify case "M": // modify
// The headless UI always modifies the transaction // The headless UI always modifies the transaction
old := big.Int(request.Transaction.Value) old := big.Int(request.Transaction.Value)
newVal := big.NewInt(0).Add(&old, big.NewInt(1)) newVal := new(big.Int).Add(&old, big.NewInt(1))
request.Transaction.Value = hexutil.Big(*newVal) request.Transaction.Value = hexutil.Big(*newVal)
return core.SignTxResponse{request.Transaction, true}, nil return core.SignTxResponse{request.Transaction, true}, nil
default: default:

View File

@ -29,11 +29,11 @@ func mixAddr(a string) (*common.MixedcaseAddress, error) {
return common.NewMixedcaseAddressFromString(a) return common.NewMixedcaseAddressFromString(a)
} }
func toHexBig(h string) hexutil.Big { func toHexBig(h string) hexutil.Big {
b := big.NewInt(0).SetBytes(common.FromHex(h)) b := new(big.Int).SetBytes(common.FromHex(h))
return hexutil.Big(*b) return hexutil.Big(*b)
} }
func toHexUint(h string) hexutil.Uint64 { func toHexUint(h string) hexutil.Uint64 {
b := big.NewInt(0).SetBytes(common.FromHex(h)) b := new(big.Int).SetBytes(common.FromHex(h))
return hexutil.Uint64(b.Uint64()) return hexutil.Uint64(b.Uint64())
} }
func dummyTxArgs(t txtestcase) *apitypes.SendTxArgs { func dummyTxArgs(t txtestcase) *apitypes.SendTxArgs {

View File

@ -449,7 +449,7 @@ func dummyTx(value hexutil.Big) *core.SignTxRequest {
} }
func dummyTxWithV(value uint64) *core.SignTxRequest { func dummyTxWithV(value uint64) *core.SignTxRequest {
v := big.NewInt(0).SetUint64(value) v := new(big.Int).SetUint64(value)
h := hexutil.Big(*v) h := hexutil.Big(*v)
return dummyTx(h) return dummyTx(h)
} }
@ -469,7 +469,7 @@ func TestLimitWindow(t *testing.T) {
return return
} }
// 0.3 ether: 429D069189E0000 wei // 0.3 ether: 429D069189E0000 wei
v := big.NewInt(0).SetBytes(common.Hex2Bytes("0429D069189E0000")) v := new(big.Int).SetBytes(common.Hex2Bytes("0429D069189E0000"))
h := hexutil.Big(*v) h := hexutil.Big(*v)
// The first three should succeed // The first three should succeed
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {