Skip to content

Commit 7336fb2

Browse files
committed
internal/gclayout: make gclayout values constants
The previous versions calculated at init() prevented `interp` from running in many cases, increasing compile times due to the increased need to revert the partially interpreted results and also increasing binary runtime because fewer optimizations had happened during interp.
1 parent 01f3d3e commit 7336fb2

File tree

4 files changed

+22
-31
lines changed

4 files changed

+22
-31
lines changed

src/internal/gclayout/gclayout.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,20 @@ import "unsafe"
55
// Internal constants for gc layout
66
// See runtime/gc_precise.go
77

8-
var (
9-
NoPtrs unsafe.Pointer
10-
Pointer unsafe.Pointer
11-
String unsafe.Pointer
12-
Slice unsafe.Pointer
13-
)
8+
type Layout uintptr
149

15-
func init() {
16-
var sizeBits uintptr
10+
const (
11+
// 16-bit int => bits = 4
12+
// 32-bit int => bits = 5
13+
// 64-bit int => bits = 6
14+
sizeBits = 4 + unsafe.Sizeof(uintptr(0))/4
1715

18-
switch unsafe.Sizeof(uintptr(0)) {
19-
case 8:
20-
sizeBits = 6
21-
case 4:
22-
sizeBits = 5
23-
case 2:
24-
sizeBits = 4
25-
}
16+
sizeShift = sizeBits + 1
2617

27-
var sizeShift = sizeBits + 1
18+
NoPtrs = Layout(uintptr(0b0<<sizeShift) | uintptr(0b1<<1) | uintptr(1))
19+
Pointer = Layout(uintptr(0b1<<sizeShift) | uintptr(0b1<<1) | uintptr(1))
20+
String = Layout(uintptr(0b01<<sizeShift) | uintptr(0b10<<1) | uintptr(1))
21+
Slice = Layout(uintptr(0b001<<sizeShift) | uintptr(0b11<<1) | uintptr(1))
22+
)
2823

29-
NoPtrs = unsafe.Pointer(uintptr(0b0<<sizeShift) | uintptr(0b1<<1) | uintptr(1))
30-
Pointer = unsafe.Pointer(uintptr(0b1<<sizeShift) | uintptr(0b1<<1) | uintptr(1))
31-
String = unsafe.Pointer(uintptr(0b01<<sizeShift) | uintptr(0b10<<1) | uintptr(1))
32-
Slice = unsafe.Pointer(uintptr(0b001<<sizeShift) | uintptr(0b11<<1) | uintptr(1))
33-
}
24+
func (l Layout) AsPtr() unsafe.Pointer { return unsafe.Pointer(l) }

src/internal/reflectlite/type.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,16 +708,16 @@ func (r *RawType) gcLayout() unsafe.Pointer {
708708
kind := r.Kind()
709709

710710
if kind < String {
711-
return gclayout.NoPtrs
711+
return gclayout.NoPtrs.AsPtr()
712712
}
713713

714714
switch kind {
715715
case Pointer, UnsafePointer, Chan, Map:
716-
return gclayout.Pointer
716+
return gclayout.Pointer.AsPtr()
717717
case String:
718-
return gclayout.String
718+
return gclayout.String.AsPtr()
719719
case Slice:
720-
return gclayout.Slice
720+
return gclayout.Slice.AsPtr()
721721
}
722722

723723
// Unknown (for now); let the conservative pointer scanning handle it

src/runtime/slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func sliceGrow(oldBuf unsafe.Pointer, oldLen, oldCap, newCap, elemSize uintptr)
5151
var layout unsafe.Pointer
5252
// less type info here; can only go off element size
5353
if elemSize < unsafe.Sizeof(uintptr(0)) {
54-
layout = gclayout.NoPtrs
54+
layout = gclayout.NoPtrs.AsPtr()
5555
}
5656

5757
buf := alloc(newCap*elemSize, layout)

src/runtime/string.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func stringConcat(x, y _string) _string {
6060
return x
6161
} else {
6262
length := x.length + y.length
63-
buf := alloc(length, gclayout.NoPtrs)
63+
buf := alloc(length, gclayout.NoPtrs.AsPtr())
6464
memcpy(buf, unsafe.Pointer(x.ptr), x.length)
6565
memcpy(unsafe.Add(buf, x.length), unsafe.Pointer(y.ptr), y.length)
6666
return _string{ptr: (*byte)(buf), length: length}
@@ -73,7 +73,7 @@ func stringFromBytes(x struct {
7373
len uintptr
7474
cap uintptr
7575
}) _string {
76-
buf := alloc(x.len, gclayout.NoPtrs)
76+
buf := alloc(x.len, gclayout.NoPtrs.AsPtr())
7777
memcpy(buf, unsafe.Pointer(x.ptr), x.len)
7878
return _string{ptr: (*byte)(buf), length: x.len}
7979
}
@@ -84,7 +84,7 @@ func stringToBytes(x _string) (slice struct {
8484
len uintptr
8585
cap uintptr
8686
}) {
87-
buf := alloc(x.length, gclayout.NoPtrs)
87+
buf := alloc(x.length, gclayout.NoPtrs.AsPtr())
8888
memcpy(buf, unsafe.Pointer(x.ptr), x.length)
8989
slice.ptr = (*byte)(buf)
9090
slice.len = x.length
@@ -101,7 +101,7 @@ func stringFromRunes(runeSlice []rune) (s _string) {
101101
}
102102

103103
// Allocate memory for the string.
104-
s.ptr = (*byte)(alloc(s.length, gclayout.NoPtrs))
104+
s.ptr = (*byte)(alloc(s.length, gclayout.NoPtrs.AsPtr()))
105105

106106
// Encode runes to UTF-8 and store the resulting bytes in the string.
107107
index := uintptr(0)

0 commit comments

Comments
 (0)