core: optimize access to NEO cache

Do not copy RW cache each block. Instead we should get RO cache and
change it only if there's a necessity.
This commit is contained in:
Anna Shaleva 2022-04-27 18:14:42 +03:00
parent a6a0c1eb12
commit 335c1ee369

View file

@ -365,7 +365,7 @@ func (n *NEO) OnPersist(ic *interop.Context) error {
// PostPersist implements Contract interface. // PostPersist implements Contract interface.
func (n *NEO) PostPersist(ic *interop.Context) error { func (n *NEO) PostPersist(ic *interop.Context) error {
gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index) gas := n.GetGASPerBlock(ic.DAO, ic.Block.Index)
cache := ic.DAO.GetRWCache(n.ID).(*NeoCache) cache := ic.DAO.GetROCache(n.ID).(*NeoCache)
pubs := getCommitteeMembers(cache) pubs := getCommitteeMembers(cache)
committeeSize := n.cfg.GetCommitteeSize(ic.Block.Index) committeeSize := n.cfg.GetCommitteeSize(ic.Block.Index)
index := int(ic.Block.Index) % committeeSize index := int(ic.Block.Index) % committeeSize
@ -380,8 +380,11 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
voterReward.Div(voterReward, big.NewInt(int64(committeeSize+validatorsCount))) voterReward.Div(voterReward, big.NewInt(int64(committeeSize+validatorsCount)))
voterReward.Div(voterReward, big100) voterReward.Div(voterReward, big100)
var cs = cache.committee var (
var key = make([]byte, 38) cs = cache.committee
isCacheRW bool
key = make([]byte, 38)
)
for i := range cs { for i := range cs {
if cs[i].Votes.Sign() > 0 { if cs[i].Votes.Sign() > 0 {
var tmp = new(big.Int) var tmp = new(big.Int)
@ -405,6 +408,10 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
tmp.Add(tmp, r) tmp.Add(tmp, r)
binary.BigEndian.PutUint32(key[34:], ic.Block.Index+1) binary.BigEndian.PutUint32(key[34:], ic.Block.Index+1)
if !isCacheRW {
cache = ic.DAO.GetRWCache(n.ID).(*NeoCache)
isCacheRW = true
}
cache.gasPerVoteCache[cs[i].Key] = *tmp cache.gasPerVoteCache[cs[i].Key] = *tmp
ic.DAO.PutStorageItem(n.ID, key, bigint.ToBytes(tmp)) ic.DAO.PutStorageItem(n.ID, key, bigint.ToBytes(tmp))