Skip to content

Commit db16de9

Browse files
committed
runtime: remove kindNoPointers
We already have the ptrdata field in a type, which encodes exactly the same information that kindNoPointers does. My problem with kindNoPointers is that it often leads to double-negative code like: t.kind & kindNoPointers != 0 Much clearer is: t.ptrdata == 0 Update #27167 Change-Id: I92307d7f018a6bbe3daca4a4abb4225e359349b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/169157 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5016323 commit db16de9

20 files changed

+44
-71
lines changed

src/cmd/compile/internal/gc/reflect.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,6 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
882882
ot = duint8(lsym, ot, t.Align) // fieldAlign
883883

884884
i = kinds[t.Etype]
885-
if !types.Haspointers(t) {
886-
i |= objabi.KindNoPointers
887-
}
888885
if isdirectiface(t) {
889886
i |= objabi.KindDirectIface
890887
}

src/cmd/internal/objabi/typekind.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,5 @@ const (
3636
KindUnsafePointer
3737
KindDirectIface = 1 << 5
3838
KindGCProg = 1 << 6
39-
KindNoPointers = 1 << 7
4039
KindMask = (1 << 5) - 1
4140
)

src/internal/reflectlite/type.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ func (n name) pkgPath() string {
370370
const (
371371
kindDirectIface = 1 << 5
372372
kindGCProg = 1 << 6 // Type.gc points to GC program
373-
kindNoPointers = 1 << 7
374373
kindMask = (1 << 5) - 1
375374
)
376375

src/reflect/export_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func FuncLayout(t Type, rcvr Type) (frametype Type, argSize, retOffset uintptr,
4040
for i := uintptr(0); i < ft.ptrdata/ptrSize; i++ {
4141
gc = append(gc, gcdata[i/8]>>(i%8)&1)
4242
}
43-
ptrs = ft.kind&kindNoPointers == 0
43+
ptrs = ft.ptrdata != 0
4444
return
4545
}
4646

src/reflect/swapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Swapper(slice interface{}) func(i, j int) {
2929

3030
typ := v.Type().Elem().(*rtype)
3131
size := typ.Size()
32-
hasPtr := typ.kind&kindNoPointers == 0
32+
hasPtr := typ.ptrdata != 0
3333

3434
// Some common & small cases, without using memmove:
3535
if hasPtr {

src/reflect/type.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ type Method struct {
586586
const (
587587
kindDirectIface = 1 << 5
588588
kindGCProg = 1 << 6 // Type.gc points to GC program
589-
kindNoPointers = 1 << 7
590589
kindMask = (1 << 5) - 1
591590
)
592591

@@ -782,7 +781,7 @@ func (t *rtype) FieldAlign() int { return int(t.fieldAlign) }
782781

783782
func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }
784783

785-
func (t *rtype) pointers() bool { return t.kind&kindNoPointers == 0 }
784+
func (t *rtype) pointers() bool { return t.ptrdata != 0 }
786785

787786
func (t *rtype) common() *rtype { return t }
788787

@@ -2156,13 +2155,6 @@ const (
21562155
)
21572156

21582157
func bucketOf(ktyp, etyp *rtype) *rtype {
2159-
// See comment on hmap.overflow in ../runtime/map.go.
2160-
var kind uint8
2161-
if ktyp.kind&kindNoPointers != 0 && etyp.kind&kindNoPointers != 0 &&
2162-
ktyp.size <= maxKeySize && etyp.size <= maxValSize {
2163-
kind = kindNoPointers
2164-
}
2165-
21662158
if ktyp.size > maxKeySize {
21672159
ktyp = PtrTo(ktyp).(*rtype)
21682160
}
@@ -2189,12 +2181,12 @@ func bucketOf(ktyp, etyp *rtype) *rtype {
21892181
panic("reflect: bad size computation in MapOf")
21902182
}
21912183

2192-
if kind != kindNoPointers {
2184+
if ktyp.ptrdata != 0 || etyp.ptrdata != 0 {
21932185
nptr := (bucketSize*(1+ktyp.size+etyp.size) + ptrSize) / ptrSize
21942186
mask := make([]byte, (nptr+7)/8)
21952187
base := bucketSize / ptrSize
21962188

2197-
if ktyp.kind&kindNoPointers == 0 {
2189+
if ktyp.ptrdata != 0 {
21982190
if ktyp.kind&kindGCProg != 0 {
21992191
panic("reflect: unexpected GC program in MapOf")
22002192
}
@@ -2210,7 +2202,7 @@ func bucketOf(ktyp, etyp *rtype) *rtype {
22102202
}
22112203
base += bucketSize * ktyp.size / ptrSize
22122204

2213-
if etyp.kind&kindNoPointers == 0 {
2205+
if etyp.ptrdata != 0 {
22142206
if etyp.kind&kindGCProg != 0 {
22152207
panic("reflect: unexpected GC program in MapOf")
22162208
}
@@ -2241,7 +2233,7 @@ func bucketOf(ktyp, etyp *rtype) *rtype {
22412233
b := &rtype{
22422234
align: ptrSize,
22432235
size: size,
2244-
kind: kind,
2236+
kind: uint8(Struct),
22452237
ptrdata: ptrdata,
22462238
gcdata: gcdata,
22472239
}
@@ -2349,7 +2341,6 @@ func StructOf(fields []StructField) Type {
23492341
repr = make([]byte, 0, 64)
23502342
fset = map[string]struct{}{} // fields' names
23512343

2352-
hasPtr = false // records whether at least one struct-field is a pointer
23532344
hasGCProg = false // records whether a struct-field type has a GCProg
23542345
)
23552346

@@ -2370,9 +2361,6 @@ func StructOf(fields []StructField) Type {
23702361
if ft.kind&kindGCProg != 0 {
23712362
hasGCProg = true
23722363
}
2373-
if ft.pointers() {
2374-
hasPtr = true
2375-
}
23762364

23772365
// Update string and hash
23782366
name := f.name.name()
@@ -2657,11 +2645,6 @@ func StructOf(fields []StructField) Type {
26572645
if len(methods) > 0 {
26582646
typ.tflag |= tflagUncommon
26592647
}
2660-
if !hasPtr {
2661-
typ.kind |= kindNoPointers
2662-
} else {
2663-
typ.kind &^= kindNoPointers
2664-
}
26652648

26662649
if hasGCProg {
26672650
lastPtrField := 0
@@ -2869,11 +2852,9 @@ func ArrayOf(count int, elem Type) Type {
28692852
array.len = uintptr(count)
28702853
array.slice = SliceOf(elem).(*rtype)
28712854

2872-
array.kind &^= kindNoPointers
28732855
switch {
2874-
case typ.kind&kindNoPointers != 0 || array.size == 0:
2856+
case typ.ptrdata == 0 || array.size == 0:
28752857
// No pointers.
2876-
array.kind |= kindNoPointers
28772858
array.gcdata = nil
28782859
array.ptrdata = 0
28792860

@@ -3087,8 +3068,6 @@ func funcLayout(t *funcType, rcvr *rtype) (frametype *rtype, argSize, retOffset
30873068
}
30883069
if ptrmap.n > 0 {
30893070
x.gcdata = &ptrmap.data[0]
3090-
} else {
3091-
x.kind |= kindNoPointers
30923071
}
30933072

30943073
var s string
@@ -3135,7 +3114,7 @@ func (bv *bitVector) append(bit uint8) {
31353114
}
31363115

31373116
func addTypeBits(bv *bitVector, offset uintptr, t *rtype) {
3138-
if t.kind&kindNoPointers != 0 {
3117+
if t.ptrdata == 0 {
31393118
return
31403119
}
31413120

src/runtime/cgocall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ const cgoResultFail = "cgo result has Go pointer"
460460
// depending on indir. The top parameter is whether we are at the top
461461
// level, where Go pointers are allowed.
462462
func cgoCheckArg(t *_type, p unsafe.Pointer, indir, top bool, msg string) {
463-
if t.kind&kindNoPointers != 0 {
463+
if t.ptrdata == 0 {
464464
// If the type has no pointers there is nothing to do.
465465
return
466466
}
@@ -523,7 +523,7 @@ func cgoCheckArg(t *_type, p unsafe.Pointer, indir, top bool, msg string) {
523523
if !top {
524524
panic(errorString(msg))
525525
}
526-
if st.elem.kind&kindNoPointers != 0 {
526+
if st.elem.ptrdata == 0 {
527527
return
528528
}
529529
for i := 0; i < s.cap; i++ {

src/runtime/cgocheck.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func cgoCheckWriteBarrier(dst *uintptr, src uintptr) {
6464
//go:nosplit
6565
//go:nowritebarrier
6666
func cgoCheckMemmove(typ *_type, dst, src unsafe.Pointer, off, size uintptr) {
67-
if typ.kind&kindNoPointers != 0 {
67+
if typ.ptrdata == 0 {
6868
return
6969
}
7070
if !cgoIsGoPointer(src) {
@@ -83,7 +83,7 @@ func cgoCheckMemmove(typ *_type, dst, src unsafe.Pointer, off, size uintptr) {
8383
//go:nosplit
8484
//go:nowritebarrier
8585
func cgoCheckSliceCopy(typ *_type, dst, src slice, n int) {
86-
if typ.kind&kindNoPointers != 0 {
86+
if typ.ptrdata == 0 {
8787
return
8888
}
8989
if !cgoIsGoPointer(src.array) {
@@ -203,7 +203,7 @@ func cgoCheckBits(src unsafe.Pointer, gcbits *byte, off, size uintptr) {
203203
//go:nowritebarrier
204204
//go:systemstack
205205
func cgoCheckUsingType(typ *_type, src unsafe.Pointer, off, size uintptr) {
206-
if typ.kind&kindNoPointers != 0 {
206+
if typ.ptrdata == 0 {
207207
return
208208
}
209209

src/runtime/chan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func makechan(t *chantype, size int) *hchan {
9595
c = (*hchan)(mallocgc(hchanSize, nil, true))
9696
// Race detector uses this location for synchronization.
9797
c.buf = c.raceaddr()
98-
case elem.kind&kindNoPointers != 0:
98+
case elem.ptrdata == 0:
9999
// Elements do not contain pointers.
100100
// Allocate hchan and buf in one call.
101101
c = (*hchan)(mallocgc(hchanSize+mem, nil, true))

src/runtime/heapdump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func dumptype(t *_type) {
195195
dwritebyte('.')
196196
dwrite(name.str, uintptr(name.len))
197197
}
198-
dumpbool(t.kind&kindDirectIface == 0 || t.kind&kindNoPointers == 0)
198+
dumpbool(t.kind&kindDirectIface == 0 || t.ptrdata != 0)
199199
}
200200

201201
// dump an object

src/runtime/malloc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
858858
dataSize := size
859859
c := gomcache()
860860
var x unsafe.Pointer
861-
noscan := typ == nil || typ.kind&kindNoPointers != 0
861+
noscan := typ == nil || typ.ptrdata == 0
862862
if size <= maxSmallSize {
863863
if noscan && size < maxTinySize {
864864
// Tiny allocator.

src/runtime/map.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (h *hmap) newoverflow(t *maptype, b *bmap) *bmap {
264264
ovf = (*bmap)(newobject(t.bucket))
265265
}
266266
h.incrnoverflow()
267-
if t.bucket.kind&kindNoPointers != 0 {
267+
if t.bucket.ptrdata == 0 {
268268
h.createOverflow()
269269
*h.extra.overflow = append(*h.extra.overflow, ovf)
270270
}
@@ -368,7 +368,7 @@ func makeBucketArray(t *maptype, b uint8, dirtyalloc unsafe.Pointer) (buckets un
368368
// but may not be empty.
369369
buckets = dirtyalloc
370370
size := t.bucket.size * nbuckets
371-
if t.bucket.kind&kindNoPointers == 0 {
371+
if t.bucket.ptrdata != 0 {
372372
memclrHasPointers(buckets, size)
373373
} else {
374374
memclrNoHeapPointers(buckets, size)
@@ -742,13 +742,13 @@ search:
742742
// Only clear key if there are pointers in it.
743743
if t.indirectkey() {
744744
*(*unsafe.Pointer)(k) = nil
745-
} else if t.key.kind&kindNoPointers == 0 {
745+
} else if t.key.ptrdata != 0 {
746746
memclrHasPointers(k, t.key.size)
747747
}
748748
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
749749
if t.indirectvalue() {
750750
*(*unsafe.Pointer)(v) = nil
751-
} else if t.elem.kind&kindNoPointers == 0 {
751+
} else if t.elem.ptrdata != 0 {
752752
memclrHasPointers(v, t.elem.size)
753753
} else {
754754
memclrNoHeapPointers(v, t.elem.size)
@@ -820,7 +820,7 @@ func mapiterinit(t *maptype, h *hmap, it *hiter) {
820820
// grab snapshot of bucket state
821821
it.B = h.B
822822
it.buckets = h.buckets
823-
if t.bucket.kind&kindNoPointers != 0 {
823+
if t.bucket.ptrdata == 0 {
824824
// Allocate the current slice and remember pointers to both current and old.
825825
// This preserves all relevant overflow buckets alive even if
826826
// the table grows and/or overflow buckets are added to the table
@@ -1232,7 +1232,7 @@ func evacuate(t *maptype, h *hmap, oldbucket uintptr) {
12321232
}
12331233
}
12341234
// Unlink the overflow buckets & clear key/value to help GC.
1235-
if h.flags&oldIterator == 0 && t.bucket.kind&kindNoPointers == 0 {
1235+
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 {
12361236
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
12371237
// Preserve b.tophash because the evacuation
12381238
// state is maintained there.

src/runtime/map_fast32.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ search:
299299
continue
300300
}
301301
// Only clear key if there are pointers in it.
302-
if t.key.kind&kindNoPointers == 0 {
302+
if t.key.ptrdata != 0 {
303303
memclrHasPointers(k, t.key.size)
304304
}
305305
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*4+i*uintptr(t.valuesize))
306-
if t.elem.kind&kindNoPointers == 0 {
306+
if t.elem.ptrdata != 0 {
307307
memclrHasPointers(v, t.elem.size)
308308
} else {
309309
memclrNoHeapPointers(v, t.elem.size)
@@ -418,7 +418,7 @@ func evacuate_fast32(t *maptype, h *hmap, oldbucket uintptr) {
418418
dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check
419419

420420
// Copy key.
421-
if sys.PtrSize == 4 && t.key.kind&kindNoPointers == 0 && writeBarrier.enabled {
421+
if sys.PtrSize == 4 && t.key.ptrdata != 0 && writeBarrier.enabled {
422422
// Write with a write barrier.
423423
*(*unsafe.Pointer)(dst.k) = *(*unsafe.Pointer)(k)
424424
} else {
@@ -436,7 +436,7 @@ func evacuate_fast32(t *maptype, h *hmap, oldbucket uintptr) {
436436
}
437437
}
438438
// Unlink the overflow buckets & clear key/value to help GC.
439-
if h.flags&oldIterator == 0 && t.bucket.kind&kindNoPointers == 0 {
439+
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 {
440440
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
441441
// Preserve b.tophash because the evacuation
442442
// state is maintained there.

src/runtime/map_fast64.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ search:
299299
continue
300300
}
301301
// Only clear key if there are pointers in it.
302-
if t.key.kind&kindNoPointers == 0 {
302+
if t.key.ptrdata != 0 {
303303
memclrHasPointers(k, t.key.size)
304304
}
305305
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*8+i*uintptr(t.valuesize))
306-
if t.elem.kind&kindNoPointers == 0 {
306+
if t.elem.ptrdata != 0 {
307307
memclrHasPointers(v, t.elem.size)
308308
} else {
309309
memclrNoHeapPointers(v, t.elem.size)
@@ -418,7 +418,7 @@ func evacuate_fast64(t *maptype, h *hmap, oldbucket uintptr) {
418418
dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check
419419

420420
// Copy key.
421-
if t.key.kind&kindNoPointers == 0 && writeBarrier.enabled {
421+
if t.key.ptrdata != 0 && writeBarrier.enabled {
422422
if sys.PtrSize == 8 {
423423
// Write with a write barrier.
424424
*(*unsafe.Pointer)(dst.k) = *(*unsafe.Pointer)(k)
@@ -442,7 +442,7 @@ func evacuate_fast64(t *maptype, h *hmap, oldbucket uintptr) {
442442
}
443443
}
444444
// Unlink the overflow buckets & clear key/value to help GC.
445-
if h.flags&oldIterator == 0 && t.bucket.kind&kindNoPointers == 0 {
445+
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 {
446446
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
447447
// Preserve b.tophash because the evacuation
448448
// state is maintained there.

src/runtime/map_faststr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ search:
332332
// Clear key's pointer.
333333
k.str = nil
334334
v := add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.valuesize))
335-
if t.elem.kind&kindNoPointers == 0 {
335+
if t.elem.ptrdata != 0 {
336336
memclrHasPointers(v, t.elem.size)
337337
} else {
338338
memclrNoHeapPointers(v, t.elem.size)
@@ -461,7 +461,7 @@ func evacuate_faststr(t *maptype, h *hmap, oldbucket uintptr) {
461461
}
462462
// Unlink the overflow buckets & clear key/value to help GC.
463463
// Unlink the overflow buckets & clear key/value to help GC.
464-
if h.flags&oldIterator == 0 && t.bucket.kind&kindNoPointers == 0 {
464+
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 {
465465
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
466466
// Preserve b.tophash because the evacuation
467467
// state is maintained there.

0 commit comments

Comments
 (0)