diff --git a/decode.go b/decode.go index 3f9fe1f..59b6fd1 100644 --- a/decode.go +++ b/decode.go @@ -361,6 +361,52 @@ func (d *decodeState) scanWhile(op int) { d.opcode = d.scan.eof() } +// rescanLiteral is similar to scanWhile(scanContinue), but it specialises the +// common case where we're decoding a literal. The decoder scans the input +// twice, once for syntax errors and to check the length of the value, and the +// second to perform the decoding. +// +// Only in the second step do we use decodeState to tokenize literals, so we +// know there aren't any syntax errors. We can take advantage of that knowledge, +// and scan a literal's bytes much more quickly. +func (d *decodeState) rescanLiteral() { + data, i := d.data, d.off +Switch: + switch data[i-1] { + case '"': // string + for ; i < len(data); i++ { + switch data[i] { + case '\\': + i++ // escaped char + case '"': + i++ // tokenize the closing quote too + break Switch + } + } + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-': // number + for ; i < len(data); i++ { + switch data[i] { + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + '.', 'e', 'E', '+', '-': + default: + break Switch + } + } + case 't': // true + i += len("rue") + case 'f': // false + i += len("alse") + case 'n': // null + i += len("ull") + } + if i < len(data) { + d.opcode = stateEndValue(&d.scan, data[i]) + } else { + d.opcode = scanEnd + } + d.off = i + 1 +} + // value consumes a JSON value from d.data[d.off-1:], decoding into v, and // reads the following byte ahead. If v is invalid, the value is discarded. // The first byte of the value has been read already. @@ -392,7 +438,7 @@ func (d *decodeState) value(v reflect.Value) error { case scanBeginLiteral: // All bytes inside literal return scanContinue op code. start := d.readIndex() - d.scanWhile(scanContinue) + d.rescanLiteral() if v.IsValid() { if err := d.literalStore(d.data[start:d.readIndex()], v, false); err != nil { @@ -677,7 +723,7 @@ func (d *decodeState) object(v reflect.Value) error { // Read key. start := d.readIndex() - d.scanWhile(scanContinue) + d.rescanLiteral() item := d.data[start:d.readIndex()] key, ok := unquoteBytes(item) if !ok { @@ -1083,7 +1129,7 @@ func (d *decodeState) objectInterface() map[string]interface{} { // Read string key. start := d.readIndex() - d.scanWhile(scanContinue) + d.rescanLiteral() item := d.data[start:d.readIndex()] key, ok := unquote(item) if !ok { @@ -1122,7 +1168,7 @@ func (d *decodeState) objectInterface() map[string]interface{} { func (d *decodeState) literalInterface() interface{} { // All bytes inside literal return scanContinue op code. start := d.readIndex() - d.scanWhile(scanContinue) + d.rescanLiteral() item := d.data[start:d.readIndex()]