Skip to content

Commit bc013bc

Browse files
authored
all: prefer new(big.Int) over big.NewInt(0) (#25087)
minor performance improvement: `big.NewInt(0).Xxx` -> `new(big.Int).Xxx`
1 parent 8cfd121 commit bc013bc

File tree

12 files changed

+22
-24
lines changed

12 files changed

+22
-24
lines changed

accounts/abi/unpack.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
255255

256256
// lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type.
257257
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
258-
bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32])
258+
bigOffsetEnd := new(big.Int).SetBytes(output[index : index+32])
259259
bigOffsetEnd.Add(bigOffsetEnd, common.Big32)
260260
outputLength := big.NewInt(int64(len(output)))
261261

@@ -268,11 +268,9 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err
268268
}
269269

270270
offsetEnd := int(bigOffsetEnd.Uint64())
271-
lengthBig := big.NewInt(0).SetBytes(output[offsetEnd-32 : offsetEnd])
271+
lengthBig := new(big.Int).SetBytes(output[offsetEnd-32 : offsetEnd])
272272

273-
totalSize := big.NewInt(0)
274-
totalSize.Add(totalSize, bigOffsetEnd)
275-
totalSize.Add(totalSize, lengthBig)
273+
totalSize := new(big.Int).Add(bigOffsetEnd, lengthBig)
276274
if totalSize.BitLen() > 63 {
277275
return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize)
278276
}
@@ -287,7 +285,7 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err
287285

