Blame SOURCES/0003-Improve-choice-rule-generation.patch

4a319f
From fd52342713012ec111b1559fa1647f88768fa782 Mon Sep 17 00:00:00 2001
4a319f
From: Michael Schroeder <mls@suse.de>
4a319f
Date: Wed, 16 Mar 2022 15:17:20 +0100
4a319f
Subject: [PATCH 3/3] Improve choice rule generation
4a319f
4a319f
New strategy: take the package update map into account when creating
4a319f
choice rules. This simplifies the code quite a bit. Also check at
4a319f
the end of the solving if a choice rule led to two packages providing
4a319f
the same thing. In that case, break the choice rule and re-run the
4a319f
solver.
4a319f
---
4a319f
 src/rules.c  | 169 +++++++++++++--------------------------------------
4a319f
 src/solver.c |  45 ++++++++++++++
4a319f
 2 files changed, 88 insertions(+), 126 deletions(-)
4a319f
4a319f
diff --git a/src/rules.c b/src/rules.c
4a319f
index 212df322..324fe5da 100644
4a319f
--- a/src/rules.c
4a319f
+++ b/src/rules.c
4a319f
@@ -3139,6 +3139,12 @@ solver_rule2rules(Solver *solv, Id rid, Queue *q, int recursive)
4a319f
 
4a319f
 
4a319f
 /* check if the newest versions of pi still provides the dependency we're looking for */
4a319f
+/* pi: installed package
4a319f
+ * r: rule for the dependency
4a319f
+ * m: map with all positive elements of r
4a319f
+ * return 0: at least one provider
4a319f
+ * return 1: the newest versions do not provide the dependency
4a319f
+ */
4a319f
 static int
4a319f
 solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
4a319f
 {
4a319f
@@ -3187,94 +3193,6 @@ solver_choicerulecheck(Solver *solv, Id pi, Rule *r, Map *m, Queue *q)
4a319f
   return 1;	/* none of the new packages provided it */
4a319f
 }
4a319f
 
4a319f
-static int
4a319f
-solver_choicerulecheck2(Solver *solv, Id pi, Id pt, Queue *q)
4a319f
-{
4a319f
-  Pool *pool = solv->pool;
4a319f
-  Rule *ur;
4a319f
-  Id p, pp;
4a319f
-  int i;
4a319f
-
4a319f
-  if (!q->count || q->elements[0] != pi)
4a319f
-    {
4a319f
-      if (q->count)
4a319f
-        queue_empty(q);
4a319f
-      ur = solv->rules + solv->updaterules + (pi - pool->installed->start);
4a319f
-      if (!ur->p)
4a319f
-        ur = solv->rules + solv->featurerules + (pi - pool->installed->start);
4a319f
-      if (!ur->p)
4a319f
-	return 1;		/* orphaned, thus newest */
4a319f
-      queue_push2(q, pi, 0);
4a319f
-      FOR_RULELITERALS(p, pp, ur)
4a319f
-	if (p > 0 && p != pi)
4a319f
-	  queue_push(q, p);
4a319f
-      queue_push(q, pi);
4a319f
-    }
4a319f
-  if (q->count <= 3)
4a319f
-    return q->count == 3 && q->elements[2] == pt ? 1 : 0;
4a319f
-  if (!q->elements[1])
4a319f
-    {
4a319f
-      queue_deleten(q, 0, 2);
4a319f
-      policy_filter_unwanted(solv, q, POLICY_MODE_CHOOSE);
4a319f
-      queue_unshift(q, 1);	/* filter mark */
4a319f
-      queue_unshift(q, pi);
4a319f
-    }
4a319f
-  for (i = 2; i < q->count; i++)
4a319f
-    if (q->elements[i] == pt)
4a319f
-      return 1;
4a319f
-  return 0;	/* not newest */
4a319f
-}
4a319f
-
4a319f
-static int
4a319f
-solver_choicerulecheck3(Solver *solv, Id pt, Queue *q)
4a319f
-{
4a319f
-  Pool *pool = solv->pool;
4a319f
-  Id p, pp;
4a319f
-  int i;
4a319f
-
4a319f
-  if (!q->count || q->elements[0] != pt)
4a319f
-    {
4a319f
-      Solvable *s = pool->solvables + pt;
4a319f
-      if (q->count)
4a319f
-        queue_empty(q);
4a319f
-      /* no installed package, so check all with same name */
4a319f
-      queue_push2(q, pt, 0);
4a319f
-      FOR_PROVIDES(p, pp, s->name)
4a319f
-        if (pool->solvables[p].name == s->name && p != pt)
4a319f
-          queue_push(q, p);
4a319f
-      queue_push(q, pt);
4a319f
-    }
4a319f
-  if (q->count <= 3)
4a319f
-    return q->count == 3 && q->elements[2] == pt ? 1 : 0;
4a319f
-  if (!q->elements[1])
4a319f
-    {
4a319f
-      queue_deleten(q, 0, 2);
4a319f
-      policy_filter_unwanted(solv, q, POLICY_MODE_CHOOSE);
4a319f
-      queue_unshift(q, 1);	/* filter mark */
4a319f
-      queue_unshift(q, pt);
4a319f
-    }
4a319f
-  for (i = 2; i < q->count; i++)
4a319f
-    if (q->elements[i] == pt)
4a319f
-      return 1;
4a319f
-  return 0;	/* not newest */
4a319f
-}
4a319f
-
4a319f
-static inline void
4a319f
-queue_removeelement(Queue *q, Id el)
4a319f
-{
4a319f
-  int i, j;
4a319f
-  for (i = 0; i < q->count; i++)
4a319f
-    if (q->elements[i] == el)
4a319f
-      break;
4a319f
-  if (i < q->count)
4a319f
-    {
4a319f
-      for (j = i++; i < q->count; i++)
4a319f
-	if (q->elements[i] != el)
4a319f
-	  q->elements[j++] = q->elements[i];
4a319f
-      queue_truncate(q, j);
4a319f
-    }
4a319f
-}
4a319f
-
4a319f
 static Id
