]> Cypherpunks repositories - gostls13.git/commit
encoding/json: avoid work when unquoting strings, take 2
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 21 Aug 2019 16:22:24 +0000 (18:22 +0200)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 31 Oct 2019 12:02:57 +0000 (12:02 +0000)
commit54697702e435bddb69c0b76b25b3209c78d2120a
tree063455bfd8d9e140e2f9ef9f35e0d809dbd990e6
parent10855608bc9361aec0f17f22bf24313a3d07ec85
encoding/json: avoid work when unquoting strings, take 2

This is a re-submission of CL 151157, since it was reverted in CL 190909
due to an introduced crash found by a fuzzer. The revert CL included
regression tests, while this CL includes a fixed version of the original
change.

In particular, what we forgot in the original optimization was that we
still need the length and trailing quote checks at the beginning of
unquoteBytes. Without those, we could end up in a crash later on.

We can work out how many bytes can be unquoted trivially in
rescanLiteral, which already iterates over a string's bytes.

Removing the extra loop in unquoteBytes simplifies the function and
speeds it up, especially when decoding simple strings, which are common.

While at it, we can remove the check that s[0]=='"', since all call
sites already meet that condition.

name           old time/op    new time/op    delta
CodeDecoder-8    10.6ms ± 2%    10.5ms ± 1%  -1.01%  (p=0.004 n=20+10)

name           old speed      new speed      delta
CodeDecoder-8   183MB/s ± 2%   185MB/s ± 1%  +1.02%  (p=0.003 n=20+10)

Updates #28923.

Change-Id: I8c6b13302bcd86a364bc998d72451332c0809cde
Reviewed-on: https://go-review.googlesource.com/c/go/+/190659
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
src/encoding/json/decode.go