diff --git a/src/reflect/value.go b/src/reflect/value.go index 15a900f9e1..06e9ccadce 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -1493,7 +1493,11 @@ func MakeSlice(typ Type, len, cap int) Value { var slice sliceHeader slice.cap = uintptr(ucap) slice.len = uintptr(ulen) - slice.data = alloc(size, nil) + var layout unsafe.Pointer + if elementSize == 1 { + layout = unsafe.Pointer(uintptr(0x3)) + } + slice.data = alloc(size, layout) return Value{ typecode: rtype, diff --git a/src/runtime/slice.go b/src/runtime/slice.go index 7d804b11be..05d23e7198 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -47,7 +47,13 @@ func sliceGrow(oldBuf unsafe.Pointer, oldLen, oldCap, newCap, elemSize uintptr) // memory allocators, this causes some difficult to debug issues. newCap = 1 << bits.Len(uint(newCap)) - buf := alloc(newCap*elemSize, nil) + var layout unsafe.Pointer + if elemSize == 1 { + // []byte + layout = gcLayoutNoPtrs + } + + buf := alloc(newCap*elemSize, layout) if oldLen > 0 { // copy any data to new slice memmove(buf, oldBuf, oldLen*elemSize) diff --git a/src/runtime/string.go b/src/runtime/string.go index aeefe1d4fa..bab19595d2 100644 --- a/src/runtime/string.go +++ b/src/runtime/string.go @@ -32,6 +32,11 @@ func stringEqual(x, y string) bool { return true } +var ( + // Constants for use with alloc() + gcLayoutNoPtrs = unsafe.Pointer(uintptr(0x3)) +) + // Return true iff x < y. // //go:nobounds @@ -59,7 +64,7 @@ func stringConcat(x, y _string) _string { return x } else { length := x.length + y.length - buf := alloc(length, nil) + buf := alloc(length, gcLayoutNoPtrs) memcpy(buf, unsafe.Pointer(x.ptr), x.length) memcpy(unsafe.Add(buf, x.length), unsafe.Pointer(y.ptr), y.length) return _string{ptr: (*byte)(buf), length: length} @@ -72,7 +77,7 @@ func stringFromBytes(x struct { len uintptr cap uintptr }) _string { - buf := alloc(x.len, nil) + buf := alloc(x.len, gcLayoutNoPtrs) memcpy(buf, unsafe.Pointer(x.ptr), x.len) return _string{ptr: (*byte)(buf), length: x.len} } @@ -83,7 +88,7 @@ func stringToBytes(x _string) (slice struct { len uintptr cap uintptr }) { - buf := alloc(x.length, nil) + buf := alloc(x.length, gcLayoutNoPtrs) memcpy(buf, unsafe.Pointer(x.ptr), x.length) slice.ptr = (*byte)(buf) slice.len = x.length @@ -100,7 +105,7 @@ func stringFromRunes(runeSlice []rune) (s _string) { } // Allocate memory for the string. - s.ptr = (*byte)(alloc(s.length, nil)) + s.ptr = (*byte)(alloc(s.length, gcLayoutNoPtrs)) // Encode runes to UTF-8 and store the resulting bytes in the string. index := uintptr(0)