4a319f
 choicerule_find_installed(Pool *pool, Id p)
4a319f
 {
4a319f
@@ -3323,14 +3241,14 @@ solver_addchoicerules(Solver *solv)
4a319f
   Pool *pool = solv->pool;
4a319f
   Map m, mneg;
4a319f
   Rule *r;
4a319f
-  Queue q, qi, qcheck, qcheck2, infoq;
4a319f
+  Queue q, qi, qcheck, infoq;
4a319f
   int i, j, rid, havechoice, negcnt;
4a319f
   Id p, d, pp, p2;
4a319f
   Solvable *s;
4a319f
   Id lastaddedp, lastaddedd;
4a319f
   int lastaddedcnt;
4a319f
   unsigned int now;
4a319f
-  int isnewest = 0;
4a319f
+  int isinstalled;
4a319f
 
4a319f
   solv->choicerules = solv->nrules;
4a319f
   if (!pool->installed)
4a319f
@@ -3342,7 +3260,6 @@ solver_addchoicerules(Solver *solv)
4a319f
   queue_init(&q);
4a319f
   queue_init(&qi);
4a319f
   queue_init(&qcheck);
4a319f
-  queue_init(&qcheck2);
4a319f
   queue_init(&infoq);
4a319f
   map_init(&m, pool->nsolvables);
4a319f
   map_init(&mneg, pool->nsolvables);
4a319f
@@ -3362,20 +3279,28 @@ solver_addchoicerules(Solver *solv)
4a319f
       if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 <= 0))
4a319f
 	continue;	/* only look at requires rules */
4a319f
       /* solver_printrule(solv, SOLV_DEBUG_RESULT, r); */
4a319f
-      queue_empty(&q);
4a319f
       queue_empty(&qi);
4a319f
       havechoice = 0;
4a319f
+      isinstalled = 0;
4a319f
       FOR_RULELITERALS(p, pp, r)
4a319f
 	{
4a319f
 	  if (p < 0)
4a319f
-	    continue;
4a319f
+	    {
4a319f
+	      Solvable *s = pool->solvables - p;
4a319f
+	      p2 = s->repo == pool->installed ? -p : 0;
4a319f
+	      if (p2)
4a319f
+		{
4a319f
+		  if (!(solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p2 - solv->installed->start))))
4a319f
+		    isinstalled = 1;
4a319f
+		}
4a319f
+	      continue;
4a319f
+	    }
4a319f
 	  s = pool->solvables + p;
