Skip to content

Commit

Permalink
cmd/go: delay parsing the testmain template
Browse files Browse the repository at this point in the history
The template is over a hundred lines and full of pipelines, and
text/template isn't optimised to parse quickly, so it's no wonder that
delaying the parsing to the first template use makes 'go env' much
faster.

Like in the previous patches to get rid of global regexp.MustCompile
vars, use the newly introduced lazytemplate package. Close to two full
milliseconds are shaved off of 'go env' runs.

name         old time/op    new time/op    delta
ExecGoEnv-8    4.27ms ± 0%    2.63ms ± 1%  -38.43%  (p=0.002 n=6+6)

Updates #29382.

Change-Id: I4e2569e51ddf2afe1b46eb1a9e9e5845f7a3b0bd
Reviewed-on: https://go-review.googlesource.com/c/155962
Run-TryBot: Daniel Martí <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
mvdan committed Feb 28, 2019
1 parent 61170f8 commit 6a72dd7
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/cmd/go/internal/load/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"go/doc"
"go/parser"
"go/token"
"internal/lazytemplate"
"path/filepath"
"sort"
"strings"
"text/template"
"unicode"
"unicode/utf8"
)
Expand Down Expand Up @@ -556,7 +556,7 @@ func checkTestFunc(fn *ast.FuncDecl, arg string) error {
return nil
}

var testmainTmpl = template.Must(template.New("main").Parse(`
var testmainTmpl = lazytemplate.New("main", `
package main
import (
Expand Down Expand Up @@ -657,4 +657,4 @@ func main() {
{{end}}
}
`))
`)

0 comments on commit 6a72dd7

Please sign in to comment.