diff options
author | Than McIntosh <thanm@google.com> | 2024-01-09 15:06:15 +0000 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2024-01-10 15:27:52 +0000 |
commit | 8c1349baf7da63de98cf2b2764607ceec37b6283 (patch) | |
tree | a0b42d23a5caedf852adea9a6d42235f49a44812 | |
parent | 1d45a7ef560a76318ed59dfdb178cecd58caf948 (diff) | |
download | go-8c1349baf7da63de98cf2b2764607ceec37b6283.tar.gz go-8c1349baf7da63de98cf2b2764607ceec37b6283.zip |
cmd/compile: use hashed symbol name for go.shape types if too long
Shape-based stenciling in the Go compiler's generic instantiation
phase looks up shape types using the underlying type of a given target
type. This has a beneficial effect in most cases (e.g. we can use the
same shape type for two different named types whose underlying type is
"int"), but causes some problems when the underlying type is a very
large structure. The link string for the underlying type of a large
imported struct can be extremely long, since the link string
essentially enumerates the full package path for every field type;
this can produce a "go.shape.struct { ... " symbol name that is
absurdly long.
This patch switches the compiler to use a hash of the underlying type
link string instead of the string itself, which should continue to
provide commoning but keep symbol name lengths reasonable for shape
types based on large imported structs.
Fixes #65030.
Change-Id: I87d602626c43172beb99c186b8ef72327b8227a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/554975
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Than McIntosh <thanm@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
-rw-r--r-- | src/cmd/compile/internal/base/debug.go | 1 | ||||
-rw-r--r-- | src/cmd/compile/internal/base/flag.go | 1 | ||||
-rw-r--r-- | src/cmd/compile/internal/noder/reader.go | 13 |
3 files changed, 14 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/base/debug.go b/src/cmd/compile/internal/base/debug.go index aadd950a0a..420ad1305e 100644 --- a/src/cmd/compile/internal/base/debug.go +++ b/src/cmd/compile/internal/base/debug.go @@ -40,6 +40,7 @@ type DebugFlags struct { LoopVar int `help:"shared (0, default), 1 (private loop variables), 2, private + log"` LoopVarHash string `help:"for debugging changes in loop behavior. Overrides experiment and loopvar flag."` LocationLists int `help:"print information about DWARF location list creation"` + MaxShapeLen int `help:"hash shape names longer than this threshold (default 500)" concurrent:"ok"` Nil int `help:"print information about nil checks"` NoOpenDefer int `help:"disable open-coded defers" concurrent:"ok"` NoRefName int `help:"do not include referenced symbol names in object file" concurrent:"ok"` diff --git a/src/cmd/compile/internal/base/flag.go b/src/cmd/compile/internal/base/flag.go index e2e15c3c9c..a3144f8fb4 100644 --- a/src/cmd/compile/internal/base/flag.go +++ b/src/cmd/compile/internal/base/flag.go @@ -176,6 +176,7 @@ func ParseFlags() { Flag.WB = true Debug.ConcurrentOk = true + Debug.MaxShapeLen = 500 Debug.InlFuncsWithClosures = 1 Debug.InlStaticInit = 1 Debug.PGOInline = 1 diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index 99e778fd70..f5d1fce50c 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -5,6 +5,7 @@ package noder import ( + "encoding/hex" "fmt" "go/constant" "internal/buildcfg" @@ -22,6 +23,7 @@ import ( "cmd/compile/internal/staticinit" "cmd/compile/internal/typecheck" "cmd/compile/internal/types" + "cmd/internal/notsha256" "cmd/internal/obj" "cmd/internal/objabi" "cmd/internal/src" @@ -883,7 +885,16 @@ func shapify(targ *types.Type, basic bool) *types.Type { under = types.NewPtr(types.Types[types.TUINT8]) } - sym := types.ShapePkg.Lookup(under.LinkString()) + // Hash long type names to bound symbol name length seen by users, + // particularly for large protobuf structs (#65030). + uls := under.LinkString() + if base.Debug.MaxShapeLen != 0 && + len(uls) > base.Debug.MaxShapeLen { + h := notsha256.Sum256([]byte(uls)) + uls = hex.EncodeToString(h[:]) + } + + sym := types.ShapePkg.Lookup(uls) if sym.Def == nil { name := ir.NewDeclNameAt(under.Pos(), ir.OTYPE, sym) typ := types.NewNamed(name) |