mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
Unmarshalling float with lots of digits can cause >= 2 allocations per number
We can fix this with a little bit of scratch space.
This commit is contained in:
parent
024077e996
commit
e79221c6f6
28
benchmarks/float_test.go
Normal file
28
benchmarks/float_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkFloatUnmarshal(b *testing.B) {
|
||||||
|
type floaty struct {
|
||||||
|
A float32
|
||||||
|
B float64
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := jsoniter.Marshal(floaty{A: 1.111111111111111, B: 1.11111111111111})
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
var out floaty
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
if err := jsoniter.Unmarshal(data, &out); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,7 +10,8 @@ import (
|
|||||||
|
|
||||||
func Benchmark_stream_encode_big_object(b *testing.B) {
|
func Benchmark_stream_encode_big_object(b *testing.B) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var stream = jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 100)
|
stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 100)
|
||||||
|
b.ReportAllocs()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
stream.Reset(&buf)
|
stream.Reset(&buf)
|
||||||
@ -22,13 +23,13 @@ func Benchmark_stream_encode_big_object(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEncodeObject(t *testing.T) {
|
func TestEncodeObject(t *testing.T) {
|
||||||
var stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 100)
|
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 100)
|
||||||
encodeObject(stream)
|
encodeObject(stream)
|
||||||
if stream.Error != nil {
|
if stream.Error != nil {
|
||||||
t.Errorf("error encoding a test object: %+v", stream.Error)
|
t.Errorf("error encoding a test object: %+v", stream.Error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var m = make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
if err := jsoniter.Unmarshal(stream.Buffer(), &m); err != nil {
|
if err := jsoniter.Unmarshal(stream.Buffer(), &m); err != nil {
|
||||||
t.Errorf("error unmarshaling a test object: %+v", err)
|
t.Errorf("error unmarshaling a test object: %+v", err)
|
||||||
return
|
return
|
||||||
|
7
iter.go
7
iter.go
@ -26,8 +26,10 @@ const (
|
|||||||
ObjectValue
|
ObjectValue
|
||||||
)
|
)
|
||||||
|
|
||||||
var hexDigits []byte
|
var (
|
||||||
var valueTypes []ValueType
|
hexDigits []byte
|
||||||
|
valueTypes []ValueType
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
hexDigits = make([]byte, 256)
|
hexDigits = make([]byte, 256)
|
||||||
@ -78,6 +80,7 @@ type Iterator struct {
|
|||||||
captureStartedAt int
|
captureStartedAt int
|
||||||
captured []byte
|
captured []byte
|
||||||
Error error
|
Error error
|
||||||
|
scratch []byte
|
||||||
Attachment interface{} // open for customized decoder
|
Attachment interface{} // open for customized decoder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,9 +11,11 @@ import (
|
|||||||
|
|
||||||
var floatDigits []int8
|
var floatDigits []int8
|
||||||
|
|
||||||
const invalidCharForNumber = int8(-1)
|
const (
|
||||||
const endOfNumber = int8(-2)
|
invalidCharForNumber = int8(-1)
|
||||||
const dotInNumber = int8(-3)
|
endOfNumber = int8(-2)
|
||||||
|
dotInNumber = int8(-3)
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
floatDigits = make([]int8, 256)
|
floatDigits = make([]int8, 256)
|
||||||
@ -66,7 +68,7 @@ func (iter *Iterator) ReadBigInt() (ret *big.Int) {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
//ReadFloat32 read float32
|
// ReadFloat32 read float32
|
||||||
func (iter *Iterator) ReadFloat32() (ret float32) {
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||||
c := iter.nextToken()
|
c := iter.nextToken()
|
||||||
if c == '-' {
|
if c == '-' {
|
||||||
@ -185,11 +187,39 @@ load_loop:
|
|||||||
return *(*string)(unsafe.Pointer(&str))
|
return *(*string)(unsafe.Pointer(&str))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (iter *Iterator) appendNumber(buf []byte) []byte {
|
||||||
|
load_loop:
|
||||||
|
for {
|
||||||
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
|
c := iter.buf[i]
|
||||||
|
switch c {
|
||||||
|
case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
|
buf = append(buf, c)
|
||||||
|
continue
|
||||||
|
default:
|
||||||
|
iter.head = i
|
||||||
|
break load_loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !iter.loadMore() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
if len(buf) == 0 {
|
||||||
|
iter.ReportError("readNumberAsString", "invalid number")
|
||||||
|
}
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
||||||
str := iter.readNumberAsString()
|
iter.scratch = iter.appendNumber(iter.scratch[:0])
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
str := *(*string)(unsafe.Pointer(&iter.scratch))
|
||||||
errMsg := validateFloat(str)
|
errMsg := validateFloat(str)
|
||||||
if errMsg != "" {
|
if errMsg != "" {
|
||||||
iter.ReportError("readFloat32SlowPath", errMsg)
|
iter.ReportError("readFloat32SlowPath", errMsg)
|
||||||
@ -297,10 +327,11 @@ non_decimal_loop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
||||||
str := iter.readNumberAsString()
|
iter.scratch = iter.appendNumber(iter.scratch[:0])
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
str := *(*string)(unsafe.Pointer(&iter.scratch))
|
||||||
errMsg := validateFloat(str)
|
errMsg := validateFloat(str)
|
||||||
if errMsg != "" {
|
if errMsg != "" {
|
||||||
iter.ReportError("readFloat64SlowPath", errMsg)
|
iter.ReportError("readFloat64SlowPath", errMsg)
|
||||||
|
Loading…
Reference in New Issue
Block a user