mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 03:41:34 +00:00
9712be78fd
``` 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>
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package vm
|
|
|
|
import (
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
)
|
|
|
|
// refCounter represents a reference counter for the VM.
|
|
type refCounter int
|
|
|
|
func newRefCounter() *refCounter {
|
|
return new(refCounter)
|
|
}
|
|
|
|
// Add adds an item to the reference counter.
|
|
func (r *refCounter) Add(item stackitem.Item) {
|
|
if r == nil {
|
|
return
|
|
}
|
|
*r++
|
|
|
|
switch t := item.(type) {
|
|
case *stackitem.Array:
|
|
if t.IncRC() == 1 {
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
r.Add(it)
|
|
}
|
|
}
|
|
case *stackitem.Struct:
|
|
if t.IncRC() == 1 {
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
r.Add(it)
|
|
}
|
|
}
|
|
case *stackitem.Map:
|
|
if t.IncRC() == 1 {
|
|
elems := t.Value().([]stackitem.MapElement)
|
|
for i := range elems {
|
|
r.Add(elems[i].Key)
|
|
r.Add(elems[i].Value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove removes an item from the reference counter.
|
|
func (r *refCounter) Remove(item stackitem.Item) {
|
|
if r == nil {
|
|
return
|
|
}
|
|
*r--
|
|
|
|
switch t := item.(type) {
|
|
case *stackitem.Array:
|
|
if t.DecRC() == 0 {
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
r.Remove(it)
|
|
}
|
|
}
|
|
case *stackitem.Struct:
|
|
if t.DecRC() == 0 {
|
|
for _, it := range t.Value().([]stackitem.Item) {
|
|
r.Remove(it)
|
|
}
|
|
}
|
|
case *stackitem.Map:
|
|
if t.DecRC() == 0 {
|
|
elems := t.Value().([]stackitem.MapElement)
|
|
for i := range elems {
|
|
r.Remove(elems[i].Key)
|
|
r.Remove(elems[i].Value)
|
|
}
|
|
}
|
|
}
|
|
}
|