From 8373bfe88e92a77a498caeab2ca729330b007483 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Tue, 15 Nov 2022 11:08:03 -0800 Subject: [PATCH] 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 Reviewed-by: Florian Zenker Run-TryBot: Matthew Dempsky Reviewed-by: David Chase TryBot-Result: Gopher Robot --- src/go/internal/gcimporter/ureader.go | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/go/internal/gcimporter/ureader.go b/src/go/internal/gcimporter/ureader.go index 94c1f2802a2..c2b9a58ee74 100644 --- a/src/go/internal/gcimporter/ureader.go +++ b/src/go/internal/gcimporter/ureader.go @@ -252,22 +252,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 }