Skip to content

Commit 6598c65

Browse files
committed
cmd/compile: fix exponential-time init-cycle reporting
I have a real 7,000-line Go program (not so big) that took over two minutes to report a trivial init cycle. I thought the compiler was in an infinite loop but it was actually just very slow. CL 170062 rewrote init cycle reporting but replaced a linear-time algorithm with an exponential one: it explores all paths through the call graph of functions involved in the cycle. The net effect was that Go 1.12 took 0.25 seconds to load, typecheck, and then diagnose the cycle in my program, while Go 1.13 takes 600X longer. This CL makes the new reporting code run in linear time, restoring the speed of Go 1.12 but preserving the semantic fixes from CL 170062. Change-Id: I7d6dc95676d577d9b96f5953b516a64db93249bf Reviewed-on: https://go-review.googlesource.com/c/go/+/282314 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent fefad1d commit 6598c65

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/cmd/compile/internal/gc/initorder.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func initOrder(l []*Node) []*Node {
108108
errorexit()
109109
}
110110

111-
findInitLoopAndExit(firstLHS(n), new([]*Node))
111+
findInitLoopAndExit(firstLHS(n), new([]*Node), make(map[*Node]bool))
112112
Fatalf("initialization unfinished, but failed to identify loop")
113113
}
114114
}
@@ -181,10 +181,7 @@ func (o *InitOrder) flushReady(initialize func(*Node)) {
181181
// path points to a slice used for tracking the sequence of
182182
// variables/functions visited. Using a pointer to a slice allows the
183183
// slice capacity to grow and limit reallocations.
184-
func findInitLoopAndExit(n *Node, path *[]*Node) {
185-
// We implement a simple DFS loop-finding algorithm. This
186-
// could be faster, but initialization cycles are rare.
187-
184+
func findInitLoopAndExit(n *Node, path *[]*Node, ok map[*Node]bool) {
188185
for i, x := range *path {
189186
if x == n {
190187
reportInitLoopAndExit((*path)[i:])
@@ -201,12 +198,18 @@ func findInitLoopAndExit(n *Node, path *[]*Node) {
201198
*path = append(*path, n)
202199
for _, ref := range refers {
203200
// Short-circuit variables that were initialized.
204-
if ref.Class() == PEXTERN && ref.Name.Defn.Initorder() == InitDone {
201+
if ref.Class() == PEXTERN && ref.Name.Defn.Initorder() == InitDone || ok[ref] {
205202
continue
206203
}
207-
208-
findInitLoopAndExit(ref, path)
204+
findInitLoopAndExit(ref, path, ok)
209205
}
206+
207+
// n is not involved in a cycle.
208+
// Record that fact to avoid checking it again when reached another way,
209+
// or else this traversal will take exponential time traversing all paths
210+
// through the part of the package's call graph implicated in the cycle.
211+
ok[n] = true
212+
210213
*path = (*path)[:len(*path)-1]
211214
}
212215

test/initexp.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// errorcheck -t 10
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
// The init cycle diagnosis used to take exponential time
10+
// to traverse the call graph paths. This test case takes
11+
// at least two minutes on a modern laptop with the bug
12+
// and runs in a fraction of a second without it.
13+
// 10 seconds (-t 10 above) should be plenty if the code is working.
14+
15+
var x = f() + z() // ERROR "initialization loop"
16+
17+
func f() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
18+
func z() int { return x }
19+
20+
func a1() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
21+
func a2() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
22+
func a3() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
23+
func a4() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
24+
func a5() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
25+
func a6() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
26+
func a7() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
27+
func a8() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() }
28+
29+
func b1() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
30+
func b2() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
31+
func b3() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
32+
func b4() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
33+
func b5() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
34+
func b6() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
35+
func b7() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }
36+
func b8() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() }

0 commit comments

Comments
 (0)