core: avoid unnecessary NEO cached values copying

This commit is contained in:
Anna Shaleva 2022-04-28 18:38:13 +03:00
parent 335c1ee369
commit 9cc41528ef

View file

@ -118,11 +118,11 @@ func (c *NeoCache) Copy() dao.NativeContractCache {
func copyNeoCache(src, dst *NeoCache) { func copyNeoCache(src, dst *NeoCache) {
dst.votesChanged = src.votesChanged dst.votesChanged = src.votesChanged
dst.nextValidators = src.nextValidators.Copy() // Can safely omit copying because the new array is created each time
dst.validators = src.validators.Copy() // validators list, nextValidators and committee are updated.
dst.nextValidators = src.nextValidators
dst.committee = make(keysWithVotes, len(src.committee)) dst.validators = src.validators
copy(dst.committee, src.committee) dst.committee = src.committee
dst.committeeHash = src.committeeHash dst.committeeHash = src.committeeHash
dst.registerPrice = src.registerPrice dst.registerPrice = src.registerPrice
@ -941,10 +941,13 @@ func (n *NEO) getAccountState(ic *interop.Context, args []stackitem.Item) stacki
// ComputeNextBlockValidators returns an actual list of current validators. // ComputeNextBlockValidators returns an actual list of current validators.
func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d *dao.Simple) (keys.PublicKeys, error) { func (n *NEO) ComputeNextBlockValidators(bc interop.Ledger, d *dao.Simple) (keys.PublicKeys, error) {
numOfCNs := n.cfg.GetNumOfCNs(bc.BlockHeight() + 1) numOfCNs := n.cfg.GetNumOfCNs(bc.BlockHeight() + 1)
cache := d.GetRWCache(n.ID).(*NeoCache) // Most of the time it should be OK with RO cache, thus try to retrieve
// validators without RW cache creation to avoid cached values copying.
cache := d.GetROCache(n.ID).(*NeoCache)
if vals := cache.validators; vals != nil && numOfCNs == len(vals) { if vals := cache.validators; vals != nil && numOfCNs == len(vals) {
return vals.Copy(), nil return vals.Copy(), nil
} }
cache = d.GetRWCache(n.ID).(*NeoCache)
result, _, err := n.computeCommitteeMembers(bc, d) result, _, err := n.computeCommitteeMembers(bc, d)
if err != nil { if err != nil {
return nil, err return nil, err