diff options
author | Matthew Dempsky <mdempsky@google.com> | 2021-05-18 01:20:13 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2021-05-26 19:58:01 +0000 |
commit | 95748d1b741d2c612cf90d9b6f4f8bdb81800e23 (patch) | |
tree | 62e6cfa627f205fda0d94f0e4151da2570cfadaf /src/cmd | |
parent | fd54ae8b0c7ed3ef9869112586069f7cac82cf1e (diff) | |
download | go-95748d1b741d2c612cf90d9b6f4f8bdb81800e23.tar.gz go-95748d1b741d2c612cf90d9b6f4f8bdb81800e23.zip |
[dev.typeparams] cmd/compile: avoid some redundant type construction
This CL updates noder and typecheck to avoid a couple of instances of
redundant evaluation of type expressions:
1. When noding struct fields or parameter tuples, check for
syntax.Type reuse between adjacent fields and then reuse the
corresponding ir.Node type expression. It would perhaps be even better
to avoid re-noding the type expression too, but noder's days are
numbered anyway, so I'd rather be minimally invasive here.
2. When importing an empty interface, reuse the same cached empty
interface instance that is used for empty interfaces that appear in
source. This matches types2's behavior, which uses a single
types2.Interface instance for all empty interfaces.
These changes are motivated by making it possible to migrate from
typecheck to types2 while passing toolstash -cmp.
Updates #46208.
Change-Id: Ia6458894494464d863181db356f3284630c90ffe
Reviewed-on: https://go-review.googlesource.com/c/go/+/320789
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/compile/internal/noder/noder.go | 6 | ||||
-rw-r--r-- | src/cmd/compile/internal/typecheck/iimport.go | 4 |
2 files changed, 10 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 4c7c9fc322..06c3b00601 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -625,6 +625,9 @@ func (p *noder) params(params []*syntax.Field, dddOk bool) []*ir.Field { for i, param := range params { p.setlineno(param) nodes = append(nodes, p.param(param, dddOk, i+1 == len(params))) + if i > 0 && params[i].Type == params[i-1].Type { + nodes[i].Ntype = nodes[i-1].Ntype + } } return nodes } @@ -917,6 +920,9 @@ func (p *noder) structType(expr *syntax.StructType) ir.Node { } else { n = ir.NewField(p.pos(field), p.name(field.Name), p.typeExpr(field.Type), nil) } + if i > 0 && expr.FieldList[i].Type == expr.FieldList[i-1].Type { + n.Ntype = l[i-1].Ntype + } if i < len(expr.TagList) && expr.TagList[i] != nil { n.Note = constant.StringVal(p.basicLit(expr.TagList[i])) } diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index 3fb675f824..16b3e7ceba 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -745,6 +745,10 @@ func (r *importReader) typ1() *types.Type { methods[i] = types.NewField(pos, sym, typ) } + if len(embeddeds)+len(methods) == 0 { + return types.Types[types.TINTER] + } + t := types.NewInterface(r.currPkg, append(embeddeds, methods...)) // Ensure we expand the interface in the frontend (#25055). |