Skip to content

Commit 4605ce2

Browse files
qiulaidongfenggopherbot
authored andcommitted
cmd/compile: use MapMaxKeyBytes,MapMaxElemBytes,MapBucketCount of internal/abi
For #59670 Change-Id: I651e211650e69989c598ab16202105bc6e68d67e GitHub-Last-Rev: fba087a GitHub-Pull-Request: #64776 Reviewed-on: https://go-review.googlesource.com/c/go/+/550615 Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 9c663e8 commit 4605ce2

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,6 @@ type typeSig struct {
5555
mtype *types.Type
5656
}
5757

58-
// Builds a type representing a Bucket structure for
59-
// the given map type. This type is not visible to users -
60-
// we include only enough information to generate a correct GC
61-
// program for it.
62-
// Make sure this stays in sync with runtime/map.go.
63-
//
64-
// A "bucket" is a "struct" {
65-
// tophash [BUCKETSIZE]uint8
66-
// keys [BUCKETSIZE]keyType
67-
// elems [BUCKETSIZE]elemType
68-
// overflow *bucket
69-
// }
70-
const (
71-
BUCKETSIZE = abi.MapBucketCount
72-
MAXKEYSIZE = abi.MapMaxKeyBytes
73-
MAXELEMSIZE = abi.MapMaxElemBytes
74-
)
75-
7658
func commonSize() int { return int(rttype.Type.Size()) } // Sizeof(runtime._type{})
7759

