mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 03:38:35 +00:00
4f98ec2f53
``` name old time/op new time/op delta RefCounter_Add-8 44.8ns ± 4% 11.7ns ± 3% -73.94% (p=0.000 n=10+10) ``` Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package vm
|
|
|
|
import (
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
)
|
|
|
|
// refCounter represents reference counter for the VM.
|
|
type refCounter int
|
|
|
|
type (
|
|
rcInc interface {
|
|
IncRC() int
|
|
}
|
|
rcDec interface {
|
|
DecRC() 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++
|
|
|
|
irc, ok := item.(rcInc)
|
|
if !ok || irc.IncRC() > 1 {
|
|
return
|
|
}
|
|
switch t := item.(type) {
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
for _, it := range item.Value().([]stackitem.Item) {
|
|
r.Add(it)
|
|
}
|
|
case *stackitem.Map:
|
|
for i := range t.Value().([]stackitem.MapElement) {
|
|
r.Add(t.Value().([]stackitem.MapElement)[i].Value)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove removes item from the reference counter.
|
|
func (r *refCounter) Remove(item stackitem.Item) {
|
|
if r == nil {
|
|
return
|
|
}
|
|
*r--
|
|
|
|
irc, ok := item.(rcDec)
|
|
if !ok || irc.DecRC() > 0 {
|
|
return
|
|
}
|
|
switch t := item.(type) {
|
|
case *stackitem.Array, *stackitem.Struct:
|
|
for _, it := range item.Value().([]stackitem.Item) {
|
|
r.Remove(it)
|
|
}
|
|
case *stackitem.Map:
|
|
for i := range t.Value().([]stackitem.MapElement) {
|
|
r.Remove(t.Value().([]stackitem.MapElement)[i].Value)
|
|
}
|
|
}
|
|
}
|