Skip to content

Commit 3460630

Browse files
richardartoulbuger
authored andcommitted
Only allocate array if required (#173)
My benchmarking showed that this array was actually leaking to the heap. Narrowing its scope in the hope of it not escaping, but also limiting its allocation only to cases where it is actually required.
1 parent bf1c66b commit 3460630

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,6 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
980980

981981
// ObjectEach iterates over the key-value pairs of a JSON object, invoking a given callback for each such entry
982982
func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error) {
983-
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
984983
offset := 0
985984

986985
// Descend to the desired key, if requested
@@ -1034,6 +1033,7 @@ func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType Va
10341033

10351034
// Unescape the string if needed
10361035
if keyEscaped {
1036+
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
10371037
if keyUnescaped, err := Unescape(key, stackbuf[:]); err != nil {
10381038
return MalformedStringEscapeError
10391039
} else {

0 commit comments

Comments
 (0)