aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder/types.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2021-01-19 10:38:33 -0800
committerMatthew Dempsky <mdempsky@google.com>2021-01-19 21:14:44 +0000
commit90bfc7307175c2f58d4efb48003670dba23385ed (patch)
treeb4fbd0254d4546880c7694b9422e4f05022849bc /src/cmd/compile/internal/noder/types.go
parent3c0a39c964bb149f0a272c396ae3e7b3c4d36e30 (diff)
downloadgo-90bfc7307175c2f58d4efb48003670dba23385ed.tar.gz
go-90bfc7307175c2f58d4efb48003670dba23385ed.zip
[dev.typeparams] cmd/compile: cache mapped types during irgen
If we see the exact same types2.Type a second time, we can map it to the same *types.Type instance. Not strictly necessary, but reduces memory usage and plays better with the rest of the compiler given the current state of things. Change-Id: I53686d072c7c7834b0c97417bc8d5f2cd24572b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/284692 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/noder/types.go')
-rw-r--r--src/cmd/compile/internal/noder/types.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/noder/types.go b/src/cmd/compile/internal/noder/types.go
index 0635d76077f..aec1846619d 100644
--- a/src/cmd/compile/internal/noder/types.go
+++ b/src/cmd/compile/internal/noder/types.go
@@ -26,6 +26,20 @@ func (g *irgen) pkg(pkg *types2.Package) *types.Pkg {
}
func (g *irgen) typ(typ types2.Type) *types.Type {
+ // Caching type mappings isn't strictly needed, because typ0 preserves
+ // type identity; but caching minimizes memory blow-up from mapping the
+ // same composite type multiple times, and also plays better with the
+ // current state of cmd/compile (e.g., haphazard calculation of type
+ // sizes).
+ res, ok := g.typs[typ]
+ if !ok {
+ res = g.typ0(typ)
+ g.typs[typ] = res
+ }
+ return res
+}
+
+func (g *irgen) typ0(typ types2.Type) *types.Type {
switch typ := typ.(type) {
case *types2.Basic:
return g.basic(typ)