mirror of
https://github.com/golang/go.git
synced 2025-10-30 00:00:56 +00:00
cmd/api: speed up API check by 2x, caching parser.ParseFile calls
Saves 5 seconds on my machine. If Issue 4380 is fixed this clone can be removed. Update #4380 R=golang-dev, remyoudompheng, minux.ma, gri CC=golang-dev https://golang.org/cl/6845058
This commit is contained in:
parent
c00bda1352
commit
aeca7a7cd2
3 changed files with 289 additions and 1 deletions
|
|
@ -353,6 +353,21 @@ func fileDeps(f *ast.File) (pkgs []string) {
|
|||
return
|
||||
}
|
||||
|
||||
var parsedFileCache = make(map[string]*ast.File)
|
||||
|
||||
func parseFile(filename string) (*ast.File, error) {
|
||||
f, ok := parsedFileCache[filename]
|
||||
if !ok {
|
||||
var err error
|
||||
f, err = parser.ParseFile(fset, filename, nil, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsedFileCache[filename] = f
|
||||
}
|
||||
return clone(f).(*ast.File), nil
|
||||
}
|
||||
|
||||
// WalkPackage walks all files in package `name'.
|
||||
// WalkPackage does nothing if the package has already been loaded.
|
||||
func (w *Walker) WalkPackage(name string) {
|
||||
|
|
@ -386,7 +401,7 @@ func (w *Walker) WalkPackage(name string) {
|
|||
|
||||
files := append(append([]string{}, info.GoFiles...), info.CgoFiles...)
|
||||
for _, file := range files {
|
||||
f, err := parser.ParseFile(fset, filepath.Join(dir, file), nil, 0)
|
||||
f, err := parseFile(filepath.Join(dir, file))
|
||||
if err != nil {
|
||||
log.Fatalf("error parsing package %s, file %s: %v", name, file, err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue