Skip to content

Commit

Permalink
all: prefer new(big.Int) over big.NewInt(0) (ethereum#25087)
Browse files Browse the repository at this point in the history
minor performance improvement: `big.NewInt(0).Xxx` -> `new(big.Int).Xxx`
  • Loading branch information
lmittmann authored Jun 14, 2022
1 parent 8cfd121 commit bc013bc
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 24 deletions.
10 changes: 4 additions & 6 deletions accounts/abi/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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)
outputLength := big.NewInt(int64(len(output)))

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

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.Add(totalSize, bigOffsetEnd)
totalSize.Add(totalSize, lengthBig)
totalSize := new(big.Int).Add(bigOffsetEnd, lengthBig)
if totalSize.BitLen() > 63 {
return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize)
}
Expand All @@ -287,7 +285,7 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err

// tuplePointsTo resolves the location reference for dynamic tuple.
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)))

if offset.Cmp(outputLen) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestMultiReturnWithStringArray(t *testing.T) {
}
buff := new(bytes.Buffer)
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}
ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
Expand Down
4 changes: 2 additions & 2 deletions cmd/devp2p/internal/ethtest/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *Chain) Len() int {
// TD calculates the total difficulty of the chain at the
// chain head.
func (c *Chain) TD() *big.Int {
sum := big.NewInt(0)
sum := new(big.Int)
for _, block := range c.blocks[:c.Len()] {
sum.Add(sum, block.Difficulty())
}
Expand All @@ -57,7 +57,7 @@ func (c *Chain) TD() *big.Int {
// TotalDifficultyAt calculates the total difficulty of the chain
// at the given block height.
func (c *Chain) TotalDifficultyAt(height int) *big.Int {
sum := big.NewInt(0)
sum := new(big.Int)
if height >= c.Len() {
return sum
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
minerReward.Add(minerReward, perOmmer)
// Add (8-delta)/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.Div(reward, big.NewInt(8))
statedb.AddBalance(ommer.Address, reward)
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestDifficultyCalculators(t *testing.T) {
for i := 0; i < 5000; i++ {
// 1 to 300 seconds diff
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 {
diffBig.Set(params.MinimumDifficulty)
}
Expand Down
6 changes: 3 additions & 3 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,7 @@ func BenchmarkBlockChain_1x1000ValueTransferToNonexisting(b *testing.B) {
numBlocks = 1
)
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 {
return nil
Expand All @@ -2781,7 +2781,7 @@ func BenchmarkBlockChain_1x1000ValueTransferToExisting(b *testing.B) {
b.ResetTimer()

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 {
return nil
Expand All @@ -2798,7 +2798,7 @@ func BenchmarkBlockChain_1x1000Executions(b *testing.B) {
b.ResetTimer()

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 {
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ type RPCTransaction struct {
// newRPCTransaction returns a transaction that will serialize to the RPC
// 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 {
signer := types.MakeSigner(config, big.NewInt(0).SetUint64(blockNumber))
signer := types.MakeSigner(config, new(big.Int).SetUint64(blockNumber))
from, _ := types.Sender(signer, tx)
v, r, s := tx.RawSignatureValues()
result := &RPCTransaction{
Expand Down
2 changes: 1 addition & 1 deletion rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ type ignoredField struct {

var (
veryBigInt = new(big.Int).Add(
big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
new(big.Int).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
big.NewInt(0xFFFF),
)
veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil)
Expand Down
6 changes: 3 additions & 3 deletions rlp/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ var encTests = []encTest{
{val: big.NewInt(0xFFFFFFFFFFFF), output: "86FFFFFFFFFFFF"},
{val: big.NewInt(0xFFFFFFFFFFFFFF), output: "87FFFFFFFFFFFFFF"},
{
val: big.NewInt(0).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")),
val: new(big.Int).SetBytes(unhex("102030405060708090A0B0C0D0E0F2")),
output: "8F102030405060708090A0B0C0D0E0F2",
},
{
val: big.NewInt(0).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")),
val: new(big.Int).SetBytes(unhex("0100020003000400050006000700080009000A000B000C000D000E01")),
output: "9C0100020003000400050006000700080009000A000B000C000D000E01",
},
{
val: big.NewInt(0).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
val: new(big.Int).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
output: "A1010000000000000000000000000000000000000000000000000000000000000000",
},
{
Expand Down
2 changes: 1 addition & 1 deletion signer/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (ui *headlessUi) ApproveTx(request *core.SignTxRequest) (core.SignTxRespons
case "M": // modify
// The headless UI always modifies the transaction
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)
return core.SignTxResponse{request.Transaction, true}, nil
default:
Expand Down
4 changes: 2 additions & 2 deletions signer/fourbyte/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func mixAddr(a string) (*common.MixedcaseAddress, error) {
return common.NewMixedcaseAddressFromString(a)
}
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)
}
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())
}
func dummyTxArgs(t txtestcase) *apitypes.SendTxArgs {
Expand Down
4 changes: 2 additions & 2 deletions signer/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func dummyTx(value hexutil.Big) *core.SignTxRequest {
}

func dummyTxWithV(value uint64) *core.SignTxRequest {
v := big.NewInt(0).SetUint64(value)
v := new(big.Int).SetUint64(value)
h := hexutil.Big(*v)
return dummyTx(h)
}
Expand All @@ -469,7 +469,7 @@ func TestLimitWindow(t *testing.T) {
return
}
// 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)
// The first three should succeed
for i := 0; i < 3; i++ {
Expand Down

0 comments on commit bc013bc

Please sign in to comment.