1
0
mirror of https://github.com/json-iterator/go.git synced 2025-07-06 23:37:39 +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:
Phil Pearl
2022-02-14 11:09:22 +00:00
parent 024077e996
commit e79221c6f6
4 changed files with 74 additions and 11 deletions

28
benchmarks/float_test.go Normal file
View 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)
}
}
}