Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve float decoding layout, add more float benchmarks #41

Merged
merged 2 commits into from Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 26 additions & 24 deletions dec_float.go
Expand Up @@ -8,17 +8,19 @@ import (
"github.com/go-faster/errors"
)

var pow10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}

var floatDigits []int8
var (
pow10 = [...]uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
floatDigits = [256]int8{}
)

const invalidCharForNumber = int8(-1)
const endOfNumber = int8(-2)
const dotInNumber = int8(-3)
const maxFloat64 = 1<<63 - 1
const (
invalidCharForNumber = int8(-1)
endOfNumber = int8(-2)
dotInNumber = int8(-3)
maxFloat64 = 1<<63 - 1
)

func init() {
floatDigits = make([]int8, 256)
for i := 0; i < len(floatDigits); i++ {
floatDigits[i] = invalidCharForNumber
}
Expand Down Expand Up @@ -88,25 +90,24 @@ func (d *Decoder) positiveFloat32() (float32, error) {
i := d.head
// First char.
if i == d.tail {
return d.f32Slow()
return d.float32Slow()
}
c := d.buf[i]
i++
ind := floatDigits[c]
switch ind {
case invalidCharForNumber:
return d.f32Slow()
return d.float32Slow()
case endOfNumber:
return 0, errors.New("empty")
case dotInNumber:
return 0, errors.New("leading dot")
case 0:
if i == d.tail {
return d.f32Slow()
return d.float32Slow()
}
c = d.buf[i]
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
if floatDigits[c] >= 0 {
return 0, errors.New("leading zero")
}
}
Expand All @@ -118,15 +119,15 @@ NonDecimalLoop:
ind := floatDigits[c]
switch ind {
case invalidCharForNumber:
return d.f32Slow()
return d.float32Slow()
case endOfNumber:
d.head = i
return float32(value), nil
case dotInNumber:
break NonDecimalLoop
}
if value > uint64SafeToMultiple10 {
return d.f32Slow()
return d.float32Slow()
}
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
}
Expand All @@ -135,7 +136,7 @@ NonDecimalLoop:
i++
decimalPlaces := 0
if i == d.tail {
return d.f32Slow()
return d.float32Slow()
}
for ; i < d.tail; i++ {
c = d.buf[i]
Expand All @@ -147,18 +148,18 @@ NonDecimalLoop:
return float32(float64(value) / float64(pow10[decimalPlaces])), nil
}
// too many decimal places
return d.f32Slow()
return d.float32Slow()
case invalidCharForNumber, dotInNumber:
return d.f32Slow()
return d.float32Slow()
}
decimalPlaces++
if value > uint64SafeToMultiple10 {
return d.f32Slow()
return d.float32Slow()
}
value = (value << 3) + (value << 1) + uint64(ind)
}
}
return d.f32Slow()
return d.float32Slow()
}

