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 ( import (
"errors" "errors"
"math"
) )
type intDecoder struct { type intDecoder struct {
@ -13,6 +12,13 @@ func newIntDecoder(op func(uintptr, int64)) *intDecoder {
return &intDecoder{op: op} 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 { func (d *intDecoder) parseInt(b []byte) int64 {
isNegative := false isNegative := false
if b[0] == '-' { if b[0] == '-' {
@ -23,7 +29,7 @@ func (d *intDecoder) parseInt(b []byte) int64 {
sum := int64(0) sum := int64(0)
for i := 0; i < maxDigit; i++ { for i := 0; i < maxDigit; i++ {
c := int64(b[i]) - 48 c := int64(b[i]) - 48
digitValue := int64(math.Pow10(maxDigit - i - 1)) digitValue := pow10i64[maxDigit-i-1]
sum += c * digitValue sum += c * digitValue
} }
if isNegative { if isNegative {
@ -33,13 +39,15 @@ func (d *intDecoder) parseInt(b []byte) int64 {
} }
func (d *intDecoder) decodeByte(ctx *context) ([]byte, error) { func (d *intDecoder) decodeByte(ctx *context) ([]byte, error) {
ctx.skipWhiteSpace()
buf := ctx.buf buf := ctx.buf
cursor := ctx.cursor cursor := ctx.cursor
buflen := ctx.buflen buflen := ctx.buflen
for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := ctx.cursor start := cursor
cursor++ cursor++
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
tk := int(buf[cursor]) tk := int(buf[cursor])
@ -50,9 +58,9 @@ func (d *intDecoder) decodeByte(ctx *context) ([]byte, error) {
} }
num := ctx.buf[start:cursor] num := ctx.buf[start:cursor]
ctx.cursor = cursor ctx.cursor = cursor
//fmt.Printf("number = [%s]\n", string(num))
return num, nil return num, nil
} }
}
return nil, errors.New("unexpected error number") 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) { func (d *stringDecoder) decodeByte(ctx *context) ([]byte, error) {
ctx.skipWhiteSpace()
buf := ctx.buf buf := ctx.buf
cursor := ctx.cursor
buflen := ctx.buflen buflen := ctx.buflen
if buf[cursor] != '"' { cursor := ctx.cursor
return nil, errors.New("unexpected error key delimiter") for ; cursor < buflen; cursor++ {
} switch buf[cursor] {
start := cursor + 1 case ' ', '\n', '\t', '\r':
continue
case '"':
cursor++ cursor++
start := cursor
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
tk := buf[cursor] tk := buf[cursor]
if tk == '\\' { if tk == '\\' {
cursor++
continue continue
} }
if tk == '"' { if tk == '"' {
break
}
}
if buf[cursor] != '"' {
return nil, errors.New("unexpected error string")
}
literal := buf[start:cursor] literal := buf[start:cursor]
//fmt.Printf("string = [%s]\n", string(literal))
cursor++ cursor++
ctx.cursor = cursor ctx.cursor = cursor
return literal, nil return literal, nil
}
}
return nil, errors.New("unexpected error string")
}
}
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)) k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k] field, exists := d.fieldMap[k]
if exists { 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 { if err := field.dec.decode(ctx, p+field.offset); err != nil {
return err return err
} }

View File

@ -2,8 +2,6 @@ package json
import ( import (
"errors" "errors"
"fmt"
"math"
) )
type uintDecoder struct { type uintDecoder struct {
@ -14,23 +12,30 @@ func newUintDecoder(op func(uintptr, uint64)) *uintDecoder {
return &uintDecoder{op: op} 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 { func (d *uintDecoder) parseUint(b []byte) uint64 {
maxDigit := len(b) maxDigit := len(b)
sum := uint64(0) sum := uint64(0)
for i := 0; i < maxDigit; i++ { for i := 0; i < maxDigit; i++ {
c := uint64(b[i]) - 48 c := uint64(b[i]) - 48
digitValue := uint64(math.Pow10(maxDigit - i - 1)) digitValue := pow10u64[maxDigit-i-1]
sum += c * digitValue sum += c * digitValue
} }
return sum return sum
} }
func (d *uintDecoder) decodeByte(ctx *context) ([]byte, error) { func (d *uintDecoder) decodeByte(ctx *context) ([]byte, error) {
ctx.skipWhiteSpace()
buf := ctx.buf buf := ctx.buf
buflen := ctx.buflen buflen := ctx.buflen
cursor := ctx.cursor cursor := ctx.cursor
for ; cursor < buflen; cursor++ {
switch buf[cursor] { switch buf[cursor] {
case ' ', '\n', '\t', '\r':
continue
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor start := cursor
cursor++ cursor++
@ -42,10 +47,10 @@ func (d *uintDecoder) decodeByte(ctx *context) ([]byte, error) {
break break
} }
num := buf[start:cursor] num := buf[start:cursor]
fmt.Printf("number = [%s]\n", string(num))
ctx.cursor = cursor ctx.cursor = cursor
return num, nil return num, nil
} }
}
return nil, errors.New("unexpected error number") return nil, errors.New("unexpected error number")
} }