4a319f
 	  if (!s->repo)
4a319f
 	    continue;
4a319f
 	  if (s->repo == pool->installed)
4a319f
 	    {
4a319f
 	      queue_push2(&qi, p, p);
4a319f
-	      queue_push(&q, p);
4a319f
 	      continue;
4a319f
 	    }
4a319f
 	  /* find an installed package p2 that we can update/downgrade to p */
4a319f
@@ -3387,7 +3312,6 @@ solver_addchoicerules(Solver *solv)
4a319f
 	      if (policy_is_illegal(solv, pool->solvables + p2, s, 0))
4a319f
 		continue;
4a319f
 	      queue_push2(&qi, p2, p);
4a319f
-	      queue_push(&q, p);
4a319f
 	      continue;
4a319f
 	    }
4a319f
 	  /* package p is independent of the installed ones */
4a319f
@@ -3396,47 +3320,31 @@ solver_addchoicerules(Solver *solv)
4a319f
 #if 0
4a319f
       printf("havechoice: %d qcount %d qicount %d\n", havechoice, q.count, qi.count);
4a319f
 #endif
4a319f
-      if (!havechoice || !q.count || !qi.count)
4a319f
+      if (!havechoice || !qi.count)
4a319f
 	continue;	/* no choice */
4a319f
 
4a319f
       FOR_RULELITERALS(p, pp, r)
4a319f
         if (p > 0)
4a319f
 	  MAPSET(&m, p);
4a319f
 
4a319f
-      isnewest = 1;
4a319f
-      FOR_RULELITERALS(p, pp, r)
4a319f
-	{
4a319f
-	  if (p > 0)
4a319f
-	    break;
4a319f
-	  p2 = choicerule_find_installed(pool, -p);
4a319f
-	  if (p2 && !solver_choicerulecheck2(solv, p2, -p, &qcheck2))
4a319f
-	    {
4a319f
-	      isnewest = 0;
4a319f
-	      break;
4a319f
-	    }
4a319f
-	  if (!p2 && !solver_choicerulecheck3(solv, -p, &qcheck2))
4a319f
-	    {
4a319f
-	      isnewest = 0;
4a319f
-	      break;
4a319f
-	    }
4a319f
-	}
4a319f
-      /* do extra checking */
4a319f
-      for (i = j = 0; i < qi.count; i += 2)
4a319f
+      if (!isinstalled)
4a319f
 	{
4a319f
-	  p2 = qi.elements[i];
4a319f
-	  if (!p2)
4a319f
-	    continue;
4a319f
-	  if (isnewest && solver_choicerulecheck(solv, p2, r, &m, &qcheck))
4a319f
+	  /* do extra checking for packages related to installed packages */
4a319f
+	  for (i = j = 0; i < qi.count; i += 2)
4a319f
 	    {
4a319f
-	      /* oops, remove element p from q */
4a319f
-	      queue_removeelement(&q, qi.elements[i + 1]);
4a319f
-	      continue;
4a319f
+	      p2 = qi.elements[i];
4a319f
+	      if (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p2 - solv->installed->start)))
4a319f
+		{
4a319f
+		  if (solver_choicerulecheck(solv, p2, r, &m, &qcheck))
4a319f
+		    continue;
4a319f
+		}
4a319f
+	      qi.elements[j++] = p2;
4a319f
+	      qi.elements[j++] = qi.elements[i + 1];
4a319f
 	    }
4a319f
-	  qi.elements[j++] = p2;
4a319f
+	  queue_truncate(&qi, j);
4a319f
 	}
4a319f
-      queue_truncate(&qi, j);
4a319f
 
4a319f
-      if (!q.count || !qi.count)
4a319f
+      if (!qi.count)
4a319f
 	{
4a319f
 	  FOR_RULELITERALS(p, pp, r)
4a319f
 	    if (p > 0)
4a319f
@@ -3444,6 +3352,15 @@ solver_addchoicerules(Solver *solv)
4a319f
 	  continue;
4a319f
 	}
4a319f
 