7860
func uncommonSize(t *types.Type) int { // Sizeof(runtime.uncommontype{})
@@ -89,6 +71,18 @@ func makefield(name string, t *types.Type) *types.Field {
8971

9072
// MapBucketType makes the map bucket type given the type of the map.
9173
func MapBucketType(t *types.Type) *types.Type {
74+
// Builds a type representing a Bucket structure for
75+
// the given map type. This type is not visible to users -
76+
// we include only enough information to generate a correct GC
77+
// program for it.
78+
// Make sure this stays in sync with runtime/map.go.
79+
//
80+
// A "bucket" is a "struct" {
81+
// tophash [abi.MapBucketCount]uint8
82+
// keys [abi.MapBucketCount]keyType
83+
// elems [abi.MapBucketCount]elemType
84+
// overflow *bucket
85+
// }
9286
if t.MapType().Bucket != nil {
9387
return t.MapType().Bucket
9488
}
@@ -97,25 +91,25 @@ func MapBucketType(t *types.Type) *types.Type {
9791
elemtype := t.Elem()
9892
types.CalcSize(keytype)
9993
types.CalcSize(elemtype)
100-
if keytype.Size() > MAXKEYSIZE {
94+
if keytype.Size() > abi.MapMaxKeyBytes {
10195
keytype = types.NewPtr(keytype)
10296
}
103-
if elemtype.Size() > MAXELEMSIZE {
97+
if elemtype.Size() > abi.MapMaxElemBytes {
10498
elemtype = types.NewPtr(elemtype)
10599
}
106100

107101
field := make([]*types.Field, 0, 5)
108102

109103
// The first field is: uint8 topbits[BUCKETSIZE].
110-
arr := types.NewArray(types.Types[types.TUINT8], BUCKETSIZE)
104+
arr := types.NewArray(types.Types[types.TUINT8], abi.MapBucketCount)
111105
field = append(field, makefield("topbits", arr))
112106

113-
arr = types.NewArray(keytype, BUCKETSIZE)
107+
arr = types.NewArray(keytype, abi.MapBucketCount)
114108
arr.SetNoalg(true)
115109
keys := makefield("keys", arr)
116110
field = append(field, keys)
117111

118-
arr = types.NewArray(elemtype, BUCKETSIZE)
112+
arr = types.NewArray(elemtype, abi.MapBucketCount)
119113
arr.SetNoalg(true)
120114
elems := makefield("elems", arr)
121115
field = append(field, elems)
@@ -142,25 +136,25 @@ func MapBucketType(t *types.Type) *types.Type {
142136
if !types.IsComparable(t.Key()) {
143137
base.Fatalf("unsupported map key type for %v", t)
144138
}
145-
if BUCKETSIZE < 8 {
146-
base.Fatalf("bucket size %d too small for proper alignment %d", BUCKETSIZE, 8)
139+
if abi.MapBucketCount < 8 {
140+
base.Fatalf("bucket size %d too small for proper alignment %d", abi.MapBucketCount, 8)
147141
}
148-
if uint8(keytype.Alignment()) > BUCKETSIZE {
142+
if uint8(keytype.Alignment()) > abi.MapBucketCount {
149143
base.Fatalf("key align too big for %v", t)
150144
}
151-
if uint8(elemtype.Alignment()) > BUCKETSIZE {
152-
base.Fatalf("elem align %d too big for %v, BUCKETSIZE=%d", elemtype.Alignment(), t, BUCKETSIZE)
145+
if uint8(elemtype.Alignment()) > abi.MapBucketCount {
146+
base.Fatalf("elem align %d too big for %v, BUCKETSIZE=%d", elemtype.Alignment(), t, abi.MapBucketCount)
153147
}
154-
if keytype.Size() > MAXKEYSIZE {
148+
if keytype.Size() > abi.MapMaxKeyBytes {
155149
base.Fatalf("key size too large for %v", t)
156150
}
157-
if elemtype.Size() > MAXELEMSIZE {
151+
if elemtype.Size() > abi.MapMaxElemBytes {
158152
base.Fatalf("elem size too large for %v", t)
159153
}
160-
if t.Key().Size() > MAXKEYSIZE && !keytype.IsPtr() {
154+
if t.Key().Size() > abi.MapMaxKeyBytes && !keytype.IsPtr() {
161155
base.Fatalf("key indirect incorrect for %v", t)
162156
}
163-
if t.Elem().Size() > MAXELEMSIZE && !elemtype.IsPtr() {
157+
if t.Elem().Size() > abi.MapMaxElemBytes && !elemtype.IsPtr() {
164158
base.Fatalf("elem indirect incorrect for %v", t)
165159
}
166160
if keytype.Size()%keytype.Alignment() != 0 {
@@ -1124,14 +1118,14 @@ func writeType(t *types.Type) *obj.LSym {
11241118
var flags uint32
11251119
// Note: flags must match maptype accessors in ../../../../runtime/type.go
11261120
// and maptype builder in ../../../../reflect/type.go:MapOf.
1127-
if t.Key().Size() > MAXKEYSIZE {
1121+
if t.Key().Size() > abi.MapMaxKeyBytes {
11281122
c.Field("KeySize").WriteUint8(uint8(types.PtrSize))
11291123
flags |= 1 // indirect key
11301124
} else {
11311125
c.Field("KeySize").WriteUint8(uint8(t.Key().Size()))
11321126
}
11331127

1134-
if t.Elem().Size() > MAXELEMSIZE {
1128+
if t.Elem().Size() > abi.MapMaxElemBytes {
11351129
c.Field("ValueSize").WriteUint8(uint8(types.PtrSize))
11361130
flags |= 2 // indirect value
11371131
} else {

src/cmd/compile/internal/walk/builtin.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"go/constant"
1010
"go/token"
11+
"internal/abi"
1112
"strings"
1213

1314
"cmd/compile/internal/base"
@@ -321,7 +322,7 @@ func walkMakeMap(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
321322
// Maximum key and elem size is 128 bytes, larger objects
322323
// are stored with an indirection. So max bucket size is 2048+eps.
323324
if !ir.IsConst(hint, constant.Int) ||
324-
constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(reflectdata.BUCKETSIZE)) {
325+
constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(abi.MapBucketCount)) {
325326

326327
// In case hint is larger than BUCKETSIZE runtime.makemap
327328
// will allocate the buckets on the heap, see #20184
@@ -332,7 +333,7 @@ func walkMakeMap(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
332333
// h.buckets = b
333334
// }
334335

335-
nif := ir.NewIfStmt(base.Pos, ir.NewBinaryExpr(base.Pos, ir.OLE, hint, ir.NewInt(base.Pos, reflectdata.BUCKETSIZE)), nil, nil)
336+
nif := ir.NewIfStmt(base.Pos, ir.NewBinaryExpr(base.Pos, ir.OLE, hint, ir.NewInt(base.Pos, abi.MapBucketCount)), nil, nil)
336337
nif.Likely = true
337338

338339
// var bv bmap
@@ -347,7 +348,7 @@ func walkMakeMap(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
347348
}
348349
}
349350

350-
if ir.IsConst(hint, constant.Int) && constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(reflectdata.BUCKETSIZE)) {
351+
if ir.IsConst(hint, constant.Int) && constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(abi.MapBucketCount)) {
351352
// Handling make(map[any]any) and
352353
// make(map[any]any, hint) where hint <= BUCKETSIZE
353354
// special allows for faster map initialization and

0 commit comments

Comments
 (0)