288286
// tuplePointsTo resolves the location reference for dynamic tuple.
289287
func tuplePointsTo(index int, output []byte) (start int, err error) {
290-
offset := big.NewInt(0).SetBytes(output[index : index+32])
288+
offset := new(big.Int).SetBytes(output[index : index+32])
291289
outputLen := big.NewInt(int64(len(output)))
292290

293291
if offset.Cmp(outputLen) > 0 {

accounts/abi/unpack_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func TestMultiReturnWithStringArray(t *testing.T) {
424424
}
425425
buff := new(bytes.Buffer)
426426
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
427-
temp, _ := big.NewInt(0).SetString("30000000000000000000", 10)
427+
temp, _ := new(big.Int).SetString("30000000000000000000", 10)
428428
ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
429429
ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
430430
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}

cmd/devp2p/internal/ethtest/chain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *Chain) Len() int {
4747
// TD calculates the total difficulty of the chain at the
4848
// chain head.
4949
func (c *Chain) TD() *big.Int {
50-
sum := big.NewInt(0)
50+
sum := new(big.Int)
5151
for _, block := range c.blocks[:c.Len()] {
5252
sum.Add(sum, block.Difficulty())
5353
}
@@ -57,7 +57,7 @@ func (c *Chain) TD() *big.Int {
5757
// TotalDifficultyAt calculates the total difficulty of the chain
5858
// at the given block height.
5959
func (c *Chain) TotalDifficultyAt(height int) *big.Int {
60-
sum := big.NewInt(0)
60+
sum := new(big.Int)
6161
if height >= c.Len() {
6262
return sum
6363
}

cmd/evm/internal/t8ntool/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
241241
minerReward.Add(minerReward, perOmmer)
242242
// Add (8-delta)/8
243243
reward := big.NewInt(8)
244-
reward.Sub(reward, big.NewInt(0).SetUint64(ommer.Delta))
244+
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
245245
reward.Mul(reward, blockReward)
246246
reward.Div(reward, big.NewInt(8))
247247
statedb.AddBalance(ommer.Address, reward)

consensus/ethash/consensus_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestDifficultyCalculators(t *testing.T) {
103103
for i := 0; i < 5000; i++ {
104104
// 1 to 300 seconds diff
105105
var timeDelta = uint64(1 + rand.Uint32()%3000)
106-
diffBig := big.NewInt(0).SetBytes(randSlice(2, 10))
106+
diffBig := new(big.Int).SetBytes(randSlice(2, 10))
107107
if diffBig.Cmp(params.MinimumDifficulty) < 0 {
108108
diffBig.Set(params.MinimumDifficulty)
109109
}

core/blockchain_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,7 @@ func BenchmarkBlockChain_1x1000ValueTransferToNonexisting(b *testing.B) {
27642764
numBlocks = 1
27652765
)
27662766
recipientFn := func(nonce uint64) common.Address {
2767-
return common.BigToAddress(big.NewInt(0).SetUint64(1337 + nonce))
2767+
return common.BigToAddress(new(big.Int).SetUint64(1337 + nonce))
27682768
}
27692769
dataFn := func(nonce uint64) []byte {
27702770
return nil
@@ -2781,7 +2781,7 @@ func BenchmarkBlockChain_1x1000ValueTransferToExisting(b *testing.B) {
27812781
b.ResetTimer()
27822782

27832783
recipientFn := func(nonce uint64) common.Address {
2784-
return common.BigToAddress(big.NewInt(0).SetUint64(1337))
2784+
return common.BigToAddress(new(big.Int).SetUint64(1337))
27852785
}
27862786
dataFn := func(nonce uint64) []byte {
27872787
return nil
@@ -2798,7 +2798,7 @@ func BenchmarkBlockChain_1x1000Executions(b *testing.B) {
27982798
b.ResetTimer()
27992799

28002800
recipientFn := func(nonce uint64) common.Address {
2801-
return common.BigToAddress(big.NewInt(0).SetUint64(0xc0de))
2801+
return common.BigToAddress(new(big.Int).SetUint64(0xc0de))
28022802
}
28032803
dataFn := func(nonce uint64) []byte {
28042804
return nil

internal/ethapi/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ type RPCTransaction struct {
12681268
// newRPCTransaction returns a transaction that will serialize to the RPC
12691269
// representation, with the given location metadata set (if available).
12701270
func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64, baseFee *big.Int, config *params.ChainConfig) *RPCTransaction {
1271-
signer := types.MakeSigner(config, big.NewInt(0).SetUint64(blockNumber))
1271+
signer := types.MakeSigner(config, new(big.Int).SetUint64(blockNumber))
12721272
from, _ := types.Sender(signer, tx)
12731273
v, r, s := tx.RawSignatureValues()
12741274
result := &RPCTransaction{

rlp/decode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ type ignoredField struct {
452452

453453
var (
454454
veryBigInt = new(big.Int).Add(
455-
big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
455+
new(big.Int).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
456456
big.NewInt(0xFFFF),
457457
)
458458
veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil)

rlp/encode_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ var encTests = []encTest{
119119
{val: big.NewInt(0xFFFFFFFFFFFF), output: "86FFFFFFFFFFFF"},
120120
{val: big.NewInt(0xFFFFFFFFFFFFFF), output: "87FFFFFFFFFFFFFF"},
121121
{
122-
val: big.NewInt(0).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")),
122+
val: new(big.Int).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")),
123123
output: "8F102030405060708090A0B0C0D0E0F2",
124124
},
125125
{
126-
val: big.NewInt(0).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")),
126+
val: new(big.Int).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")),
127127
output: "9C0100020003000400050006000700080009000A000B000C000D000E01",
128128
},
129129
{
130-
val: big.NewInt(0).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
130+
val: new(big.Int).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
131131
output: "A1010000000000000000000000000000000000000000000000000000000000000000",
132132
},
133133
{

signer/core/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (ui *headlessUi) ApproveTx(request *core.SignTxRequest) (core.SignTxRespons
6262
case "M": // modify
6363
// The headless UI always modifies the transaction
6464
old := big.Int(request.Transaction.Value)
65-
newVal := big.NewInt(0).Add(&old, big.NewInt(1))
65+
newVal := new(big.Int).Add(&old, big.NewInt(1))
6666
request.Transaction.Value = hexutil.Big(*newVal)
6767
return core.SignTxResponse{request.Transaction, true}, nil
6868
default:

0 commit comments

Comments
 (0)