stackitem: add NewPointerWithHash() to save on hash calculations

Inspired by neo-project/neo-vm#352. We can't directly compare slices, so we're
better optimize things we already have. At the same time this code would
behave a bit different if A is to call B and then B is call A and then some
pointer from the first A invocation is to be compared with a pointer from the
second A invocation. Not sure it really matters.
This commit is contained in:
Roman Khimov 2020-08-22 22:19:44 +03:00
parent e5813ae8cd
commit 74097ae8b0
2 changed files with 14 additions and 2 deletions

View file

@ -871,6 +871,18 @@ func NewPointer(pos int, script []byte) *Pointer {
}
}
// NewPointerWithHash returns new pointer on the specified position of the
// specified script. It differs from NewPointer in that the script hash is being
// passed explicitly to save on hash calculcation. This hash is then being used
// for pointer comparisons.
func NewPointerWithHash(pos int, script []byte, h util.Uint160) *Pointer {
return &Pointer{
pos: pos,
script: script,
hash: h,
}
}
// String implements Item interface.
func (p *Pointer) String() string {
return "Pointer"
@ -1079,7 +1091,7 @@ func deepCopy(item Item, seen map[Item]Item) Item {
case *Bool:
return NewBool(it.value)
case *Pointer:
return NewPointer(it.pos, it.script)
return NewPointerWithHash(it.pos, it.script, it.hash)
case *Interop:
return NewInterop(it.value)
default:

View file

@ -535,7 +535,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
case opcode.PUSHA:
n := v.getJumpOffset(ctx, parameter)
ptr := stackitem.NewPointer(n, ctx.prog)
ptr := stackitem.NewPointerWithHash(n, ctx.prog, ctx.ScriptHash())
v.estack.PushVal(ptr)
case opcode.PUSHNULL: