mirror of https://github.com/golang/go.git
internal/gcimporter: fix performance regression for unified IR
flattenImports used to traverse the transitive closure of all imports by recursively visiting all imported packages. This is a lot of redundant work, because flattening happens for every package, the recursion is not necessary and this change removes the recursion. Change-Id: Id5a1b3b15ef3ce8e0fc6c945b5484b3dd06bd6b6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/450755 Run-TryBot: Matthew Dempsky <mdempsky@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Florian Zenker <floriank@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
1cb4c17983
commit
fc702c522d
|
|
@ -259,22 +259,22 @@ func (r *reader) doPkg() *types.Package {
|
|||
// packages rooted from pkgs.
|
||||
func flattenImports(pkgs []*types.Package) []*types.Package {
|
||||
var res []*types.Package
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
seen := make(map[*types.Package]struct{})
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue