vm: embed reference counter in compound items

```
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>
This commit is contained in:
Evgeniy Stratonikov 2021-08-11 15:16:29 +03:00
parent dc9287bf5c
commit 4f98ec2f53
7 changed files with 99 additions and 55 deletions

View file

@ -188,6 +188,7 @@ func convertPrimitive(item Item, typ Type) (Item, error) {
// Struct represents a struct on the stack.
type Struct struct {
value []Item
rc
}
// NewStruct returns an new Struct object.
@ -311,7 +312,7 @@ func (i *Struct) Clone() (*Struct, error) {
}
func (i *Struct) clone(limit *int) (*Struct, error) {
ret := &Struct{make([]Item, len(i.value))}
ret := &Struct{value: make([]Item, len(i.value))}
for j := range i.value {
*limit--
if *limit < 0 {
@ -624,6 +625,7 @@ func (i *ByteArray) Convert(typ Type) (Item, error) {
// Array represents a new Array object.
type Array struct {
value []Item
rc
}
// NewArray returns a new Array object.
@ -724,6 +726,7 @@ type MapElement struct {
// if need be.
type Map struct {
value []MapElement
rc
}
// NewMap returns new Map object.

View file

@ -0,0 +1,15 @@
package stackitem
type rc struct {
count int
}
func (r *rc) IncRC() int {
r.count++
return r.count
}
func (r *rc) DecRC() int {
r.count--
return r.count
}