From df2a3bd5316aa25253a52cb51df59d7a8a0b2ce2 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Tue, 28 Nov 2023 16:28:54 +0100 Subject: [PATCH] ASP-based solver: use a unique ID counter (#41290) * solver: use a unique counter for condition, triggers and effects * Do not reset counters when re-running setup What we need is just a unique ID, it doesn't need to start from zero every time. --- lib/spack/spack/solver/asp.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/spack/spack/solver/asp.py b/lib/spack/spack/solver/asp.py index 1716890887..93e9a87583 100644 --- a/lib/spack/spack/solver/asp.py +++ b/lib/spack/spack/solver/asp.py @@ -1117,11 +1117,8 @@ def __init__(self, tests=False): self.reusable_and_possible = ConcreteSpecsByHash() - # id for dummy variables - self._condition_id_counter = itertools.count() - self._trigger_id_counter = itertools.count() + self._id_counter = itertools.count() self._trigger_cache = collections.defaultdict(dict) - self._effect_id_counter = itertools.count() self._effect_cache = collections.defaultdict(dict) # Caches to optimize the setup phase of the solver @@ -1517,7 +1514,7 @@ def condition( # In this way, if a condition can't be emitted but the exception is handled in the caller, # we won't emit partial facts. - condition_id = next(self._condition_id_counter) + condition_id = next(self._id_counter) self.gen.fact(fn.pkg_fact(named_cond.name, fn.condition(condition_id))) self.gen.fact(fn.condition_reason(condition_id, msg)) @@ -1525,7 +1522,7 @@ def condition( named_cond_key = (str(named_cond), transform_required) if named_cond_key not in cache: - trigger_id = next(self._trigger_id_counter) + trigger_id = next(self._id_counter) requirements = self.spec_clauses(named_cond, body=True, required_from=name) if transform_required: @@ -1541,7 +1538,7 @@ def condition( cache = self._effect_cache[named_cond.name] imposed_spec_key = (str(imposed_spec), transform_imposed) if imposed_spec_key not in cache: - effect_id = next(self._effect_id_counter) + effect_id = next(self._id_counter) requirements = self.spec_clauses(imposed_spec, body=False, required_from=name) if transform_imposed: @@ -2549,12 +2546,8 @@ def setup( reuse: list of concrete specs that can be reused allow_deprecated: if True adds deprecated versions into the solve """ - self._condition_id_counter = itertools.count() - - # preliminary checks check_packages_exist(specs) - # get list of all possible dependencies self.possible_virtuals = set(x.name for x in specs if x.virtual) node_counter = _create_counter(specs, tests=self.tests) @@ -2674,8 +2667,8 @@ def setup( def literal_specs(self, specs): for spec in specs: self.gen.h2("Spec: %s" % str(spec)) - condition_id = next(self._condition_id_counter) - trigger_id = next(self._trigger_id_counter) + condition_id = next(self._id_counter) + trigger_id = next(self._id_counter) # Special condition triggered by "literal_solved" self.gen.fact(fn.literal(trigger_id)) @@ -2689,7 +2682,7 @@ def literal_specs(self, specs): "literal specs have different requirements. clear cache before computing literals" ) assert imposed_spec_key not in cache, msg - effect_id = next(self._effect_id_counter) + effect_id = next(self._id_counter) requirements = self.spec_clauses(spec) root_name = spec.name for clause in requirements: