Skip to content

Commit

Permalink
WIP: add layout for strings and slices
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Oct 3, 2024
1 parent bcd4c6b commit 6db7b16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 6db7b16

Please sign in to comment.