Skip to content

Commit 275f2e8

Browse files
authored
Merge pull request #223 from Villenny/patch-3
**Description**: Eliminate 2 allocations from EachKey() **Benchmark before change**: ``` BenchmarkEachKey BenchmarkEachKey-8 308006 3885 ns/op 272 B/op 3 allocs/op PASS ``` **Benchmark after change**: ``` BenchmarkEachKey BenchmarkEachKey-8 333666 3674 ns/op 8 B/op 1 allocs/op PASS ``` Benchmarked with: ``` func BenchmarkEachKey(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { var id string var bidId string var bidImpId string var bidPrice string var bidAdm string var cur string var foo string bodyBytes := stringHelper.StringAsBytes(BidResponse_Banner) jsonparser.EachKey(bodyBytes, func(idx int, value []byte, vt jsonparser.ValueType, err error) { switch idx { case 0: id = stringHelper.BytesAsString(value) case 1: bidId = stringHelper.BytesAsString(value) case 2: bidImpId = stringHelper.BytesAsString(value) case 3: bidPrice = stringHelper.BytesAsString(value) case 4: bidAdm = stringHelper.BytesAsString(value) case 5: cur = stringHelper.BytesAsString(value) case 6: foo = stringHelper.BytesAsString(value) } }, [][]string{ {"id"}, {"seatbid", "[0]", "bid", "[0]", "id"}, {"seatbid", "[0]", "bid", "[0]", "impid"}, {"seatbid", "[0]", "bid", "[0]", "price"}, {"seatbid", "[0]", "bid", "[0]", "adm"}, {"cur"}, {"foo"}, }..., ) if "9f3701ef-1d8a-4b67-a601-e9a1a2fbcb7c" != id || "0" != bidId || "1" != bidImpId || "0.35258" != bidPrice || `some \"html\" code\nnext line` != bidAdm || "USD" != cur || "" != foo { panic("ack") } } } const BidResponse_Banner = ` { "id":"9f3701ef-1d8a-4b67-a601-e9a1a2fbcb7c", "seatbid":[ { "bid":[ { "id":"0", "adomain":[ "someurl.com", "someotherurl.com" ], "impid":"1", "price":0.35258, "adm":"some \"html\" code\nnext line", "w":300, "h":250 } ] } ], "cur":"USD" } ` ```
2 parents 702655d + 449b024 commit 275f2e8

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

parser.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,20 +374,31 @@ func sameTree(p1, p2 []string) bool {
374374
return true
375375
}
376376

377+
const stackArraySize = 128
378+
377379
func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int {
378380
var x struct{}
379-
pathFlags := make([]bool, len(paths))
380381
var level, pathsMatched, i int
381382
ln := len(data)
382383

384+
pathFlags := make([]bool, stackArraySize)[:]
385+
if len(paths) > cap(pathFlags) {
386+
pathFlags = make([]bool, len(paths))[:]
387+
}
388+
pathFlags = pathFlags[0:len(paths)]
389+
383390
var maxPath int
384391
for _, p := range paths {
385392
if len(p) > maxPath {
386393
maxPath = len(p)
387394
}
388395
}
389396

390-
pathsBuf := make([]string, maxPath)
397+
pathsBuf := make([]string, stackArraySize)[:]
398+
if maxPath > cap(pathsBuf) {
399+
pathsBuf = make([]string, maxPath)[:]
400+
}
401+
pathsBuf = pathsBuf[0:maxPath]
391402

392403
for i < ln {
393404
switch data[i] {
@@ -662,7 +673,6 @@ func calcAllocateSpace(keys []string, setValue []byte, comma, object bool) int {
662673
}
663674
}
664675

665-
666676
lk += len(setValue)
667677
for i := 1; i < len(keys); i++ {
668678
if string(keys[i][0]) == "[" {

0 commit comments

Comments
 (0)