var numberSet = [256]byte{
Expand Down Expand Up @@ -214,7 +215,7 @@ const (
size64 = 64
)

func (d *Decoder) f32Slow() (float32, error) {
func (d *Decoder) float32Slow() (float32, error) {
v, err := d.floatSlow(size32)
if err != nil {
return 0, err
Expand All @@ -228,16 +229,17 @@ func (d *Decoder) Float64() (float64, error) {
if err != nil {
return 0, errors.Wrap(err, "byte")
}
if floatDigits[c] >= 0 {
d.unread()
return d.positiveFloat64()
}
switch c {
case '-':
v, err := d.positiveFloat64()
if err != nil {
return 0, err
}
return -v, err
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
d.unread()
return d.positiveFloat64()
default:
return 0, badToken(c)
}
Expand Down
36 changes: 21 additions & 15 deletions dec_float_test.go
Expand Up @@ -134,23 +134,29 @@ func TestDecoder_Float64(t *testing.T) {
}

func BenchmarkDecoder_Float64(b *testing.B) {
runTestdataFile("floats.json", b.Fatal, func(name string, data []byte) {
b.Run(name, func(b *testing.B) {
d := GetDecoder()
cb := func(d *Decoder) error {
_, err := d.Float64()
return err
}
b.ReportAllocs()
b.ResetTimer()
for _, file := range []string{
"floats.json",
"slow_floats.json",
"integers.json",
} {
runTestdataFile(file, b.Fatal, func(name string, data []byte) {
b.Run(name, func(b *testing.B) {
d := GetDecoder()
cb := func(d *Decoder) error {
_, err := d.Float64()
return err
}
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
d.ResetBytes(data)
for i := 0; i < b.N; i++ {
d.ResetBytes(data)

if err := d.Arr(cb); err != nil {
b.Fatal(err)
if err := d.Arr(cb); err != nil {
b.Fatal(err)
}
}
}
})
})
})
}
}
200 changes: 100 additions & 100 deletions testdata/floats.json
@@ -1,102 +1,102 @@
[
0.6046602879796196,
0.9405090880450124,
0.6645600532184904,
0.4377141871869802,
0.4246374970712657,
0.6868230728671094,
0.06563701921747622,
0.15651925473279124,
0.09696951891448456,
0.30091186058528707,
0.5152126285020654,
0.8136399609900968,
0.21426387258237492,
0.380657189299686,
0.31805817433032985,
0.4688898449024232,
0.28303415118044517,
0.29310185733681576,
0.6790846759202163,
0.21855305259276428,
0.20318687664732285,
0.360871416856906,
0.5706732760710226,
0.8624914374478864,
0.29311424455385804,
0.29708256355629153,
0.7525730355516119,
0.2065826619136986,
0.865335013001561,
0.6967191657466347,
0.5238203060500009,
0.028303083325889995,
0.15832827774512764,
0.6072534395455154,
0.9752416188605784,
0.07945362337387198,
0.5948085976830626,
0.05912065131387529,
0.692024587353112,
0.30152268100656,
0.17326623818270528,
0.5410998550087353,
0.544155573000885,
0.27850762181610883,
0.4231522015718281,
0.5305857153507052,
0.2535405005150605,
0.28208099496492467,
0.7886049150193449,
0.3618054804803169,
0.8805431227416171,
0.2971122606397708,
0.8943617293304537,
0.09745461839911657,
0.9769168685862624,
0.07429099894984302,
0.22228941700678773,
0.6810783123925709,
0.24151508854715265,
0.31152244431052484,
0.932846428518434,
0.741848959991823,
0.8010550426526613,
0.7302314772948083,
0.18292491645390843,
0.4283570818068078,
0.8969919575618727,
0.6826534880132438,
0.9789293555766876,
0.9222122589217269,
0.09083727535388708,
0.4931419977048804,
0.9269868035744142,
0.9549454404167818,
0.3479539636282229,
0.6908388315056789,
0.7109071952999951,
0.5637795958152644,
0.6494894605929404,
0.5517650490127749,
0.7558235074915978,
0.40380328579570035,
0.13065111702897217,
0.9859647293402467,
0.8963417453962161,
0.3220839705208817,
0.7211477651926741,
0.6445397825093294,
0.08552050754191123,
0.6695752976997745,
0.6227283173637045,
0.3696928436398219,
0.2368225468054852,
0.5352818906344061,
0.18724610140105305,
0.2388407028053186,
0.6280981712183633,
0.1267529293726013,
0.28133029380535923,
0.41032284435628247
0.6,
0.9,
0.6,
0.4,
0.4,
0.6,
0.0,
0.1,
0.0,
0.3,
0.5,
0.8,
0.2,
0.3,
0.3,
0.4,
0.2,
0.2,
0.6,
0.2,
0.2,
0.3,
0.5,
0.8,
0.2,
0.2,
0.7,
0.2,
0.8,
0.6,
0.5,
0.0,
0.1,
0.6,
0.9,
0.0,
0.5,
0.0,
0.6,
0.3,
0.1,
0.5,
0.5,
0.2,
0.4,
0.5,
0.2,
0.2,
0.7,
0.3,
0.8,
0.2,
0.8,
0.0,
0.9,
0.0,
0.2,
0.6,
0.2,
0.3,
0.9,
0.7,
0.8,
0.7,
0.1,
0.4,
0.8,
0.6,
0.9,
0.9,
0.0,
0.4,
0.9,
0.9,
0.3,
0.6,
0.7,
0.5,
0.6,
0.5,
0.7,
0.4,
0.1,
0.9,
0.8,
0.3,
0.7,
0.6,
0.0,
0.6,
0.6,
0.3,
0.2,
0.5,
0.1,
0.2,
0.6,
0.1,
0.2,
0.4
]