From fd41951c2b243a80e7b2b5d28a90839c928cfd2e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Mo=CC=88hrmann?= Date: Sat, 3 Sep 2016 18:39:25 +0200 Subject: [PATCH] unicode/utf8: reduce bounds checks in EncodeRune MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Provide bounds elim hints in EncodeRune. name old time/op new time/op delta EncodeASCIIRune-4 2.69ns ± 2% 2.69ns ± 2% ~ (p=0.193 n=47+46) EncodeJapaneseRune-4 5.97ns ± 2% 5.38ns ± 2% -9.93% (p=0.000 n=49+50) Change-Id: I1a6dcffff3bdd64ab93c2130021e3b00981de4c8 Reviewed-on: https://go-review.googlesource.com/28492 Reviewed-by: Josh Bleecher Snyder Reviewed-by: Joe Tsai Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot --- src/unicode/utf8/utf8.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/unicode/utf8/utf8.go b/src/unicode/utf8/utf8.go index 9d35be6c06..2ff79f6683 100644 --- a/src/unicode/utf8/utf8.go +++ b/src/unicode/utf8/utf8.go @@ -347,6 +347,7 @@ func EncodeRune(p []byte, r rune) int { p[0] = byte(r) return 1 case i <= rune2Max: + _ = p[1] // eliminate bounds checks p[0] = t2 | byte(r>>6) p[1] = tx | byte(r)&maskx return 2 @@ -354,11 +355,13 @@ func EncodeRune(p []byte, r rune) int { r = RuneError fallthrough case i <= rune3Max: + _ = p[2] // eliminate bounds checks p[0] = t3 | byte(r>>12) p[1] = tx | byte(r>>6)&maskx p[2] = tx | byte(r)&maskx return 3 default: + _ = p[3] // eliminate bounds checks p[0] = t4 | byte(r>>18) p[1] = tx | byte(r>>12)&maskx p[2] = tx | byte(r>>6)&maskx -- 2.48.1