go/src/encoding
Shengyu Zhang 88b1d6115a encoding/json: prevent duplicate slicebytetostring
When storing literal to JSON number v, if s is valid number, the slicebytetostring operation will be performed twice. In fact, the operation is unavoidable on any code path, so just perform it at the very beginning.

This is not a big optimization, but better than nothing:

    $ ../bin/go test ./encoding/json/ -bench UnmarshalNumber -run NOTEST -benchtime 10000000x -count 16  > old.txt
    $ ../bin/go test ./encoding/json/ -bench UnmarshalNumber -run NOTEST -benchtime 10000000x -count 16  > new.txt
    $ benchstat old.txt new.txt
                      │   old.txt   │              new.txt               │
                      │   sec/op    │   sec/op     vs base               │
    UnmarshalNumber-8   234.5n ± 3%   228.2n ± 4%  -2.67% (p=0.033 n=16)

                      │  old.txt   │            new.txt             │
                      │    B/op    │    B/op     vs base            │
    UnmarshalNumber-8   168.0 ± 0%   168.0 ± 0%  ~ (p=1.000 n=16) ¹
    ¹ all samples are equal

                      │  old.txt   │            new.txt             │
                      │ allocs/op  │ allocs/op   vs base            │
    UnmarshalNumber-8   2.000 ± 0%   2.000 ± 0%  ~ (p=1.000 n=16) ¹
    ¹ all samples are equal

Change-Id: I1dfdb1ed0883e385f753b2046b7f047c792aa4e3
GitHub-Last-Rev: d236dd7265
GitHub-Pull-Request: golang/go#61242
Reviewed-on: https://go-review.googlesource.com/c/go/+/508556
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-04-02 15:00:17 +00:00
..
ascii85 encoding: modernize Go documentation 2023-09-08 19:04:28 +00:00
asn1 encoding/asn1: simplify appendFourDigits 2024-03-28 18:16:09 +00:00
base32 encoding: modernize Go documentation 2023-09-08 19:04:28 +00:00
base64 encoding: modernize Go documentation 2023-09-08 19:04:28 +00:00
binary encoding/binary: cache struct sizes to speed up Read and Write for slice of structs. 2024-03-13 18:32:53 +00:00
csv encoding: modernize Go documentation 2023-09-08 19:04:28 +00:00
gob encoding/gob: close files used in dump.go and encgen.go 2024-03-22 16:39:09 +00:00
hex encoding: modernize Go documentation 2023-09-08 19:04:28 +00:00
json encoding/json: prevent duplicate slicebytetostring 2024-04-02 15:00:17 +00:00
pem encoding: modernize Go documentation 2023-09-08 19:04:28 +00:00
xml all: fix a large number of comments 2024-03-26 19:58:28 +00:00
encoding.go encoding: document when marshaling methods can be added 2023-05-23 19:43:37 +00:00