Skip to content

Commit

Permalink
Speedup dependency resolver by pre-cutting unsolvable branches
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Apr 7, 2023
1 parent e797f1e commit 1e9020d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
22 changes: 19 additions & 3 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (ar *Archive) resolve(solution map[string]Release, depsToProcess []Dependen
// Pick the first dependency in the deps to process
dep := depsToProcess[0]
depName := dep.GetName()
debug("Considering next dep: %s", dep)
debug("Considering next dep: %s", depName)

// If a release is already picked in the solution check if it match the dep
if existingRelease, has := solution[depName]; has {
Expand All @@ -92,11 +92,27 @@ func (ar *Archive) resolve(solution map[string]Release, depsToProcess []Dependen
// Consider the latest versions first
releases.SortDescent()

findMissingDeps := func(deps []Dependency) Dependency {
for _, dep := range deps {
if _, ok := ar.Releases[dep.GetName()]; !ok {
return dep
}
}
return nil
}

debug("releases matching criteria: %s", releases)
for _, release := range releases {
debug("try with %s %s", release, release.GetDependencies())
deps := release.GetDependencies()
debug("try with %s %s", release, deps)

if missingDep := findMissingDeps(deps); missingDep != nil {
debug("%s did not work, becuase his dependency %s does not exists", release, missingDep.GetName())
continue
}

solution[depName] = release
res := ar.resolve(solution, append(depsToProcess[1:], release.GetDependencies()...))
res := ar.resolve(solution, append(depsToProcess[1:], deps...))
if res != nil {
return res
}
Expand Down
3 changes: 2 additions & 1 deletion resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ func TestResolver(t *testing.T) {
d100 := rel("D", "1.0.0", deps())
d120 := rel("D", "1.2.0", deps("E"))
e100 := rel("E", "1.0.0", deps())
e101 := rel("E", "1.0.1", deps("F")) // INVALID
arch := &Archive{
Releases: map[string]Releases{
"B": {b131, b130, b121, b120, b111, b110, b100},
"C": {c200, c120, c111, c110, c102, c101, c100, c021, c020, c010},
"D": {d100, d120},
"E": {e100},
"E": {e100, e101},
},
}

Expand Down

0 comments on commit 1e9020d

Please sign in to comment.