Skip to content

Commit 673e19c

Browse files
committed
fix: avoid VERY long source scans via caching
1 parent 2c2ce54 commit 673e19c

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

gosrc/deps-tree.go

+34-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func (p *Pkg) Resolve() {
4343
if name == "" {
4444
return
4545
}
46-
p.Depth = p.depth()
4746

4847
// Stop resolving imports if we've reached a loop.
4948
var importMode build.ImportMode
@@ -108,14 +107,26 @@ func (p *Pkg) setDeps(imports []string, parentDir string) {
108107

109108
// addDep creates a Pkg and it's dependencies from an imported package name.
110109
func (p *Pkg) addDep(name string, parentDir string) {
111-
dep := Pkg{
112-
Name: name,
113-
Tree: p.Tree,
114-
//TODO: maybe better pass ParentDir as a param to Resolve() instead
115-
ParentDir: parentDir,
116-
Parent: p,
110+
var dep Pkg
111+
cached := p.Tree.getCachedPkg(name)
112+
if cached != nil {
113+
dep = *cached
114+
dep.ParentDir = parentDir
115+
dep.Parent = p
116+
} else {
117+
dep = Pkg{
118+
Name: name,
119+
Tree: p.Tree,
120+
//TODO: maybe better pass ParentDir as a param to Resolve() instead
121+
ParentDir: parentDir,
122+
Parent: p,
123+
}
124+
dep.Resolve()
125+
126+
p.Tree.cacheResolvedPackage(&dep)
117127
}
118-
dep.Resolve()
128+
129+
p.Depth = p.depth()
119130

120131
if dep.IsBuiltin || dep.Name == "C" {
121132
return
@@ -200,6 +211,8 @@ type Tree struct {
200211

201212
UnresolvedPkgs map[string]struct{}
202213

214+
PkgCache map[string]*Pkg
215+
203216
importCache map[string]struct{}
204217
}
205218

@@ -221,6 +234,7 @@ func (t *Tree) Resolve(name string) error {
221234
// reuse the same cache.
222235
t.importCache = map[string]struct{}{}
223236
t.UnresolvedPkgs = map[string]struct{}{}
237+
t.PkgCache = map[string]*Pkg{}
224238

225239
t.Root.Resolve()
226240
if !t.Root.IsResolved {
@@ -244,6 +258,18 @@ func (t *Tree) rememverUnresolvedPkg(name string) {
244258
t.UnresolvedPkgs[name] = struct{}{}
245259
}
246260

261+
func (t *Tree) cacheResolvedPackage(pkg *Pkg) {
262+
t.PkgCache[pkg.Name] = pkg
263+
}
264+
265+
func (t *Tree) getCachedPkg(name string) *Pkg {
266+
pkg, ok := t.PkgCache[name]
267+
if !ok {
268+
return nil
269+
}
270+
return pkg
271+
}
272+
247273
func prettyPrintJSON(j interface{}) {
248274
e := json.NewEncoder(os.Stdout)
249275
e.SetIndent("", " ")

0 commit comments

Comments
 (0)