Skip to content

encoding/json: remove redundant c <= ' ' check in isSpace #73552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/encoding/json/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (s *scanner) popParseState() {
}

func isSpace(c byte) bool {
return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n')
return c == ' ' || c == '\t' || c == '\r' || c == '\n'
}

// stateBeginValueOrEmpty is the state after reading `[`.
Expand Down
38 changes: 38 additions & 0 deletions src/encoding/json/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,41 @@ func genMap(n int) map[string]any {
}
return x
}

var (
isSpaceRes bool

nonPrintable = []byte{0x01, 0x08}
printableNonSpace = []byte{'a', 'z', '1', 'X'}
spaceChars = []byte{' ', '\t', '\r', '\n'}
)

func BenchmarkIsSpace_NonPrintable(b *testing.B) {
var r bool
for i := 0; i < b.N; i++ {
for _, c := range nonPrintable {
r = isSpace(c)
}
}
isSpaceRes = r
}

func BenchmarkIsSpace_PrintableNonSpace(b *testing.B) {
var r bool
for i := 0; i < b.N; i++ {
for _, c := range printableNonSpace {
r = isSpace(c)
}
}
isSpaceRes = r
}

func BenchmarkIsSpace_SpaceChars(b *testing.B) {
var r bool
for i := 0; i < b.N; i++ {
for _, c := range spaceChars {
r = isSpace(c)
}
}
isSpaceRes = r
}