From 10cb803f1e2028947b67983459ae364db7032fc7 Mon Sep 17 00:00:00 2001 From: konsti Date: Fri, 20 Dec 2024 17:01:28 +0100 Subject: [PATCH] Add `State::add_incompatibility_from_dependencies` (#27) (#300) This wrapper avoids accessing the `incompatibility_store` directly in uv code. Before: ```rust let dep_incompats = self.pubgrub.add_version( package.clone(), version.clone(), dependencies, ); self.pubgrub.partial_solution.add_version( package.clone(), version.clone(), dep_incompats, &self.pubgrub.incompatibility_store, ); ``` After: ```rust self.pubgrub.add_incompatibility_from_dependencies(package.clone(), version.clone(), dependencies); ``` `add_incompatibility_from_dependencies` is one of the main methods for the custom interface to pubgrub. --- src/internal/core.rs | 21 +++++++++++++++++++-- src/internal/partial_solution.rs | 15 +++++++++------ src/solver.rs | 12 +++--------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/internal/core.rs b/src/internal/core.rs index 4dc5d447..1b82c1af 100644 --- a/src/internal/core.rs +++ b/src/internal/core.rs @@ -7,8 +7,8 @@ use std::collections::HashSet as Set; use std::sync::Arc; use crate::internal::{ - Arena, DecisionLevel, HashArena, Id, IncompDpId, Incompatibility, PartialSolution, Relation, - SatisfierSearch, SmallVec, + Arena, DecisionLevel, HashArena, Id, IncompDpId, IncompId, Incompatibility, PartialSolution, + Relation, SatisfierSearch, SmallVec, }; use crate::{DependencyProvider, DerivationTree, Map, NoSolutionError, VersionSet}; @@ -73,6 +73,23 @@ impl State { } } + /// Add the dependencies for the current version of the current package as incompatibilities. + pub(crate) fn add_package_version_dependencies( + &mut self, + package: Id, + version: DP::V, + dependencies: impl IntoIterator, + ) -> Option> { + let dep_incompats = + self.add_incompatibility_from_dependencies(package, version.clone(), dependencies); + self.partial_solution.add_package_version_incompatibilities( + package, + version.clone(), + dep_incompats, + &self.incompatibility_store, + ) + } + /// Add an incompatibility to the state. pub(crate) fn add_incompatibility(&mut self, incompat: Incompatibility) { let id = self.incompatibility_store.alloc(incompat); diff --git a/src/internal/partial_solution.rs b/src/internal/partial_solution.rs index f1345634..2ae6cae8 100644 --- a/src/internal/partial_solution.rs +++ b/src/internal/partial_solution.rs @@ -417,12 +417,15 @@ impl PartialSolution { self.has_ever_backtracked = true; } - /// We can add the version to the partial solution as a decision - /// if it doesn't produce any conflict with the new incompatibilities. - /// In practice I think it can only produce a conflict if one of the dependencies - /// (which are used to make the new incompatibilities) - /// is already in the partial solution with an incompatible version. - pub(crate) fn add_version( + /// Add a package version as decision if none of its dependencies conflicts with the partial + /// solution. + /// + /// If the resolution never backtracked before, a fast path adds the package version directly + /// without checking dependencies. + /// + /// Returns the incompatibility that caused the current version to be rejected, if a conflict + /// in the dependencies was found. + pub(crate) fn add_package_version_incompatibilities( &mut self, package: Id, version: DP::V, diff --git a/src/solver.rs b/src/solver.rs index 7c4ed0f8..df667df0 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -228,15 +228,9 @@ pub fn resolve( }; // Add that package and version if the dependencies are not problematic. - let dep_incompats = - state.add_incompatibility_from_dependencies(p, v.clone(), dependencies); - - if let Some(conflict) = state.partial_solution.add_version( - p, - v, - dep_incompats, - &state.incompatibility_store, - ) { + if let Some(conflict) = + state.add_package_version_dependencies(p, v.clone(), dependencies) + { conflict_tracker.entry(p).or_default().dependencies_affected += 1; for (incompat_package, _) in state.incompatibility_store[conflict].iter() { if incompat_package == p {