1
0
mirror of https://github.com/golang/go synced 2024-11-17 11:04:53 -07:00

cmd/compile: use MapMaxKeyBytes,MapMaxElemBytes,MapBucketCount of internal/abi

For #59670

Change-Id: I651e211650e69989c598ab16202105bc6e68d67e
GitHub-Last-Rev: fba087a35f
GitHub-Pull-Request: golang/go#64776
Reviewed-on: https://go-review.googlesource.com/c/go/+/550615
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
qiulaidongfeng 2023-12-17 13:04:20 +00:00 committed by Gopher Robot
parent 9c663e8bf7
commit 4605ce2d34
2 changed files with 32 additions and 37 deletions

View File

@ -55,24 +55,6 @@ type typeSig struct {
mtype *types.Type
}
// Builds a type representing a Bucket structure for
// the given map type. This type is not visible to users -
// we include only enough information to generate a correct GC
// program for it.
// Make sure this stays in sync with runtime/map.go.
//
// A "bucket" is a "struct" {
// tophash [BUCKETSIZE]uint8
// keys [BUCKETSIZE]keyType
// elems [BUCKETSIZE]elemType
// overflow *bucket
// }
const (
BUCKETSIZE = abi.MapBucketCount
MAXKEYSIZE = abi.MapMaxKeyBytes
MAXELEMSIZE = abi.MapMaxElemBytes
)
func commonSize() int { return int(rttype.Type.Size()) } // Sizeof(runtime._type{})
func uncommonSize(t *types.Type) int { // Sizeof(runtime.uncommontype{})
@ -89,6 +71,18 @@ func makefield(name string, t *types.Type) *types.Field {
// MapBucketType makes the map bucket type given the type of the map.
func MapBucketType(t *types.Type) *types.Type {
// Builds a type representing a Bucket structure for
// the given map type. This type is not visible to users -
// we include only enough information to generate a correct GC
// program for it.
// Make sure this stays in sync with runtime/map.go.
//
// A "bucket" is a "struct" {
// tophash [abi.MapBucketCount]uint8
// keys [abi.MapBucketCount]keyType
// elems [abi.MapBucketCount]elemType
// overflow *bucket
// }
if t.MapType().Bucket != nil {
return t.MapType().Bucket
}
@ -97,25 +91,25 @@ func MapBucketType(t *types.Type) *types.Type {
elemtype := t.Elem()
types.CalcSize(keytype)
types.CalcSize(elemtype)
if keytype.Size() > MAXKEYSIZE {
if keytype.Size() > abi.MapMaxKeyBytes {
keytype = types.NewPtr(keytype)
}
if elemtype.Size() > MAXELEMSIZE {
if elemtype.Size() > abi.MapMaxElemBytes {
elemtype = types.NewPtr(elemtype)
}
field := make([]*types.Field, 0, 5)
// The first field is: uint8 topbits[BUCKETSIZE].
arr := types.NewArray(types.Types[types.TUINT8], BUCKETSIZE)
arr := types.NewArray(types.Types[types.TUINT8], abi.MapBucketCount)
field = append(field, makefield("topbits", arr))
arr = types.NewArray(keytype, BUCKETSIZE)
arr = types.NewArray(keytype, abi.MapBucketCount)
arr.SetNoalg(true)
keys := makefield("keys", arr)
field = append(field, keys)
arr = types.NewArray(elemtype, BUCKETSIZE)
arr = types.NewArray(elemtype, abi.MapBucketCount)
arr.SetNoalg(true)
elems := makefield("elems", arr)
field = append(field, elems)
@ -142,25 +136,25 @@ func MapBucketType(t *types.Type) *types.Type {
if !types.IsComparable(t.Key()) {
base.Fatalf("unsupported map key type for %v", t)
}
if BUCKETSIZE < 8 {
base.Fatalf("bucket size %d too small for proper alignment %d", BUCKETSIZE, 8)
if abi.MapBucketCount < 8 {
base.Fatalf("bucket size %d too small for proper alignment %d", abi.MapBucketCount, 8)
}
if uint8(keytype.Alignment()) > BUCKETSIZE {
if uint8(keytype.Alignment()) > abi.MapBucketCount {
base.Fatalf("key align too big for %v", t)
}
if uint8(elemtype.Alignment()) > BUCKETSIZE {
base.Fatalf("elem align %d too big for %v, BUCKETSIZE=%d", elemtype.Alignment(), t, BUCKETSIZE)
if uint8(elemtype.Alignment()) > abi.MapBucketCount {
base.Fatalf("elem align %d too big for %v, BUCKETSIZE=%d", elemtype.Alignment(), t, abi.MapBucketCount)
}
if keytype.Size() > MAXKEYSIZE {
if keytype.Size() > abi.MapMaxKeyBytes {
base.Fatalf("key size too large for %v", t)
}
if elemtype.Size() > MAXELEMSIZE {
if elemtype.Size() > abi.MapMaxElemBytes {
base.Fatalf("elem size too large for %v", t)
}
if t.Key().Size() > MAXKEYSIZE && !keytype.IsPtr() {
if t.Key().Size() > abi.MapMaxKeyBytes && !keytype.IsPtr() {
base.Fatalf("key indirect incorrect for %v", t)
}
if t.Elem().Size() > MAXELEMSIZE && !elemtype.IsPtr() {
if t.Elem().Size() > abi.MapMaxElemBytes && !elemtype.IsPtr() {
base.Fatalf("elem indirect incorrect for %v", t)
}
if keytype.Size()%keytype.Alignment() != 0 {
@ -1124,14 +1118,14 @@ func writeType(t *types.Type) *obj.LSym {
var flags uint32
// Note: flags must match maptype accessors in ../../../../runtime/type.go
// and maptype builder in ../../../../reflect/type.go:MapOf.
if t.Key().Size() > MAXKEYSIZE {
if t.Key().Size() > abi.MapMaxKeyBytes {
c.Field("KeySize").WriteUint8(uint8(types.PtrSize))
flags |= 1 // indirect key
} else {
c.Field("KeySize").WriteUint8(uint8(t.Key().Size()))
}
if t.Elem().Size() > MAXELEMSIZE {
if t.Elem().Size() > abi.MapMaxElemBytes {
c.Field("ValueSize").WriteUint8(uint8(types.PtrSize))
flags |= 2 // indirect value
} else {

View File

@ -8,6 +8,7 @@ import (
"fmt"
"go/constant"
"go/token"
"internal/abi"
"strings"
"cmd/compile/internal/base"
@ -321,7 +322,7 @@ func walkMakeMap(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
// Maximum key and elem size is 128 bytes, larger objects
// are stored with an indirection. So max bucket size is 2048+eps.
if !ir.IsConst(hint, constant.Int) ||
constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(reflectdata.BUCKETSIZE)) {
constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(abi.MapBucketCount)) {
// In case hint is larger than BUCKETSIZE runtime.makemap
// will allocate the buckets on the heap, see #20184
@ -332,7 +333,7 @@ func walkMakeMap(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
// h.buckets = b
// }
nif := ir.NewIfStmt(base.Pos, ir.NewBinaryExpr(base.Pos, ir.OLE, hint, ir.NewInt(base.Pos, reflectdata.BUCKETSIZE)), nil, nil)
nif := ir.NewIfStmt(base.Pos, ir.NewBinaryExpr(base.Pos, ir.OLE, hint, ir.NewInt(base.Pos, abi.MapBucketCount)), nil, nil)
nif.Likely = true
// var bv bmap
@ -347,7 +348,7 @@ func walkMakeMap(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
}
}
if ir.IsConst(hint, constant.Int) && constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(reflectdata.BUCKETSIZE)) {
if ir.IsConst(hint, constant.Int) && constant.Compare(hint.Val(), token.LEQ, constant.MakeInt64(abi.MapBucketCount)) {
// Handling make(map[any]any) and
// make(map[any]any, hint) where hint <= BUCKETSIZE
// special allows for faster map initialization and