From a1103dcc27b9c85800624367ebb89ef46d4307af Mon Sep 17 00:00:00 2001 From: codesoap Date: Wed, 26 Feb 2020 17:36:01 +0000 Subject: [PATCH] encoding/json: consolidate the isSpace function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The new code is easier to read, and practically equivalent in terms of performance. name old time/op new time/op delta CodeUnmarshal-2 166ms ± 1% 166ms ± 1% ~ (p=0.863 n=11+10) CodeUnmarshalReuse-2 139ms ± 1% 139ms ± 1% ~ (p=0.050 n=10+12) UnmarshalString-2 1.08µs ± 1% 1.07µs ± 1% -0.64% (p=0.001 n=10+11) UnmarshalFloat64-2 1.01µs ± 1% 1.01µs ± 1% ~ (p=0.280 n=12+11) UnmarshalInt64-2 850ns ± 0% 851ns ± 0% ~ (p=0.455 n=11+12) name old speed new speed delta CodeUnmarshal-2 11.7MB/s ± 1% 11.7MB/s ± 1% ~ (p=0.904 n=11+10) CodeUnmarshalReuse-2 14.0MB/s ± 1% 14.0MB/s ± 1% +0.40% (p=0.041 n=10+12) name old alloc/op new alloc/op delta CodeUnmarshal-2 3.28MB ± 0% 3.28MB ± 0% ~ (p=0.907 n=10+11) CodeUnmarshalReuse-2 2.19MB ± 0% 2.19MB ± 0% ~ (p=0.306 n=12+12) UnmarshalString-2 192B ± 0% 192B ± 0% ~ (all equal) UnmarshalFloat64-2 180B ± 0% 180B ± 0% ~ (all equal) UnmarshalInt64-2 176B ± 0% 176B ± 0% ~ (all equal) name old allocs/op new allocs/op delta CodeUnmarshal-2 92.7k ± 0% 92.7k ± 0% ~ (all equal) CodeUnmarshalReuse-2 80.4k ± 0% 80.4k ± 0% ~ (all equal) UnmarshalString-2 2.00 ± 0% 2.00 ± 0% ~ (all equal) UnmarshalFloat64-2 2.00 ± 0% 2.00 ± 0% ~ (all equal) UnmarshalInt64-2 1.00 ± 0% 1.00 ± 0% ~ (all equal) Change-Id: I6d5a48c624d436551409a17c21542e26d29e26b3 GitHub-Last-Rev: 7d81961688b5ee3a7e4718188c0eaf3413521f97 GitHub-Pull-Request: golang/go#37385 Reviewed-on: https://go-review.googlesource.com/c/go/+/220581 Reviewed-by: Daniel Martí Reviewed-by: Joe Tsai Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot --- src/encoding/json/scanner.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/encoding/json/scanner.go b/src/encoding/json/scanner.go index 975b2bf80f..9dc1903e2d 100644 --- a/src/encoding/json/scanner.go +++ b/src/encoding/json/scanner.go @@ -195,12 +195,12 @@ func (s *scanner) popParseState() { } func isSpace(c byte) bool { - return c == ' ' || c == '\t' || c == '\r' || c == '\n' + return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n') } // stateBeginValueOrEmpty is the state after reading `[`. func stateBeginValueOrEmpty(s *scanner, c byte) int { - if c <= ' ' && isSpace(c) { + if isSpace(c) { return scanSkipSpace } if c == ']' { @@ -211,7 +211,7 @@ func stateBeginValueOrEmpty(s *scanner, c byte) int { // stateBeginValue is the state at the beginning of the input. func stateBeginValue(s *scanner, c byte) int { - if c <= ' ' && isSpace(c) { + if isSpace(c) { return scanSkipSpace } switch c { @@ -249,7 +249,7 @@ func stateBeginValue(s *scanner, c byte) int { // stateBeginStringOrEmpty is the state after reading `{`. func stateBeginStringOrEmpty(s *scanner, c byte) int { - if c <= ' ' && isSpace(c) { + if isSpace(c) { return scanSkipSpace } if c == '}' { @@ -262,7 +262,7 @@ func stateBeginStringOrEmpty(s *scanner, c byte) int { // stateBeginString is the state after reading `{"key": value,`. func stateBeginString(s *scanner, c byte) int { - if c <= ' ' && isSpace(c) { + if isSpace(c) { return scanSkipSpace } if c == '"' { @@ -282,7 +282,7 @@ func stateEndValue(s *scanner, c byte) int { s.endTop = true return stateEndTop(s, c) } - if c <= ' ' && isSpace(c) { + if isSpace(c) { s.step = stateEndValue return scanSkipSpace } -- 2.50.0