vm: optimize refcounter

```
name                    old time/op    new time/op    delta
RefCounter_Add-8          9.47ns ± 4%    2.75ns ± 1%   -70.92%  (p=0.000 n=10+9)
RefCounter_AddRemove-8    88.9ns ±20%     5.5ns ± 4%   -93.78%  (p=0.000 n=10+10)

name                    old alloc/op   new alloc/op   delta
RefCounter_Add-8           0.00B          0.00B           ~     (all equal)
RefCounter_AddRemove-8     48.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)

name                    old allocs/op  new allocs/op  delta
RefCounter_Add-8            0.00           0.00           ~     (all equal)
RefCounter_AddRemove-8      2.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
```

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-08-23 09:19:09 +03:00
parent 59fbc689e4
commit 9712be78fd
2 changed files with 40 additions and 31 deletions

View file

@ -16,6 +16,12 @@ var fuzzSeedValidScripts = [][]byte{
makeProgram(opcode.PUSH2, opcode.NEWARRAY, opcode.DUP, opcode.PUSH0, opcode.PUSH1, opcode.SETITEM, opcode.VALUES), makeProgram(opcode.PUSH2, opcode.NEWARRAY, opcode.DUP, opcode.PUSH0, opcode.PUSH1, opcode.SETITEM, opcode.VALUES),
append([]byte{byte(opcode.PUSHDATA1), 10}, randomBytes(10)...), append([]byte{byte(opcode.PUSHDATA1), 10}, randomBytes(10)...),
append([]byte{byte(opcode.PUSHDATA1), 100}, randomBytes(100)...), append([]byte{byte(opcode.PUSHDATA1), 100}, randomBytes(100)...),
// Simplified version of fuzzer output from #2659.
{byte(opcode.CALL), 3, byte(opcode.ASSERT),
byte(opcode.CALL), 3, byte(opcode.ASSERT),
byte(opcode.DEPTH), byte(opcode.PACKSTRUCT), byte(opcode.DUP),
byte(opcode.UNPACK), byte(opcode.PACKSTRUCT), byte(opcode.POPITEM),
byte(opcode.DEPTH)},
} }
func FuzzIsScriptCorrect(f *testing.F) { func FuzzIsScriptCorrect(f *testing.F) {

View file

@ -7,15 +7,6 @@ import (
// refCounter represents a reference counter for the VM. // refCounter represents a reference counter for the VM.
type refCounter int type refCounter int
type (
rcInc interface {
IncRC() int
}
rcDec interface {
DecRC() int
}
)
func newRefCounter() *refCounter { func newRefCounter() *refCounter {
return new(refCounter) return new(refCounter)
} }
@ -27,22 +18,28 @@ func (r *refCounter) Add(item stackitem.Item) {
} }
*r++ *r++
irc, ok := item.(rcInc)
if !ok || irc.IncRC() > 1 {
return
}
switch t := item.(type) { switch t := item.(type) {
case *stackitem.Array, *stackitem.Struct: case *stackitem.Array:
for _, it := range item.Value().([]stackitem.Item) { if t.IncRC() == 1 {
for _, it := range t.Value().([]stackitem.Item) {
r.Add(it) r.Add(it)
} }
}
case *stackitem.Struct:
if t.IncRC() == 1 {
for _, it := range t.Value().([]stackitem.Item) {
r.Add(it)
}
}
case *stackitem.Map: case *stackitem.Map:
if t.IncRC() == 1 {
elems := t.Value().([]stackitem.MapElement) elems := t.Value().([]stackitem.MapElement)
for i := range elems { for i := range elems {
r.Add(elems[i].Key) r.Add(elems[i].Key)
r.Add(elems[i].Value) r.Add(elems[i].Value)
} }
} }
}
} }
// Remove removes an item from the reference counter. // Remove removes an item from the reference counter.
@ -52,20 +49,26 @@ func (r *refCounter) Remove(item stackitem.Item) {
} }
*r-- *r--
irc, ok := item.(rcDec)
if !ok || irc.DecRC() > 0 {
return
}
switch t := item.(type) { switch t := item.(type) {
case *stackitem.Array, *stackitem.Struct: case *stackitem.Array:
for _, it := range item.Value().([]stackitem.Item) { if t.DecRC() == 0 {
for _, it := range t.Value().([]stackitem.Item) {
r.Remove(it) r.Remove(it)
} }
}
case *stackitem.Struct:
if t.DecRC() == 0 {
for _, it := range t.Value().([]stackitem.Item) {
r.Remove(it)
}
}
case *stackitem.Map: case *stackitem.Map:
if t.DecRC() == 0 {
elems := t.Value().([]stackitem.MapElement) elems := t.Value().([]stackitem.MapElement)
for i := range elems { for i := range elems {
r.Remove(elems[i].Key) r.Remove(elems[i].Key)
r.Remove(elems[i].Value) r.Remove(elems[i].Value)
} }
} }
}
} }