Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d205438

Browse files
committed
unicode/utf8: make DecodeRune{,InString} inlineable
This change makes the fast path for ASCII characters inlineable in DecodeRune and DecodeRuneInString. Here are some benchmark results (no change to allocations): goos: darwin goarch: amd64 pkg: unicode/utf8 cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz │ old │ new │ │ sec/op │ sec/op vs base │ DecodeASCIIRune-8 2.4585n ± 1% 0.6187n ± 1% -74.83% (p=0.000 n=20) DecodeJapaneseRune-8 3.987n ± 1% 3.984n ± 1% ~ (p=0.941 n=20) DecodeASCIIRuneInString-8 2.2945n ± 1% 0.3123n ± 2% -86.39% (p=0.000 n=20) DecodeJapaneseRuneInString-8 3.821n ± 1% 3.983n ± 1% +4.25% (p=0.000 n=20) geomean 3.045n 1.323n -56.53% Note: when #61502 gets resolved, this change should likely be reverted. Updates #48195
1 parent d029eaa commit d205438

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

‎src/cmd/compile/internal/test/inl_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func TestIntendedInlining(t *testing.T) {
125125
"assemble64",
126126
},
127127
"unicode/utf8": {
128+
"DecodeRune",
129+
"DecodeRuneInString",
128130
"FullRune",
129131
"FullRuneInString",
130132
"RuneLen",

‎src/unicode/utf8/utf8.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ func FullRuneInString(s string) bool {
155155
// out of range, or is not the shortest possible UTF-8 encoding for the
156156
// value. No other validation is performed.
157157
func DecodeRune(p []byte) (r rune, size int) {
158+
// Inlineable fast path for ASCII characters; see #48195.
159+
// This implementation is weird but effective at rendering the
160+
// function inlineable.
161+
for _, b := range p {
162+
if b < RuneSelf {
163+
return rune(b), 1
164+
}
165+
break
166+
}
167+
r, size = decodeRune(p)
168+
return
169+
}
170+
171+
func decodeRune(p []byte) (r rune, size int) {
158172
n := len(p)
159173
if n < 1 {
160174
return RuneError, 0
@@ -203,6 +217,18 @@ func DecodeRune(p []byte) (r rune, size int) {
203217
// out of range, or is not the shortest possible UTF-8 encoding for the
204218
// value. No other validation is performed.
205219
func DecodeRuneInString(s string) (r rune, size int) {
220+
// Inlineable fast path for ASCII characters; see #48195.
221+
// This implementation is a bit weird but effective at rendering the
222+
// function inlineable.
223+
if s != "" && s[0] < RuneSelf {
224+
return rune(s[0]), 1
225+
} else {
226+
r, size = decodeRuneInString(s)
227+
}
228+
return
229+
}
230+
231+
func decodeRuneInString(s string) (rune, int) {
206232
n := len(s)
207233
if n < 1 {
208234
return RuneError, 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /