vm: optimise emit.Array() to use NEWARRAY0 for zero-lenght arrays

This commit is contained in:
Anna Shaleva 2022-01-14 17:21:14 +03:00
parent 2f18b114f2
commit 0e1f85b2bf
3 changed files with 6 additions and 8 deletions

View file

@ -13,7 +13,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/core/state"
@ -29,7 +28,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
@ -89,11 +87,7 @@ func newOracle() *Oracle {
defer o.UpdateHash()
w := io.NewBufBinWriter()
emit.Opcodes(w.BinWriter, opcode.NEWARRAY0)
emit.Int(w.BinWriter, int64(callflag.All))
emit.String(w.BinWriter, "finish")
emit.Bytes(w.BinWriter, o.Hash.BytesBE())
emit.Syscall(w.BinWriter, interopnames.SystemContractCall)
emit.AppCall(w.BinWriter, o.Hash, "finish", callflag.All)
o.oracleScript = w.Bytes()
desc := newDescriptor("request", smartcontract.VoidType,

View file

@ -76,6 +76,10 @@ func bigInt(w *io.BinWriter, n *big.Int) {
// Array emits array of elements to the given buffer.
func Array(w *io.BinWriter, es ...interface{}) {
if len(es) == 0 {
Opcodes(w, opcode.NEWARRAY0)
return
}
for i := len(es) - 1; i >= 0; i-- {
switch e := es[i].(type) {
case []interface{}:

View file

@ -177,7 +177,7 @@ func TestEmitArray(t *testing.T) {
buf := io.NewBufBinWriter()
Array(buf.BinWriter)
require.NoError(t, buf.Err)
assert.EqualValues(t, []byte{byte(opcode.PUSH0), byte(opcode.PACK)}, buf.Bytes())
assert.EqualValues(t, []byte{byte(opcode.NEWARRAY0)}, buf.Bytes())
})
t.Run("invalid type", func(t *testing.T) {