From: qiulaidongfeng <2645477756@qq.com> Date: Sun, 17 Dec 2023 13:04:20 +0000 (+0000) Subject: cmd/compile: use MapMaxKeyBytes,MapMaxElemBytes,MapBucketCount of internal/abi X-Git-Tag: go1.23rc1~1413 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4605ce2d344d9e25c1d58898acb28edab93af97f;p=gostls13.git cmd/compile: use MapMaxKeyBytes,MapMaxElemBytes,MapBucketCount of internal/abi For #59670 Change-Id: I651e211650e69989c598ab16202105bc6e68d67e GitHub-Last-Rev: fba087a35fa563cba0dc5f70e8c9d9108dc1f1d4 GitHub-Pull-Request: golang/go#64776 Reviewed-on: https://go-review.googlesource.com/c/go/+/550615 Reviewed-by: Keith Randall TryBot-Result: Gopher Robot Reviewed-by: Keith Randall Auto-Submit: Keith Randall Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/compile/internal/reflectdata/reflect.go b/src/cmd/compile/internal/reflectdata/reflect.go index c2407af017..8ef1a913e8 100644 --- a/src/cmd/compile/internal/reflectdata/reflect.go +++ b/src/cmd/compile/internal/reflectdata/reflect.go @@ -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 { diff --git a/src/cmd/compile/internal/walk/builtin.go b/src/cmd/compile/internal/walk/builtin.go index 37143baa28..41fc0bf5dc 100644 --- a/src/cmd/compile/internal/walk/builtin.go +++ b/src/cmd/compile/internal/walk/builtin.go @@ -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