diff options
author | Joe Tsai <joetsai@digital-static.net> | 2016-10-26 14:31:48 -0700 |
---|---|---|
committer | Joe Tsai <thebrokentoaster@gmail.com> | 2016-10-26 23:02:52 +0000 |
commit | 8eca08611ac1c65622400f526ab5b9065a4c9d67 (patch) | |
tree | c1e306c0ccb2db8404c7de3048ac0be281d1784a /src/unicode | |
parent | 3cfc757c625d8f9fabf2c52e2ae3a8eab3fbdf61 (diff) | |
download | go-8eca08611ac1c65622400f526ab5b9065a4c9d67.tar.gz go-8eca08611ac1c65622400f526ab5b9065a4c9d67.zip |
unicode/utf8: optimize ValidRune
Re-writing the switch statement as a single boolean expression
reduces the number of branches that the compiler generates.
It is also arguably easier to read as a pair of numeric ranges
that valid runes can exist in.
No test changes since the existing test does a good job of
testing all of the boundaries.
This change was to gain back some performance after a correctness
fix done in http://golang.org/cl/32123.
The correctness fix (CL/32123) slowed down the benchmarks slightly:
benchmark old ns/op new ns/op delta
BenchmarkIndexRune/10-4 19.3 21.6 +11.92%
BenchmarkIndexRune/32-4 33.6 35.2 +4.76%
Since the fix relies on utf8.ValidRune, this CL improves benchmarks:
benchmark old ns/op new ns/op delta
BenchmarkIndexRune/10-4 21.6 20.0 -7.41%
BenchmarkIndexRune/32-4 35.2 33.5 -4.83%
Change-Id: Ib1ca10a2e29c90e879a8ef9b7221c33e85d015d8
Reviewed-on: https://go-review.googlesource.com/32122
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/unicode')
-rw-r--r-- | src/unicode/utf8/utf8.go | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/unicode/utf8/utf8.go b/src/unicode/utf8/utf8.go index 2ff79f6683..6ccd464373 100644 --- a/src/unicode/utf8/utf8.go +++ b/src/unicode/utf8/utf8.go @@ -516,12 +516,10 @@ func ValidString(s string) bool { // Code points that are out of range or a surrogate half are illegal. func ValidRune(r rune) bool { switch { - case r < 0: - return false - case surrogateMin <= r && r <= surrogateMax: - return false - case r > MaxRune: - return false + case 0 <= r && r < surrogateMin: + return true + case surrogateMax < r && r <= MaxRune: + return true } - return true + return false } |