Skip to content

Commit

Permalink
runtime: add gc layout info for some basic types
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Oct 23, 2024
1 parent b8fe75a commit badf1dd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/reflect/gclayout_none.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !gc.precise

package reflect

import "unsafe"

func (r *rawType) gcLayout() unsafe.Pointer {
return nil
}
37 changes: 37 additions & 0 deletions src/reflect/gclayout_precise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build gc.precise

package reflect

import "unsafe"

var (
// Constants for use with alloc()
// See runtime/gc_precise.go
// pppppppp_pppppppp_pppppppp_ppsssss1
gcLayoutNoPtrs = unsafe.Pointer(uintptr(0b0000_00001_1))
gcLayoutPointer = unsafe.Pointer(uintptr(0b0001_00001_1))
gcLayoutString = unsafe.Pointer(uintptr(0b0001_00010_1))
gcLayoutSlice = unsafe.Pointer(uintptr(0b0001_00011_1))
)

func (r *rawType) gcLayout() unsafe.Pointer {

if r.Kind() < String {
return gcLayoutNoPtrs
}

if r.Kind() == Pointer || r.Kind() == UnsafePointer {
return gcLayoutPointer
}

if r.Kind() == String {
return gcLayoutString
}

if r.Kind() == Slice {
return gcLayoutSlice
}

// Unknown (for now); less the conservative pointer scanning handle it
return nil
}
7 changes: 5 additions & 2 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,8 @@ func MakeSlice(typ Type, len, cap int) Value {
ulen := uint(len)
ucap := uint(cap)
maxSize := (^uintptr(0)) / 2
elementSize := rtype.elem().Size()
elem := rtype.elem()
elementSize := elem.Size()
if elementSize > 1 {
maxSize /= uintptr(elementSize)
}
Expand All @@ -1493,7 +1494,9 @@ func MakeSlice(typ Type, len, cap int) Value {
var slice sliceHeader
slice.cap = uintptr(ucap)
slice.len = uintptr(ulen)
slice.data = alloc(size, nil)
layout := elem.gcLayout()

slice.data = alloc(size, layout)

return Value{
typecode: rtype,
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions src/runtime/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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}
}
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit badf1dd

Please sign in to comment.