Skip to content

Commit 1d95b47

Browse files
committed
progressively increase cache buffer
1 parent a4ccd62 commit 1d95b47

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

parser.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func (p *Parser) ParseBytes(b []byte) (*Value, error) {
5353
}
5454

5555
type cache struct {
56-
vs [][]Value
56+
vs [][]Value
57+
allocated int
5758
}
5859

5960
func (c *cache) reset() {
@@ -63,7 +64,10 @@ func (c *cache) reset() {
6364
c.vs = c.vs[:0]
6465
}
6566

66-
const preAllocatedCacheSize = 409 // calculated as 32768 / unsafe.SizeOf(Value)
67+
const (
68+
minPreAllocatedCacheSize = 256
69+
maxPreAllocatedCacheSize = 32768
70+
)
6771

6872
func (c *cache) getValue() *Value {
6973
last := len(c.vs) - 1
@@ -73,7 +77,12 @@ func (c *cache) getValue() *Value {
7377
if cap(c.vs) > len(c.vs) {
7478
c.vs = c.vs[:len(c.vs)+1]
7579
} else {
76-
c.vs = append(c.vs, make([]Value, 0, preAllocatedCacheSize))
80+
c.allocated++
81+
newSz := minPreAllocatedCacheSize * c.allocated
82+
if newSz > maxPreAllocatedCacheSize {
83+
newSz = maxPreAllocatedCacheSize
84+
}
85+
c.vs = append(c.vs, make([]Value, 0, newSz))
7786
}
7887
last = len(c.vs) - 1
7988
needExt = false

0 commit comments

Comments
 (0)