Skip to content

Commit 3824d3d

Browse files
prattmicgopherbot
authored andcommitted
cmd/go: always track visited packages in setPGOProfilePath
Currently we only track visited (copied) packages when a copy is required. When a copy is not required, we will rewalk each package's entire dependency graph every time we see it, which is terribly inefficient. Pull the visited package check up a level so that we visit packages only once regardless of how many times they are visited. Fixes #60455. Fixes #60428. Change-Id: I4e9b31eeeaa170db650c461a5de2ca984b9aba0f Reviewed-on: https://go-review.googlesource.com/c/go/+/498735 Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Michael Pratt <[email protected]>
1 parent 6e248b8 commit 3824d3d

File tree

1 file changed

+12
-6
lines changed
  • src/cmd/go/internal/load

1 file changed

+12
-6
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,17 +2950,21 @@ func setPGOProfilePath(pkgs []*Package) {
29502950
continue // no profile
29512951
}
29522952

2953-
copied := make(map[*Package]*Package)
2953+
// Packages already visited. The value should replace
2954+
// the key, as it may be a forked copy of the original
2955+
// Package.
2956+
visited := make(map[*Package]*Package)
29542957
var split func(p *Package) *Package
29552958
split = func(p *Package) *Package {
2959+
if p1 := visited[p]; p1 != nil {
2960+
return p1
2961+
}
2962+
29562963
if len(pkgs) > 1 && p != pmain {
29572964
// Make a copy, then attach profile.
29582965
// No need to copy if there is only one root package (we can
29592966
// attach profile directly in-place).
29602967
// Also no need to copy the main package.
2961-
if p1 := copied[p]; p1 != nil {
2962-
return p1
2963-
}
29642968
if p.Internal.PGOProfile != "" {
29652969
panic("setPGOProfilePath: already have profile")
29662970
}
@@ -2969,9 +2973,11 @@ func setPGOProfilePath(pkgs []*Package) {
29692973
// Unalias the Internal.Imports slice, which is we're going to
29702974
// modify. We don't copy other slices as we don't change them.
29712975
p1.Internal.Imports = slices.Clone(p.Internal.Imports)
2972-
copied[p] = p1
2976+
p1.Internal.ForMain = pmain.ImportPath
2977+
visited[p] = p1
29732978
p = p1
2974-
p.Internal.ForMain = pmain.ImportPath
2979+
} else {
2980+
visited[p] = p
29752981
}
29762982
p.Internal.PGOProfile = file
29772983
updateBuildInfo(p, file)

0 commit comments

Comments
 (0)