4a319f
+      queue_empty(&q);
4a319f
+      /* split q from qi */
4a319f
+      for (i = j = 0; i < qi.count; i += 2)
4a319f
+	{
4a319f
+	  queue_push(&q, qi.elements[i + 1]);
4a319f
+	  qi.elements[j++] = qi.elements[i];
4a319f
+	}
4a319f
+      queue_truncate(&qi, j);
4a319f
+
4a319f
 
4a319f
       /* now check the update rules of the installed package.
4a319f
        * if all packages of the update rules are contained in
4a319f
@@ -3463,6 +3380,7 @@ solver_addchoicerules(Solver *solv)
4a319f
 	      break;
4a319f
 	  if (p)
4a319f
 	    break;
4a319f
+	  /* speed improvement: only check each package once */
4a319f
 	  for (j = i + 1; j < qi.count; j++)
4a319f
 	    if (qi.elements[i] == qi.elements[j])
4a319f
 	      qi.elements[j] = 0;
4a319f
@@ -3520,7 +3438,6 @@ solver_addchoicerules(Solver *solv)
4a319f
   queue_free(&q);
4a319f
   queue_free(&qi);
4a319f
   queue_free(&qcheck);
4a319f
-  queue_free(&qcheck2);
4a319f
   queue_free(&infoq);
4a319f
   map_free(&m);
4a319f
   map_free(&mneg);
4a319f
diff --git a/src/solver.c b/src/solver.c
4a319f
index 1dc2c783..08029baa 100644
4a319f
--- a/src/solver.c
4a319f
+++ b/src/solver.c
4a319f
@@ -2615,6 +2615,43 @@ resolve_orphaned(Solver *solv, int level, int disablerules, Queue *dq, int *reru
4a319f
   return level;
4a319f
 }
4a319f
 
4a319f
+int
4a319f
+solver_check_unneeded_choicerules(Solver *solv)
4a319f
+{
4a319f
+  Pool *pool = solv->pool;
4a319f
+  Rule *r, *or;
4a319f
+  Id p, pp, p2, pp2;
4a319f
+  int i;
4a319f
+  int havedisabled = 0;
4a319f
+
4a319f
+  /* check if some choice rules could have been broken */
4a319f
+  for (i = solv->choicerules, r = solv->rules + i; i < solv->choicerules_end; i++, r++)
4a319f
+    {
4a319f
+      if (r->d < 0)
4a319f
+	continue;
4a319f
+      or = solv->rules + solv->choicerules_info[i - solv->choicerules];
4a319f
+      if (or->d < 0)
4a319f
+	continue;
4a319f
+      FOR_RULELITERALS(p, pp, or)
4a319f
+	{
4a319f
+	  if (p < 0 || solv->decisionmap[p] <= 0)
4a319f
+	    continue;
4a319f
+	  FOR_RULELITERALS(p2, pp2, r)
4a319f
+	    if (p2 == p)
4a319f
+	      break;
4a319f
+	  if (!p2)
4a319f
+	    {
4a319f
+	      /* did not find p in choice rule, disable it */
4a319f
+	      POOL_DEBUG(SOLV_DEBUG_SOLVER, "disabling unneeded choice rule #%d\n", i);
4a319f
+	      solver_disablechoicerules(solv, r);
4a319f
+	      havedisabled = 1;
4a319f
+	      break;
4a319f
+	    }
4a319f
+	}
4a319f
+    }
4a319f
+  return havedisabled;
4a319f
+}
4a319f
+
4a319f
 /*-------------------------------------------------------------------
4a319f
  *
4a319f
  * solver_run_sat
4a319f
@@ -2800,6 +2837,14 @@ solver_run_sat(Solver *solv, int disablerules, int doweak)
4a319f
 	  continue;
4a319f
 	}
4a319f
 
4a319f
+      if (solv->choicerules != solv->choicerules_end && solver_check_unneeded_choicerules(solv))
4a319f
+	{
4a319f
+	  POOL_DEBUG(SOLV_DEBUG_SOLVER, "did choice rule minimization, rerunning solver\n");
4a319f
+	  solver_reset(solv);
4a319f
+	  level = 0;	/* restart from scratch */
4a319f
+	  continue;
4a319f
+	}
4a319f
+
4a319f
       if (solv->solution_callback)
4a319f
 	{
4a319f
 	  solv->solution_callback(solv, solv->solution_callback_data);
4a319f
-- 
4a319f
2.31.1
4a319f