Improve performance at decoding

This commit is contained in:
Masaaki Goshima 2020-04-24 14:07:33 +09:00
parent 508e4e4dcc
commit a523e61c20
4 changed files with 67 additions and 54 deletions

View File

@ -2,7 +2,6 @@ package json
import (
"errors"
"math"
)
type intDecoder struct {
@ -13,6 +12,13 @@ func newIntDecoder(op func(uintptr, int64)) *intDecoder {
return &intDecoder{op: op}
}
var (
pow10i64 = [...]int64{
1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18,
}
)
func (d *intDecoder) parseInt(b []byte) int64 {
isNegative := false
if b[0] == '-' {
@ -23,7 +29,7 @@ func (d *intDecoder) parseInt(b []byte) int64 {
sum := int64(0)
for i := 0; i < maxDigit; i++ {
c := int64(b[i]) - 48
digitValue := int64(math.Pow10(maxDigit - i - 1))
digitValue := pow10i64[maxDigit-i-1]
sum += c * digitValue
}
if isNegative {
@ -33,25 +39,27 @@ func (d *intDecoder) parseInt(b []byte) int64 {
}
func (d *intDecoder) decodeByte(ctx *context) ([]byte, error) {
ctx.skipWhiteSpace()
buf := ctx.buf
cursor := ctx.cursor
buflen := ctx.buflen
switch buf[cursor] {
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := ctx.cursor
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if int('0') <= tk && tk <= int('9') {
continue
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if int('0') <= tk && tk <= int('9') {
continue
}
break
}
break
num := ctx.buf[start:cursor]
ctx.cursor = cursor
return num, nil
}
num := ctx.buf[start:cursor]
ctx.cursor = cursor
//fmt.Printf("number = [%s]\n", string(num))
return num, nil
}
return nil, errors.New("unexpected error number")
}

View File

@ -22,30 +22,31 @@ func (d *stringDecoder) decode(ctx *context, p uintptr) error {
}
func (d *stringDecoder) decodeByte(ctx *context) ([]byte, error) {
ctx.skipWhiteSpace()
buf := ctx.buf
cursor := ctx.cursor
buflen := ctx.buflen
if buf[cursor] != '"' {
return nil, errors.New("unexpected error key delimiter")
}
start := cursor + 1
cursor++
cursor := ctx.cursor
for ; cursor < buflen; cursor++ {
tk := buf[cursor]
if tk == '\\' {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
}
if tk == '"' {
break
case '"':
cursor++
start := cursor
for ; cursor < buflen; cursor++ {
tk := buf[cursor]
if tk == '\\' {
cursor++
continue
}
if tk == '"' {
literal := buf[start:cursor]
cursor++
ctx.cursor = cursor
return literal, nil
}
}
return nil, errors.New("unexpected error string")
}
}
if buf[cursor] != '"' {
return nil, errors.New("unexpected error string")
}
literal := buf[start:cursor]
//fmt.Printf("string = [%s]\n", string(literal))
cursor++
ctx.cursor = cursor
return literal, nil
return nil, errors.New("unexpected error key delimiter")
}

View File

@ -81,7 +81,6 @@ func (d *structDecoder) decode(ctx *context, p uintptr) error {
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if exists {
//fmt.Printf("k = %s dec = %#v, p = %x\n", k, field.dec, p)
if err := field.dec.decode(ctx, p+field.offset); err != nil {
return err
}

View File

@ -2,8 +2,6 @@ package json
import (
"errors"
"fmt"
"math"
)
type uintDecoder struct {
@ -14,37 +12,44 @@ func newUintDecoder(op func(uintptr, uint64)) *uintDecoder {
return &uintDecoder{op: op}
}
var pow10u64 = [...]uint64{
1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
}
func (d *uintDecoder) parseUint(b []byte) uint64 {
maxDigit := len(b)
sum := uint64(0)
for i := 0; i < maxDigit; i++ {
c := uint64(b[i]) - 48
digitValue := uint64(math.Pow10(maxDigit - i - 1))
digitValue := pow10u64[maxDigit-i-1]
sum += c * digitValue
}
return sum
}
func (d *uintDecoder) decodeByte(ctx *context) ([]byte, error) {
ctx.skipWhiteSpace()
buf := ctx.buf
buflen := ctx.buflen
cursor := ctx.cursor
switch buf[cursor] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if int('0') <= tk && tk <= int('9') {
continue
for ; cursor < buflen; cursor++ {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor
cursor++
for ; cursor < buflen; cursor++ {
tk := int(buf[cursor])
if int('0') <= tk && tk <= int('9') {
continue
}
break
}
break
num := buf[start:cursor]
ctx.cursor = cursor
return num, nil
}
num := buf[start:cursor]
fmt.Printf("number = [%s]\n", string(num))
ctx.cursor = cursor
return num, nil
}
return nil, errors.New("unexpected error number")
}