Commit 275f2e8
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"
}
`
```1 file changed
+13
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
| 377 | + | |
| 378 | + | |
377 | 379 | | |
378 | 380 | | |
379 | | - | |
380 | 381 | | |
381 | 382 | | |
382 | 383 | | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
383 | 390 | | |
384 | 391 | | |
385 | 392 | | |
386 | 393 | | |
387 | 394 | | |
388 | 395 | | |
389 | 396 | | |
390 | | - | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
391 | 402 | | |
392 | 403 | | |
393 | 404 | | |
| |||
662 | 673 | | |
663 | 674 | | |
664 | 675 | | |
665 | | - | |
666 | 676 | | |
667 | 677 | | |
668 | 678 | | |
| |||
0 commit comments