Skip to content

Commit 279fad3

Browse files
committed
hypothesis: reduce allocations
1 parent 93f67d9 commit 279fad3

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

parser.go

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

5555
type cache struct {
56-
vs []Value
56+
vs [][]Value
5757
}
5858

5959
func (c *cache) reset() {
60+
for i := range c.vs {
61+
c.vs[i] = c.vs[i][:0]
62+
}
6063
c.vs = c.vs[:0]
6164
}
6265

6366
func (c *cache) getValue() *Value {
64-
if cap(c.vs) > len(c.vs) {
65-
c.vs = c.vs[:len(c.vs)+1]
66-
} else {
67-
c.vs = append(c.vs, Value{})
67+
l := len(c.vs) - 1
68+
needExt := l < 0 || cap(c.vs[l]) == len(c.vs[l])
69+
for {
70+
if needExt {
71+
if cap(c.vs) > len(c.vs) {
72+
c.vs = c.vs[:len(c.vs)+1]
73+
} else {
74+
c.vs = append(c.vs, make([]Value, 0, 32768))
75+
}
76+
l = len(c.vs) - 1
77+
needExt = false
78+
}
79+
if cap(c.vs[l]) > len(c.vs[l]) {
80+
c.vs[l] = c.vs[l][:len(c.vs[l])+1]
81+
} else {
82+
needExt = true
83+
continue
84+
}
85+
// Do not reset the value, since the caller must properly init it.
86+
return &c.vs[l][len(c.vs[l])-1]
6887
}
69-
// Do not reset the value, since the caller must properly init it.
70-
return &c.vs[len(c.vs)-1]
7188
}
7289

7390
func skipWS(s string) string {

0 commit comments

Comments
 (0)