Skip to content

Commit 7ffbcd1

Browse files
cuonglmgopherbot
authored andcommitted
runtime: replace stringStruct with unsafe.String where appropriate
Simplify the code a bit, no significant performance changes. name old time/op new time/op delta HashStringSpeed-8 9.64ns ±11% 8.91ns ± 9% -7.60% (p=0.007 n=10+10) HashStringArraySpeed-8 19.8ns ± 3% 19.5ns ± 2% ~ (p=0.085 n=10+10) MapStringKeysEight_16-8 10.7ns ± 3% 10.2ns ± 2% -4.48% (p=0.000 n=10+10) MapStringKeysEight_32-8 8.89ns ± 3% 8.71ns ± 3% ~ (p=0.082 n=9+10) MapStringKeysEight_64-8 8.84ns ± 2% 8.60ns ± 3% -2.73% (p=0.004 n=9+10) MapStringKeysEight_1M-8 8.90ns ± 3% 8.62ns ± 3% -3.15% (p=0.000 n=10+10) MapStringConversion/32/simple-8 8.62ns ± 3% 8.61ns ± 2% ~ (p=0.895 n=10+10) MapStringConversion/32/struct-8 8.53ns ± 2% 8.63ns ± 2% ~ (p=0.123 n=10+10) MapStringConversion/32/array-8 8.54ns ± 2% 8.50ns ± 1% ~ (p=0.590 n=9+9) MapStringConversion/64/simple-8 8.44ns ± 2% 8.38ns ± 2% ~ (p=0.353 n=10+10) MapStringConversion/64/struct-8 8.41ns ± 2% 8.48ns ± 2% ~ (p=0.143 n=10+10) MapStringConversion/64/array-8 8.42ns ± 2% 8.44ns ± 2% ~ (p=0.739 n=10+10) MapInterfaceString-8 13.6ns ±26% 13.6ns ±20% ~ (p=0.736 n=10+9) AppendGrowString-8 38.9ms ± 9% 40.2ms ±13% ~ (p=0.481 n=10+10) CompareStringEqual-8 3.03ns ± 2% 2.86ns ± 3% -5.58% (p=0.000 n=10+10) CompareStringIdentical-8 1.20ns ± 3% 1.01ns ± 4% -16.16% (p=0.000 n=10+10) CompareStringSameLength-8 2.11ns ± 3% 1.85ns ± 3% -12.33% (p=0.000 n=10+10) CompareStringDifferentLength-8 0.30ns ± 0% 0.30ns ± 0% ~ (p=0.508 n=10+9) CompareStringBigUnaligned-8 43.0µs ± 1% 42.8µs ± 2% ~ (p=0.165 n=10+10) CompareStringBig-8 43.2µs ± 2% 43.4µs ± 2% ~ (p=0.661 n=9+10) ConcatStringAndBytes-8 15.1ns ± 1% 14.9ns ± 1% -1.57% (p=0.001 n=8+10) SliceByteToString/1-8 2.45ns ± 2% 2.39ns ± 2% -2.64% (p=0.000 n=10+10) SliceByteToString/2-8 10.9ns ± 2% 10.8ns ± 4% ~ (p=0.060 n=10+10) SliceByteToString/4-8 11.9ns ± 0% 11.8ns ± 1% -0.97% (p=0.000 n=8+8) SliceByteToString/8-8 13.9ns ± 1% 13.9ns ± 1% +0.57% (p=0.009 n=9+9) SliceByteToString/16-8 18.0ns ± 3% 18.6ns ± 5% +2.78% (p=0.001 n=9+10) SliceByteToString/32-8 20.1ns ± 3% 20.5ns ± 5% +2.10% (p=0.034 n=10+10) SliceByteToString/64-8 24.3ns ± 3% 24.9ns ± 3% +2.28% (p=0.001 n=9+10) SliceByteToString/128-8 33.8ns ± 1% 34.5ns ± 4% ~ (p=0.264 n=8+10) Updates #54854 Change-Id: I7ce57a92c5f590fa8cb31a48969d281147eb05f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/428759 Reviewed-by: hopehook <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent d36466f commit 7ffbcd1

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

