You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
optimize read float
This commit is contained in:
38
jsoniter.go
38
jsoniter.go
@ -133,6 +133,8 @@ func (iter *Iterator) readByte() (ret byte) {
|
|||||||
ret = iter.buf[iter.head]
|
ret = iter.buf[iter.head]
|
||||||
iter.head++
|
iter.head++
|
||||||
return ret
|
return ret
|
||||||
|
} else {
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret = iter.buf[iter.head]
|
ret = iter.buf[iter.head]
|
||||||
@ -557,17 +559,27 @@ func (iter *Iterator) readObjectField() (ret string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) ReadFloat32() (ret float32) {
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||||
str := make([]byte, 0, 4)
|
strBuf := [8]byte{}
|
||||||
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() {
|
str := strBuf[0:0]
|
||||||
|
hasMore := true
|
||||||
|
for(hasMore) {
|
||||||
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
|
c := iter.buf[i]
|
||||||
switch c {
|
switch c {
|
||||||
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
str = append(str, c)
|
str = append(str, c)
|
||||||
continue
|
continue
|
||||||
default:
|
default:
|
||||||
iter.unreadByte()
|
hasMore = false
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if hasMore {
|
||||||
|
if !iter.loadMore() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -580,17 +592,27 @@ func (iter *Iterator) ReadFloat32() (ret float32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (iter *Iterator) ReadFloat64() (ret float64) {
|
func (iter *Iterator) ReadFloat64() (ret float64) {
|
||||||
str := make([]byte, 0, 4)
|
strBuf := [8]byte{}
|
||||||
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() {
|
str := strBuf[0:0]
|
||||||
|
hasMore := true
|
||||||
|
for(hasMore) {
|
||||||
|
for i := iter.head; i < iter.tail; i++ {
|
||||||
|
c := iter.buf[i]
|
||||||
switch c {
|
switch c {
|
||||||
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
str = append(str, c)
|
str = append(str, c)
|
||||||
continue
|
continue
|
||||||
default:
|
default:
|
||||||
iter.unreadByte()
|
hasMore = false
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if hasMore {
|
||||||
|
if !iter.loadMore() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if iter.Error != nil && iter.Error != io.EOF {
|
if iter.Error != nil && iter.Error != io.EOF {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package jsoniter
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_float64_0(t *testing.T) {
|
func Test_float64_0(t *testing.T) {
|
||||||
@ -25,13 +26,15 @@ func Test_float32_1_dot_1_comma(t *testing.T) {
|
|||||||
iter := ParseString(`1.1,`)
|
iter := ParseString(`1.1,`)
|
||||||
val := iter.ReadFloat32()
|
val := iter.ReadFloat32()
|
||||||
if val != 1.1 {
|
if val != 1.1 {
|
||||||
|
fmt.Println(iter.Error)
|
||||||
t.Fatal(val)
|
t.Fatal(val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_float(b *testing.B) {
|
func Benchmark_jsoniter_float(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(`1.1`)
|
iter := ParseString(`1.1111111111`)
|
||||||
iter.ReadFloat64()
|
iter.ReadFloat64()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user