From a523e61c2096a971265a8d227a60cc30dce62709 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 24 Apr 2020 14:07:33 +0900 Subject: [PATCH] Improve performance at decoding --- decode_int.go | 40 ++++++++++++++++++++++++---------------- decode_string.go | 41 +++++++++++++++++++++-------------------- decode_struct.go | 1 - decode_uint.go | 39 ++++++++++++++++++++++----------------- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/decode_int.go b/decode_int.go index 50df969..66d8723 100644 --- a/decode_int.go +++ b/decode_int.go @@ -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") } diff --git a/decode_string.go b/decode_string.go index 15ffa4e..44312d4 100644 --- a/decode_string.go +++ b/decode_string.go @@ -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") } diff --git a/decode_struct.go b/decode_struct.go index 997e93a..c5b0535 100644 --- a/decode_struct.go +++ b/decode_struct.go @@ -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 } diff --git a/decode_uint.go b/decode_uint.go index 967b741..3b23335 100644 --- a/decode_uint.go +++ b/decode_uint.go @@ -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") }