Skip to content

Commit 880c967

Browse files
committed
runtime: minor string/rune optimizations
Eliminate a spill in concatstrings. Provide bounds elim hints in runetochar. No significant benchmark movement. Before: "".runetochar t=1 size=412 args=0x28 locals=0x0 "".concatstrings t=1 size=736 args=0x30 locals=0x98 After: "".runetochar t=1 size=337 args=0x28 locals=0x0 "".concatstrings t=1 size=711 args=0x30 locals=0x90 Change-Id: Icce646976cb20a223163b7e72a54761193ac17e3 Reviewed-on: https://go-review.googlesource.com/27460 Run-TryBot: Josh Bleecher Snyder <[email protected]> Reviewed-by: Martin Möhrmann <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent fa8a28d commit 880c967

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/runtime/rune.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func runetochar(str []byte, r rune) int {
178178
* 0080-07FF => t2 tx
179179
*/
180180
if c <= rune2 {
181+
_ = str[1]
181182
str[0] = byte(t2 | (c >> (1 * bitx)))
182183
str[1] = byte(tx | (c & maskx))
183184
return 2
@@ -201,6 +202,7 @@ func runetochar(str []byte, r rune) int {
201202
* 0800-FFFF => t3 tx tx
202203
*/
203204
if c <= rune3 {
205+
_ = str[2]
204206
str[0] = byte(t3 | (c >> (2 * bitx)))
205207
str[1] = byte(tx | ((c >> (1 * bitx)) & maskx))
206208
str[2] = byte(tx | (c & maskx))
@@ -211,6 +213,7 @@ func runetochar(str []byte, r rune) int {
211213
* four character sequence (21-bit value)
212214
* 10000-1FFFFF => t4 tx tx tx
213215
*/
216+
_ = str[3]
214217
str[0] = byte(t4 | (c >> (3 * bitx)))
215218
str[1] = byte(tx | ((c >> (2 * bitx)) & maskx))
216219
str[2] = byte(tx | ((c >> (1 * bitx)) & maskx))

src/runtime/string.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ func concatstrings(buf *tmpBuf, a []string) string {
4747
return a[idx]
4848
}
4949
s, b := rawstringtmp(buf, l)
50-
l = 0
5150
for _, x := range a {
52-
copy(b[l:], x)
53-
l += len(x)
51+
copy(b, x)
52+
b = b[len(x):]
5453
}
5554
return s
5655
}

0 commit comments

Comments
 (0)