Skip to content

Commit 0a9ace7

Browse files
authored
Allow backtracking to before a specific package (#38)
This allows discarding a previously made decision if it turned out to be a bad decision, even if all options with this decision have not yet been rejected. We allow attempting to backtrack on packages that were not decided yet to avoid the caller from making the duplicative check. astral-sh/uv#8157
1 parent e280683 commit 0a9ace7

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/internal/core.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<DP: DependencyProvider> State<DP> {
8282
) {
8383
let dep_incompats =
8484
self.add_incompatibility_from_dependencies(package, version.clone(), dependencies);
85-
self.partial_solution.add_package_version_incompatibilities(
85+
self.partial_solution.add_package_version_dependencies(
8686
package,
8787
version.clone(),
8888
dep_incompats,
@@ -249,7 +249,8 @@ impl<DP: DependencyProvider> State<DP> {
249249
}
250250
}
251251

252-
/// Backtracking.
252+
/// After a conflict occurred, backtrack the partial solution to a given decision level, and add
253+
/// the incompatibility if it was new.
253254
fn backtrack(
254255
&mut self,
255256
incompat: IncompDpId<DP>,
@@ -265,6 +266,21 @@ impl<DP: DependencyProvider> State<DP> {
265266
}
266267
}
267268

269+
/// Manually backtrack before the given package was selected.
270+
///
271+
/// This can be used to switch the order of packages if the previous prioritization was bad.
272+
///
273+
/// Returns the number of the decisions that were backtracked, or `None` if the package was not
274+
/// decided on yet.
275+
pub fn backtrack_package(&mut self, package: Id<DP::P>) -> Option<u32> {
276+
let base_decision_level = self.partial_solution.current_decision_level();
277+
let new_decision_level = self.partial_solution.backtrack_package(package).ok()?;
278+
// Remove contradicted incompatibilities that depend on decisions we just backtracked away.
279+
self.contradicted_incompatibilities
280+
.retain(|_, dl| *dl <= new_decision_level);
281+
Some(base_decision_level.0 - new_decision_level.0)
282+
}
283+
268284
/// Add this incompatibility into the set of all incompatibilities.
269285
///
270286
/// PubGrub collapses identical dependencies from adjacent package versions

src/internal/partial_solution.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use std::fmt::{Debug, Display};
77
use std::hash::BuildHasherDefault;
88

9+
use log::debug;
910
use priority_queue::PriorityQueue;
1011
use rustc_hash::FxHasher;
1112

@@ -377,12 +378,37 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
377378
self.has_ever_backtracked = true;
378379
}
379380

380-
/// We can add the version to the partial solution as a decision
381-
/// if it doesn't produce any conflict with the new incompatibilities.
382-
/// In practice I think it can only produce a conflict if one of the dependencies
383-
/// (which are used to make the new incompatibilities)
384-
/// is already in the partial solution with an incompatible version.
385-
pub(crate) fn add_package_version_incompatibilities(
381+
/// Backtrack the partial solution before a particular package was selected.
382+
///
383+
/// This can be used to switch the order of packages if the previous prioritization was bad.
384+
///
385+
/// Returns the new decision level on success and an error if the package was not decided on
386+
/// yet.
387+
pub(crate) fn backtrack_package(&mut self, package: Id<DP::P>) -> Result<DecisionLevel, ()> {
388+
let Some(decision_level) = self.package_assignments.get_index_of(&package) else {
389+
return Err(());
390+
};
391+
let decision_level = DecisionLevel(decision_level as u32);
392+
if decision_level > self.current_decision_level {
393+
return Err(());
394+
}
395+
debug!(
396+
"Package backtracking ot decision level {}",
397+
decision_level.0
398+
);
399+
self.backtrack(decision_level);
400+
Ok(decision_level)
401+
}
402+
403+
/// Add a package version as decision if none of its dependencies conflicts with the partial
404+
/// solution.
405+
///
406+
/// If the resolution never backtracked before, a fast path adds the package version directly
407+
/// without checking dependencies.
408+
///
409+
/// Returns the incompatibility that caused the current version to be rejected, if a conflict
410+
/// in the dependencies was found.
411+
pub(crate) fn add_package_version_dependencies(
386412
&mut self,
387413
package: Id<DP::P>,
388414
version: DP::V,

0 commit comments

Comments
 (0)