Skip to content

Commit 5a42af7

Browse files
Robert Griesemergopherbot
authored andcommitted
go/types, types2: in resolveUnderlying, only compute path when needed
When following a RHS chain, the (TypeName) Object path is only needed when there is a cycle (i.e., an error), in which case we can be slow. Rather than always compute the path, only compute it in the error case. In the same vain, allocate the seen map lazily, only when needed. This code could use a test (it doesn't seem to be encountered by our test suite), but I haven't found a case to provoke the error yet. Change-Id: Iff6313394442a251adc56580f746928ec13450fd Reviewed-on: https://go-review.googlesource.com/c/go/+/712321 Auto-Submit: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
1 parent 4bdb55b commit 5a42af7

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/cmd/compile/internal/types2/named.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,7 @@ func (n *Named) resolveUnderlying() {
593593
return
594594
}
595595

596-
seen := make(map[*Named]int)
597-
var path []Object
598-
596+
var seen map[*Named]int // allocated lazily
599597
var u Type
600598
for rhs := Type(n); u == nil; {
601599
switch t := rhs.(type) {
@@ -607,9 +605,15 @@ func (n *Named) resolveUnderlying() {
607605

608606
case *Named:
609607
if i, ok := seen[t]; ok {
608+
// compute cycle path
609+
path := make([]Object, len(seen))
610+
for t, j := range seen {
611+
path[j] = t.obj
612+
}
613+
path = path[i:]
610614
// Note: This code may only be called during type checking,
611615
// hence n.check != nil.
612-
n.check.cycleError(path[i:], firstInSrc(path[i:]))
616+
n.check.cycleError(path, firstInSrc(path))
613617
u = Typ[Invalid]
614618
break
615619
}
@@ -625,8 +629,10 @@ func (n *Named) resolveUnderlying() {
625629
break
626630
}
627631

632+
if seen == nil {
633+
seen = make(map[*Named]int)
634+
}
628635
seen[t] = len(seen)
629-
path = append(path, t.obj)
630636

631637
assert(t.fromRHS != nil || t.allowNilRHS)
632638
rhs = t.fromRHS

src/go/types/named.go

Lines changed: 11 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)