*: use internal variables for simple big.NewInt() values

Avoid additional allocations.
This commit is contained in:
Roman Khimov 2021-11-30 21:10:48 +03:00
parent 3eed9d06f8
commit 418ba1dbc3
5 changed files with 31 additions and 15 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)
}

View file

@ -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: