max: preallocate fastjson array object var memory

This commit is contained in:
c9s 2021-07-08 10:28:49 +08:00 committed by c9s
parent e67155d6cc
commit 6cf5300650
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -229,25 +229,27 @@ func parseBookEvent(val *fastjson.Value) (event *BookEvent, err error) {
// parseBookEntries2 parses JSON struct like `[["233330", "0.33"], ....]`
func parseBookEntries2(vals []*fastjson.Value) (entries types.PriceVolumeSlice, err error) {
entries = make(types.PriceVolumeSlice, 0, 50)
var arr []*fastjson.Value
for _, entry := range vals {
arr, err := entry.Array()
arr, err = entry.Array()
if err != nil {
return nil, err
return entries, err
}
if len(arr) < 2 {
return nil, ErrIncorrectBookEntryElementLength
return entries, ErrIncorrectBookEntryElementLength
}
var pv types.PriceVolume
pv.Price, err = fixedpoint.NewFromString(string(arr[0].GetStringBytes()))
if err != nil {
return nil, err
return entries, err
}
pv.Volume, err = fixedpoint.NewFromString(string(arr[1].GetStringBytes()))
if err != nil {
return nil, err
return entries, err
}
entries = append(entries, pv)