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

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