Skip to content

Commit ad28b93

Browse files
adonovangopherbot
authored andcommitted
go/packages: minor cleanups to loader.parseFiles
Details: - Break out of overlay loop when a match is found. This is a (theoretical) optimization. - Document optimization opportunities: one TODO, and one not worth doing. - Clarify some comments. - Use errgroup instead of WaitGroup (more concise). No intended behavior changes. Updates golang/go#70094 Change-Id: I27e34adb1176b65b4fae60e7d67bb4d2f4b19a14 Reviewed-on: https://go-review.googlesource.com/c/tools/+/624815 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent cceaf96 commit ad28b93

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

go/packages/packages.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,15 +1313,20 @@ func (ld *loader) parseFile(filename string) (*ast.File, error) {
13131313

13141314
var src []byte
13151315
for f, contents := range ld.Config.Overlay {
1316+
// TODO(adonovan): Inefficient for large overlays.
1317+
// Do an exact name-based map lookup
1318+
// (for nonexistent files) followed by a
1319+
// FileID-based map lookup (for existing ones).
13161320
if sameFile(f, filename) {
13171321
src = contents
1322+
break
13181323
}
13191324
}
13201325
var err error
13211326
if src == nil {
1322-
ioLimit <- true // wait
1327+
ioLimit <- true // acquire a token
13231328
src, err = os.ReadFile(filename)
1324-
<-ioLimit // signal
1329+
<-ioLimit // release a token
13251330
}
13261331
if err != nil {
13271332
v.err = err
@@ -1341,18 +1346,21 @@ func (ld *loader) parseFile(filename string) (*ast.File, error) {
13411346
// Because files are scanned in parallel, the token.Pos
13421347
// positions of the resulting ast.Files are not ordered.
13431348
func (ld *loader) parseFiles(filenames []string) ([]*ast.File, []error) {
1344-
var wg sync.WaitGroup
1345-
n := len(filenames)
1346-
parsed := make([]*ast.File, n)
1347-
errors := make([]error, n)
1348-
for i, file := range filenames {
1349-
wg.Add(1)
1350-
go func(i int, filename string) {
1349+
var (
1350+
n = len(filenames)
1351+
parsed = make([]*ast.File, n)
1352+
errors = make([]error, n)
1353+
)
1354+
var g errgroup.Group
1355+
for i, filename := range filenames {
1356+
// This creates goroutines unnecessarily in the
1357+
// cache-hit case, but that case is uncommon.
1358+
g.Go(func() error {
13511359
parsed[i], errors[i] = ld.parseFile(filename)
1352-
wg.Done()
1353-
}(i, file)
1360+
return nil
1361+
})
13541362
}
1355-
wg.Wait()
1363+
g.Wait()
13561364

13571365
// Eliminate nils, preserving order.
13581366
var o int

0 commit comments

Comments
 (0)