src/runtime/debuglog.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,21 +292,19 @@ func (l *dlogger) s(x string) *dlogger {
292292
if !dlogEnabled {
293293
return l
294294
}
295-
str := stringStructOf(&x)
295+
296+
strData := unsafe.StringData(x)
296297
datap := &firstmoduledata
297-
if len(x) > 4 && datap.etext <= uintptr(str.str) && uintptr(str.str) < datap.end {
298+
if len(x) > 4 && datap.etext <= uintptr(unsafe.Pointer(strData)) && uintptr(unsafe.Pointer(strData)) < datap.end {
298299
// String constants are in the rodata section, which
299300
// isn't recorded in moduledata. But it has to be
300301
// somewhere between etext and end.
301302
l.w.byte(debugLogConstString)
302-
l.w.uvarint(uint64(str.len))
303-
l.w.uvarint(uint64(uintptr(str.str) - datap.etext))
303+
l.w.uvarint(uint64(len(x)))
304+
l.w.uvarint(uint64(uintptr(unsafe.Pointer(strData)) - datap.etext))
304305
} else {
305306
l.w.byte(debugLogString)
306-
var b []byte
307-
bb := (*slice)(unsafe.Pointer(&b))
308-
bb.array = str.str
309-
bb.len, bb.cap = str.len, str.len
307+
b := unsafe.Slice(strData, len(x))
310308
if len(b) > debugLogStringLimit {
311309
b = b[:debugLogStringLimit]
312310
}

src/runtime/heapdump.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ func dumpslice(b []byte) {
156156
}
157157

158158
func dumpstr(s string) {
159-
sp := stringStructOf(&s)
160-
dumpmemrange(sp.str, uintptr(sp.len))
159+
dumpmemrange(unsafe.Pointer(unsafe.StringData(s)), uintptr(len(s)))
161160
}
162161

163162
// dump information for a type
@@ -197,14 +196,12 @@ func dumptype(t *_type) {
197196
if x := t.uncommon(); x == nil || t.nameOff(x.pkgpath).name() == "" {
198197
dumpstr(t.string())
199198
} else {
200-
pkgpathstr := t.nameOff(x.pkgpath).name()
201-
pkgpath := stringStructOf(&pkgpathstr)
202-
namestr := t.name()
203-
name := stringStructOf(&namestr)
204-
dumpint(uint64(uintptr(pkgpath.len) + 1 + uintptr(name.len)))
205-
dwrite(pkgpath.str, uintptr(pkgpath.len))
199+
pkgpath := t.nameOff(x.pkgpath).name()
200+
name := t.name()
201+
dumpint(uint64(uintptr(len(pkgpath)) + 1 + uintptr(len(name))))
202+
dwrite(unsafe.Pointer(unsafe.StringData(pkgpath)), uintptr(len(pkgpath)))
206203
dwritebyte('.')
207-
dwrite(name.str, uintptr(name.len))
204+
dwrite(unsafe.Pointer(unsafe.StringData(name)), uintptr(len(name)))
208205
}
209206
dumpbool(t.kind&kindDirectIface == 0 || t.ptrdata != 0)
210207
}

src/runtime/string.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string {
7878
// n is the length of the slice.
7979
// Buf is a fixed-size buffer for the result,
8080
// it is not nil if the result does not escape.
81-
func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
81+
func slicebytetostring(buf *tmpBuf, ptr *byte, n int) string {
8282
if n == 0 {
8383
// Turns out to be a relatively common case.
8484
// Consider that you want to parse out data between parens in "foo()bar",
@@ -102,9 +102,7 @@ func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
102102
if goarch.BigEndian {
103103
p = add(p, 7)
104104
}
105-
stringStructOf(&str).str = p
106-
stringStructOf(&str).len = 1
107-
return
105+
return unsafe.String((*byte)(p), 1)
108106
}
109107

110108
var p unsafe.Pointer
@@ -113,16 +111,14 @@ func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
113111
} else {
114112
p = mallocgc(uintptr(n), nil, false)
115113
}
116-
stringStructOf(&str).str = p
117-
stringStructOf(&str).len = n
118114
memmove(p, unsafe.Pointer(ptr), uintptr(n))
119-
return
115+
return unsafe.String((*byte)(p), n)
120116
}
121117

122118
// stringDataOnStack reports whether the string's data is
123119
// stored on the current goroutine's stack.
124120
func stringDataOnStack(s string) bool {
125-
ptr := uintptr(stringStructOf(&s).str)
121+
ptr := uintptr(unsafe.Pointer(unsafe.StringData(s)))
126122
stk := getg().stack
127123
return stk.lo <= ptr && ptr < stk.hi
128124
}
@@ -151,7 +147,7 @@ func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
151147
// where k is []byte, T1 to Tn is a nesting of struct and array literals.
152148
// - Used for "<"+string(b)+">" concatenation where b is []byte.
153149
// - Used for string(b)=="foo" comparison where b is []byte.
154-
func slicebytetostringtmp(ptr *byte, n int) (str string) {
150+
func slicebytetostringtmp(ptr *byte, n int) string {
155151
if raceenabled && n > 0 {
156152
racereadrangepc(unsafe.Pointer(ptr),
157153
uintptr(n),
@@ -164,9 +160,7 @@ func slicebytetostringtmp(ptr *byte, n int) (str string) {
164160
if asanenabled && n > 0 {
165161
asanread(unsafe.Pointer(ptr), uintptr(n))
166162
}
167-
stringStructOf(&str).str = unsafe.Pointer(ptr)
168-
stringStructOf(&str).len = n
169-
return
163+
return unsafe.String(ptr, n)
170164
}
171165

172166
func stringtoslicebyte(buf *tmpBuf, s string) []byte {
@@ -271,13 +265,7 @@ func intstring(buf *[4]byte, v int64) (s string) {
271265
// b to set the string contents and then drop b.
272266
func rawstring(size int) (s string, b []byte) {
273267
p := mallocgc(uintptr(size), nil, false)
274-
275-
stringStructOf(&s).str = p
276-
stringStructOf(&s).len = size
277-
278-
*(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
279-
280-
return
268+
return unsafe.String((*byte)(p), size), unsafe.Slice((*byte)(p), size)
281269
}
282270

283271
// rawbyteslice allocates a new byte slice. The byte slice is not zeroed.

src/runtime/type.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,30 +454,24 @@ func (n name) readvarint(off int) (int, int) {
454454
}
455455
}
456456

457-
func (n name) name() (s string) {
457+
func (n name) name() string {
458458
if n.bytes == nil {
459459
return ""
460460
}
461461
i, l := n.readvarint(1)
462462
if l == 0 {
463463
return ""
464464
}
465-
hdr := (*stringStruct)(unsafe.Pointer(&s))
466-
hdr.str = unsafe.Pointer(n.data(1 + i))
467-
hdr.len = l
468-
return
465+
return unsafe.String(n.data(1+i), l)
469466
}
470467

471-
func (n name) tag() (s string) {
468+
func (n name) tag() string {
472469
if *n.data(0)&(1<<1) == 0 {
473470
return ""
474471
}
475472
i, l := n.readvarint(1)
476473
i2, l2 := n.readvarint(1 + i + l)
477-
hdr := (*stringStruct)(unsafe.Pointer(&s))
478-
hdr.str = unsafe.Pointer(n.data(1 + i + l + i2))
479-
hdr.len = l2
480-
return
474+
return unsafe.String(n.data(1+i+l+i2), l2)
481475
}
482476

483477
func (n name) pkgPath() string {

0 commit comments

Comments
 (0)