]> Cypherpunks repositories - gostls13.git/commitdiff
all: use go/parser.SkipObjectResolution in more places
authorDaniel Martí <mvdan@mvdan.cc>
Thu, 21 Apr 2022 13:17:40 +0000 (14:17 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 23 Aug 2022 18:54:32 +0000 (18:54 +0000)
None of cgo, "go test", nor srcimporter make use of go/ast's object
resolution via go/ast.Object. As such, we can skip that work during
parse time, which should save some CPU time.

We don't have any benchmark numbers, as none of the three packages have
any usable benchmarks, but we measured gofmt to be about 5% faster
thanks to this tweak in https://go.dev/cl/401454.
These three packages are quite different to gofmt, but one can expect
similar speed-ups in the 1-5% range.

Two notable exceptions, which do make use of go/ast.Object, are cmd/fix
and cmd/doc - we do not modify those here.

See #46485.

Change-Id: Ie3e65600d4790641c4e4d6f1c379be477fa02cee
Reviewed-on: https://go-review.googlesource.com/c/go/+/401455
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/cgo/ast.go
src/cmd/go/internal/load/test.go
src/go/internal/srcimporter/srcimporter.go

index 28879e349c49cf371b40a7fb0d09f2e4b285f549..c419699cb1a9ae6fd6b02667eedd9c6617e49909 100644 (file)
@@ -52,8 +52,8 @@ func (f *File) ParseGo(abspath string, src []byte) {
        // and reprinting.
        // In cgo mode, we ignore ast2 and just apply edits directly
        // the text behind ast1. In godefs mode we modify and print ast2.
-       ast1 := parse(abspath, src, parser.ParseComments)
-       ast2 := parse(abspath, src, 0)
+       ast1 := parse(abspath, src, parser.SkipObjectResolution|parser.ParseComments)
+       ast2 := parse(abspath, src, parser.SkipObjectResolution)
 
        f.Package = ast1.Name.Name
        f.Name = make(map[string]*Name)
index 3780f358f4b3d513563b6ef97cfa8d4b478c66de..1abefd8ad1cd705236357fde1f4535fe84230f4e 100644 (file)
@@ -611,7 +611,7 @@ func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
                return err
        }
        defer src.Close()
-       f, err := parser.ParseFile(testFileSet, filename, src, parser.ParseComments)
+       f, err := parser.ParseFile(testFileSet, filename, src, parser.ParseComments|parser.SkipObjectResolution)
        if err != nil {
                return err
        }
index caf76a24deed276440996bcd94ae126bf28dacb8..c96427486e872eaee938efec7235139c9d4ba319 100644 (file)
@@ -182,7 +182,7 @@ func (p *Importer) parseFiles(dir string, filenames []string) ([]*ast.File, erro
                                errors[i] = err // open provides operation and filename in error
                                return
                        }
-                       files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, 0)
+                       files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, parser.SkipObjectResolution)
                        src.Close() // ignore Close error - parsing may have succeeded which is all we need
                }(i, p.joinPath(dir, filename))
        }
@@ -240,7 +240,7 @@ func (p *Importer) cgo(bp *build.Package) (*ast.File, error) {
                return nil, fmt.Errorf("go tool cgo: %w", err)
        }
 
-       return parser.ParseFile(p.fset, filepath.Join(tmpdir, "_cgo_gotypes.go"), nil, 0)
+       return parser.ParseFile(p.fset, filepath.Join(tmpdir, "_cgo_gotypes.go"), nil, parser.SkipObjectResolution)
 }
 
 // context-controlled file system operations