From f957af35d4aa8fe7c1e33365b16d3500fcd18702 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 17 Dec 2019 11:00:40 +0300 Subject: [PATCH] vm: do not store items of scalar types in map As they do not contain any other items, they can be only accounted via total size. --- pkg/vm/stack.go | 57 +++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index af8f2cecc..300adbbf0 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -214,19 +214,21 @@ func (s *Stack) insert(e, at *Element) *Element { func (s *Stack) updateSizeAdd(item StackItem) { *s.size++ - s.itemCount[item]++ - if s.itemCount[item] > 1 { - return - } - - switch t := item.(type) { - case *ArrayItem, *StructItem: - for _, it := range item.Value().([]StackItem) { - s.updateSizeAdd(it) + switch item.(type) { + case *ArrayItem, *StructItem, *MapItem: + if s.itemCount[item]++; s.itemCount[item] > 1 { + return } - case *MapItem: - for _, v := range t.value { - s.updateSizeAdd(v) + + switch t := item.(type) { + case *ArrayItem, *StructItem: + for _, it := range item.Value().([]StackItem) { + s.updateSizeAdd(it) + } + case *MapItem: + for _, v := range t.value { + s.updateSizeAdd(v) + } } } } @@ -234,21 +236,24 @@ func (s *Stack) updateSizeAdd(item StackItem) { func (s *Stack) updateSizeRemove(item StackItem) { *s.size-- - if s.itemCount[item] > 1 { - s.itemCount[item]-- - return - } - - delete(s.itemCount, item) - - switch t := item.(type) { - case *ArrayItem, *StructItem: - for _, it := range item.Value().([]StackItem) { - s.updateSizeRemove(it) + switch item.(type) { + case *ArrayItem, *StructItem, *MapItem: + if s.itemCount[item] > 1 { + s.itemCount[item]-- + return } - case *MapItem: - for _, v := range t.value { - s.updateSizeRemove(v) + + delete(s.itemCount, item) + + switch t := item.(type) { + case *ArrayItem, *StructItem: + for _, it := range item.Value().([]StackItem) { + s.updateSizeRemove(it) + } + case *MapItem: + for _, v := range t.value { + s.updateSizeRemove(v) + } } } }