From 1e9020de4c2c120f0ccdba325a616a9b10aceee5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 7 Apr 2023 14:52:01 +0200 Subject: [PATCH] Speedup dependency resolver by pre-cutting unsolvable branches --- resolver.go | 22 +++++++++++++++++++--- resolver_test.go | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/resolver.go b/resolver.go index 9f48357..ca469fa 100644 --- a/resolver.go +++ b/resolver.go @@ -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 { @@ -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 } diff --git a/resolver_test.go b/resolver_test.go index 0435427..55b6a5c 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -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}, }, }