From 418ba1dbc339a6a4ab69e47063de533042f6d4b1 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 30 Nov 2021 21:10:48 +0300 Subject: [PATCH] *: use internal variables for simple big.NewInt() values Avoid additional allocations. --- pkg/core/native/native_neo.go | 24 ++++++++++++++++-------- pkg/core/native/util.go | 1 + pkg/crypto/keys/publickey.go | 9 ++++++--- pkg/encoding/bigint/bigint.go | 6 ++++-- pkg/vm/vm.go | 6 ++++-- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/pkg/core/native/native_neo.go b/pkg/core/native/native_neo.go index 53604bca3..eb5291d87 100644 --- a/pkg/core/native/native_neo.go +++ b/pkg/core/native/native_neo.go @@ -90,6 +90,12 @@ const ( var ( // prefixCommittee is a key used to store committee. prefixCommittee = []byte{14} + + bigCommitteeRewardRatio = big.NewInt(committeeRewardRatio) + bigVoterRewardRatio = big.NewInt(voterRewardRatio) + bigVoterRewardFactor = big.NewInt(voterRewardFactor) + bigEffectiveVoterTurnout = big.NewInt(effectiveVoterTurnout) + big100 = big.NewInt(100) ) // makeValidatorKey creates a key from account script hash. @@ -316,24 +322,26 @@ func (n *NEO) PostPersist(ic *interop.Context) error { pubs := n.GetCommitteeMembers() committeeSize := len(ic.Chain.GetConfig().StandbyCommittee) index := int(ic.Block.Index) % committeeSize - committeeReward := new(big.Int).Mul(gas, big.NewInt(committeeRewardRatio)) - n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big.NewInt(100)), false) + committeeReward := new(big.Int).Mul(gas, bigCommitteeRewardRatio) + n.GAS.mint(ic, pubs[index].GetScriptHash(), committeeReward.Div(committeeReward, big100), false) if ShouldUpdateCommittee(ic.Block.Index, ic.Chain) { - var voterReward = big.NewInt(voterRewardRatio) + var voterReward = new(big.Int).Set(bigVoterRewardRatio) voterReward.Mul(voterReward, gas) voterReward.Mul(voterReward, big.NewInt(voterRewardFactor*int64(committeeSize))) var validatorsCount = ic.Chain.GetConfig().ValidatorsCount voterReward.Div(voterReward, big.NewInt(int64(committeeSize+validatorsCount))) - voterReward.Div(voterReward, big.NewInt(100)) + voterReward.Div(voterReward, big100) var cs = n.committee.Load().(keysWithVotes) var key = make([]byte, 38) for i := range cs { if cs[i].Votes.Sign() > 0 { - tmp := big.NewInt(1) + var tmp = new(big.Int) if i < validatorsCount { - tmp = big.NewInt(2) + tmp.Set(intTwo) + } else { + tmp.Set(intOne) } tmp.Mul(tmp, voterReward) tmp.Div(tmp, cs[i].Votes) @@ -633,7 +641,7 @@ func (n *NEO) calculateBonus(d dao.DAO, vote *keys.PublicKey, value *big.Int, st var reward = n.getGASPerVote(d, key, start, end) var tmp = new(big.Int).Sub(&reward[1], &reward[0]) tmp.Mul(tmp, value) - tmp.Div(tmp, big.NewInt(voterRewardFactor)) + tmp.Div(tmp, bigVoterRewardFactor) tmp.Add(tmp, r) return tmp, nil } @@ -982,7 +990,7 @@ func (n *NEO) computeCommitteeMembers(bc blockchainer.Blockchainer, d dao.DAO) ( } votersCount := bigint.FromBytes(si) // votersCount / totalSupply must be >= 0.2 - votersCount.Mul(votersCount, big.NewInt(effectiveVoterTurnout)) + votersCount.Mul(votersCount, bigEffectiveVoterTurnout) _, totalSupply := n.getTotalSupply(d) voterTurnout := votersCount.Div(votersCount, totalSupply) diff --git a/pkg/core/native/util.go b/pkg/core/native/util.go index afb20728c..0fadf740d 100644 --- a/pkg/core/native/util.go +++ b/pkg/core/native/util.go @@ -13,6 +13,7 @@ import ( ) var intOne = big.NewInt(1) +var intTwo = big.NewInt(2) func getConvertibleFromDAO(id int32, d dao.DAO, key []byte, conv stackitem.Convertible) error { si := d.GetStorageItem(id, key) diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index f39a15b1d..ecbeee259 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -29,6 +29,9 @@ const SignatureLen = 64 // PublicKeys is a list of public keys. type PublicKeys []*PublicKey +var big0 = big.NewInt(0) +var big3 = big.NewInt(3) + func (keys PublicKeys) Len() int { return len(keys) } func (keys PublicKeys) Swap(i, j int) { keys[i], keys[j] = keys[j], keys[i] } func (keys PublicKeys) Less(i, j int) bool { @@ -189,12 +192,12 @@ func decodeCompressedY(x *big.Int, ylsb uint, curve elliptic.Curve) (*big.Int, e var a *big.Int switch curve.(type) { case *btcec.KoblitzCurve: - a = big.NewInt(0) + a = big0 default: - a = big.NewInt(3) + a = big3 } cp := curve.Params() - xCubed := new(big.Int).Exp(x, big.NewInt(3), cp.P) + xCubed := new(big.Int).Exp(x, big3, cp.P) aX := new(big.Int).Mul(x, a) aX.Mod(aX, cp.P) ySquared := new(big.Int).Sub(xCubed, aX) diff --git a/pkg/encoding/bigint/bigint.go b/pkg/encoding/bigint/bigint.go index b2ab067b0..a6b80b70a 100644 --- a/pkg/encoding/bigint/bigint.go +++ b/pkg/encoding/bigint/bigint.go @@ -15,6 +15,8 @@ const ( wordSizeBytes = bits.UintSize / 8 ) +var bigOne = big.NewInt(1) + // FromBytesUnsigned converts data in little-endian format to an unsigned integer. func FromBytesUnsigned(data []byte) *big.Int { bs := slice.CopyReverse(data) @@ -70,7 +72,7 @@ func FromBytes(data []byte) *big.Int { n.SetBits(ws) n.Neg(n) - return n.Sub(n, big.NewInt(1)) + return n.Sub(n, bigOne) } return n.SetBits(ws) @@ -114,7 +116,7 @@ func ToPreallocatedBytes(n *big.Int, data []byte) []byte { if sign == 1 { ws = n.Bits() } else { - n1 := new(big.Int).Add(n, big.NewInt(1)) + n1 := new(big.Int).Add(n, bigOne) if n1.Sign() == 0 { // n == -1 return append(data, 0xFF) } diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 07c6739cc..cfa037501 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -90,6 +90,8 @@ type VM struct { invTree *InvocationTree } +var bigOne = big.NewInt(1) + // New returns a new VM object ready to load AVM bytecode scripts. func New() *VM { return NewWithTrigger(trigger.Application) @@ -889,12 +891,12 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro case opcode.INC: x := v.estack.Pop().BigInt() - a := new(big.Int).Add(x, big.NewInt(1)) + a := new(big.Int).Add(x, bigOne) v.estack.PushItem(stackitem.NewBigInteger(a)) case opcode.DEC: x := v.estack.Pop().BigInt() - a := new(big.Int).Sub(x, big.NewInt(1)) + a := new(big.Int).Sub(x, bigOne) v.estack.PushItem(stackitem.NewBigInteger(a)) case opcode.ADD: