1
0
mirror of https://github.com/golang/go synced 2024-11-24 06:40:17 -07:00

go/internal/gcimporter: backport flattenImports optimization

Backport of go.dev/cl/450755 from the x/tools importer.

Change-Id: Ia9a2cc038d589a4cde1b31f74484a5f88dabeef9
Reviewed-on: https://go-review.googlesource.com/c/go/+/450795
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Florian Zenker <floriank@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Matthew Dempsky 2022-11-15 11:08:03 -08:00 committed by Gopher Robot
parent 10661bd952
commit 8373bfe88e

View File

@ -252,22 +252,22 @@ func (r *reader) doPkg() *types.Package {
// packages rooted from pkgs. // packages rooted from pkgs.
func flattenImports(pkgs []*types.Package) []*types.Package { func flattenImports(pkgs []*types.Package) []*types.Package {
var res []*types.Package var res []*types.Package
seen := make(map[*types.Package]struct{})
seen := make(map[*types.Package]bool)
var add func(pkg *types.Package)
add = func(pkg *types.Package) {
if seen[pkg] {
return
}
seen[pkg] = true
res = append(res, pkg)
for _, imp := range pkg.Imports() {
add(imp)
}
}
for _, pkg := range pkgs { for _, pkg := range pkgs {
add(pkg) if _, ok := seen[pkg]; ok {
continue
}
seen[pkg] = struct{}{}
res = append(res, pkg)
// pkg.Imports() is already flattened.
for _, pkg := range pkg.Imports() {
if _, ok := seen[pkg]; ok {
continue
}
seen[pkg] = struct{}{}
res = append(res, pkg)
}
} }
return res return res
} }