cmd/compile: avoid giant init functions due to many user inits

We generate code that calls each user init function one at a time.
When there are lots of user init functions,
usually due to generated code, like test/rotate* or
github.com/juju/govmomi/vim25/types,
we can end up with a giant function,
which can be slow to compile.

This CL puts in an escape valve.
When there are more than 500 functions, instead of doing:

init.0()
init.1()
// ...

we construct a static array of functions:

var fns = [...]func(){init.0, init.1, ... }

and call them in a loop.

This generates marginally bigger, marginally worse code,
so we restrict it to cases in which it might start to matter.

500 was selected as a mostly arbitrary threshold for "lots".
Each call uses two Progs, one for PCDATA and one for the call,
so at 500 calls we use ~1000 Progs.
At concurrency==8, we get a Prog cache of about
1000 Progs per worker.
So a threshold of 500 should more or less avoid
exhausting the Prog cache in most cases.

Change-Id: I276b887173ddbf65b2164ec9f9b5eb04d8c753c2
Reviewed-on: https://go-review.googlesource.com/41500
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2017-04-23 17:31:15 -07:00
parent b666f2860b
commit d1b544c7eb
2 changed files with 74 additions and 9 deletions

View file

@ -1359,7 +1359,10 @@ func genAsStatic(as *Node) {
Fatalf("genAsStatic: lhs %v", as.Left)
}
if as.Right.Op != OLITERAL {
switch {
case as.Right.Op == OLITERAL:
case as.Right.Op == ONAME && as.Right.Class() == PFUNC:
default:
Fatalf("genAsStatic: rhs %v", as.Right)
}