Skip to content

Commit a4ccd62

Browse files
committed
fit Value slice to sizeclass valyala#67 (32768 bytes)
1 parent 279fad3 commit a4ccd62

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

parser.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,29 @@ func (c *cache) reset() {
6363
c.vs = c.vs[:0]
6464
}
6565

66+
const preAllocatedCacheSize = 409 // calculated as 32768 / unsafe.SizeOf(Value)
67+
6668
func (c *cache) getValue() *Value {
67-
l := len(c.vs) - 1
68-
needExt := l < 0 || cap(c.vs[l]) == len(c.vs[l])
69+
last := len(c.vs) - 1
70+
needExt := last < 0 || cap(c.vs[last]) == len(c.vs[last])
6971
for {
7072
if needExt {
7173
if cap(c.vs) > len(c.vs) {
7274
c.vs = c.vs[:len(c.vs)+1]
7375
} else {
74-
c.vs = append(c.vs, make([]Value, 0, 32768))
76+
c.vs = append(c.vs, make([]Value, 0, preAllocatedCacheSize))
7577
}
76-
l = len(c.vs) - 1
78+
last = len(c.vs) - 1
7779
needExt = false
7880
}
79-
if cap(c.vs[l]) > len(c.vs[l]) {
80-
c.vs[l] = c.vs[l][:len(c.vs[l])+1]
81+
if cap(c.vs[last]) > len(c.vs[last]) {
82+
c.vs[last] = c.vs[last][:len(c.vs[last])+1]
8183
} else {
8284
needExt = true
8385
continue
8486
}
8587
// Do not reset the value, since the caller must properly init it.
86-
return &c.vs[l][len(c.vs[l])-1]
88+
return &c.vs[last][len(c.vs[last])-1]
8789
}
8890
}
8991

0 commit comments

Comments
 (0)