Merge pull request #2531 from nspcc-dev/perf

Minor performance improvements
This commit is contained in:
Roman Khimov 2022-06-03 10:40:56 +03:00 committed by GitHub
commit edb6ca8926
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 198 additions and 99 deletions

View file

@ -601,8 +601,7 @@ func (m *Management) getNextContractID(d *dao.Simple) (int32, error) {
id := bigint.FromBytes(si)
ret := int32(id.Int64())
id.Add(id, intOne)
si = bigint.ToPreallocatedBytes(id, si)
d.PutStorageItem(m.ID, keyNextAvailableID, si)
d.PutBigInt(m.ID, keyNextAvailableID, id)
return ret, nil
}

View file

@ -281,7 +281,7 @@ func (n *NEO) Initialize(ic *interop.Context) error {
return err
}
ic.DAO.PutStorageItem(n.ID, prefixCommittee, cvs.Bytes())
ic.DAO.PutStorageItem(n.ID, prefixCommittee, cvs.Bytes(ic.DAO.GetItemCtx()))
h, err := getStandbyValidatorsHash(ic)
if err != nil {
@ -355,7 +355,7 @@ func (n *NEO) updateCache(cache *NeoCache, cvs keysWithVotes, blockHeight uint32
func (n *NEO) updateCommittee(cache *NeoCache, ic *interop.Context) error {
if !cache.votesChanged {
// We need to put in storage anyway, as it affects dumps
ic.DAO.PutStorageItem(n.ID, prefixCommittee, cache.committee.Bytes())
ic.DAO.PutStorageItem(n.ID, prefixCommittee, cache.committee.Bytes(ic.DAO.GetItemCtx()))
return nil
}
@ -367,7 +367,7 @@ func (n *NEO) updateCommittee(cache *NeoCache, ic *interop.Context) error {
return err
}
cache.votesChanged = false
ic.DAO.PutStorageItem(n.ID, prefixCommittee, cvs.Bytes())
ic.DAO.PutStorageItem(n.ID, prefixCommittee, cvs.Bytes(ic.DAO.GetItemCtx()))
return nil
}
@ -440,7 +440,7 @@ func (n *NEO) PostPersist(ic *interop.Context) error {
}
cache.gasPerVoteCache[cs[i].Key] = *tmp
ic.DAO.PutStorageItem(n.ID, key, bigint.ToBytes(tmp))
ic.DAO.PutBigInt(n.ID, key, tmp)
}
}
}
@ -495,7 +495,7 @@ func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.Sto
postF = func() { n.GAS.mint(ic, h, newGas, true) }
}
if amount.Sign() == 0 {
*si = acc.Bytes()
*si = acc.Bytes(ic.DAO.GetItemCtx())
return postF, nil
}
if err := n.ModifyAccountVotes(acc, ic.DAO, amount, false); err != nil {
@ -508,7 +508,7 @@ func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.Sto
}
acc.Balance.Add(&acc.Balance, amount)
if acc.Balance.Sign() != 0 {
*si = acc.Bytes()
*si = acc.Bytes(ic.DAO.GetItemCtx())
} else {
*si = nil
}
@ -872,7 +872,7 @@ func (n *NEO) VoteInternal(ic *interop.Context, h util.Uint160, pub *keys.Public
if err := n.ModifyAccountVotes(acc, ic.DAO, &acc.Balance, true); err != nil {
return err
}
ic.DAO.PutStorageItem(n.ID, key, acc.Bytes())
ic.DAO.PutStorageItem(n.ID, key, acc.Bytes(ic.DAO.GetItemCtx()))
ic.AddNotification(n.Hash, "Vote", stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray(h.BytesBE()),
@ -1088,8 +1088,7 @@ func (n *NEO) modifyVoterTurnout(d *dao.Simple, amount *big.Int) error {
}
votersCount := bigint.FromBytes(si)
votersCount.Add(votersCount, amount)
si = bigint.ToPreallocatedBytes(votersCount, si)
d.PutStorageItem(n.ID, key, si)
d.PutBigInt(n.ID, key, votersCount)
return nil
}
@ -1218,5 +1217,5 @@ func (n *NEO) putGASRecord(dao *dao.Simple, index uint32, value *big.Int) {
key := make([]byte, 5)
key[0] = prefixGASPerBlock
binary.BigEndian.PutUint32(key[1:], index)
dao.PutStorageItem(n.ID, key, bigint.ToBytes(value))
dao.PutBigInt(n.ID, key, value)
}

View file

@ -108,8 +108,7 @@ func (c *nep17TokenNative) getTotalSupply(d *dao.Simple) (state.StorageItem, *bi
}
func (c *nep17TokenNative) saveTotalSupply(d *dao.Simple, si state.StorageItem, supply *big.Int) {
si = state.StorageItem(bigint.ToPreallocatedBytes(supply, si))
d.PutStorageItem(c.ID, totalSupplyKey, si)
d.PutBigInt(c.ID, totalSupplyKey, supply)
}
func (c *nep17TokenNative) Transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item {

View file

@ -82,8 +82,8 @@ func (k *keysWithVotes) fromStackItem(item stackitem.Item) error {
}
// Bytes serializes keys with votes slice.
func (k keysWithVotes) Bytes() []byte {
buf, err := stackitem.Serialize(k.toStackItem())
func (k keysWithVotes) Bytes(sc *stackitem.SerializationContext) []byte {
buf, err := sc.Serialize(k.toStackItem(), false)
if err != nil {
panic(err)
}

View file

@ -355,8 +355,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string
itemID := bigint.FromBytes(si)
id := itemID.Uint64()
itemID.Add(itemID, intOne)
si = bigint.ToPreallocatedBytes(itemID, si)
ic.DAO.PutStorageItem(o.ID, prefixRequestID, si)
ic.DAO.PutBigInt(o.ID, prefixRequestID, itemID)
// Should be executed from the contract.
_, err := ic.GetContract(ic.VM.GetCallingScriptHash())
@ -364,13 +363,14 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string
return err
}
data, err := stackitem.Serialize(userData)
data, err := ic.DAO.GetItemCtx().Serialize(userData, false)
if err != nil {
return err
}
if len(data) > maxUserDataLength {
return ErrBigArgument
}
data = slice.Copy(data) // Serialization context will be used in PutRequestInternal again.
var filterNotif stackitem.Item
if filter != nil {

View file

@ -160,8 +160,8 @@ func newStd() *Std {
return s
}
func (s *Std) serialize(_ *interop.Context, args []stackitem.Item) stackitem.Item {
data, err := stackitem.Serialize(args[0])
func (s *Std) serialize(ic *interop.Context, args []stackitem.Item) stackitem.Item {
data, err := ic.DAO.GetItemCtx().Serialize(args[0], false)
if err != nil {
panic(err)
}
@ -169,7 +169,7 @@ func (s *Std) serialize(_ *interop.Context, args []stackitem.Item) stackitem.Ite
panic(errors.New("too big item"))
}
return stackitem.NewByteArray(data)
return stackitem.NewByteArray(slice.Copy(data)) // Serialization context can be reused.
}
func (s *Std) deserialize(_ *interop.Context, args []stackitem.Item) stackitem.Item {

View file

@ -9,6 +9,7 @@ import (
"testing"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
base58neogo "github.com/nspcc-dev/neo-go/pkg/encoding/base58"
"github.com/nspcc-dev/neo-go/pkg/vm"
@ -19,7 +20,7 @@ import (
func TestStdLibItoaAtoi(t *testing.T) {
s := newStd()
ic := &interop.Context{VM: vm.New()}
ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}}
var actual stackitem.Item
t.Run("itoa-atoi", func(t *testing.T) {
@ -251,7 +252,7 @@ func TestStdLibEncodeDecode(t *testing.T) {
func TestStdLibSerialize(t *testing.T) {
s := newStd()
ic := &interop.Context{VM: vm.New()}
ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}}
t.Run("recursive", func(t *testing.T) {
arr := stackitem.NewArray(nil)
@ -294,7 +295,7 @@ func TestStdLibSerialize(t *testing.T) {
func TestStdLibSerializeDeserialize(t *testing.T) {
s := newStd()
ic := &interop.Context{VM: vm.New()}
ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}}
var actual stackitem.Item
checkSerializeDeserialize := func(t *testing.T, value interface{}, expected stackitem.Item) {
@ -381,7 +382,7 @@ func TestStdLibSerializeDeserialize(t *testing.T) {
func TestMemoryCompare(t *testing.T) {
s := newStd()
ic := &interop.Context{VM: vm.New()}
ic := &interop.Context{VM: vm.New(), DAO: &dao.Simple{}}
check := func(t *testing.T, result int64, s1, s2 string) {
actual := s.memoryCompare(ic, []stackitem.Item{stackitem.Make(s1), stackitem.Make(s2)})

View file

@ -24,7 +24,11 @@ func getConvertibleFromDAO(id int32, d *dao.Simple, key []byte, conv stackitem.C
}
func putConvertibleToDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Convertible) error {
data, err := stackitem.SerializeConvertible(conv)
item, err := conv.ToStackItem()
if err != nil {
return err
}
data, err := d.GetItemCtx().Serialize(item, false)
if err != nil {
return err
}
@ -33,7 +37,7 @@ func putConvertibleToDAO(id int32, d *dao.Simple, key []byte, conv stackitem.Con
}
func setIntWithKey(id int32, dao *dao.Simple, key []byte, value int64) {
dao.PutStorageItem(id, key, bigint.ToBytes(big.NewInt(value)))
dao.PutBigInt(id, key, big.NewInt(value))
}
func getIntWithKey(id int32, dao *dao.Simple, key []byte) int64 {