Skip to content

Commit

Permalink
perf: invalidate less contradicted_incompatibilities (pubgrub-rs#170)
Browse files Browse the repository at this point in the history
* Invalidate less contradicted_incompatibilities

* update comments
  • Loading branch information
Eh2406 authored and konstin committed Dec 26, 2023
1 parent f297ef0 commit 38f515f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/internal/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ pub struct State<P: Package, VS: VersionSet, Priority: Ord + Clone> {

pub incompatibilities: Map<P, Vec<IncompId<P, VS>>>,

/// Store the ids of incompatibilities that are already contradicted
/// and will stay that way until the next conflict and backtrack is operated.
contradicted_incompatibilities: rustc_hash::FxHashSet<IncompId<P, VS>>,
/// Store the ids of incompatibilities that are already contradicted.
/// For each one keep track of the decision level when it was found to be contradicted.
/// These will stay contradicted until we have backtracked beyond its associated decision level.
contradicted_incompatibilities: Map<IncompId<P, VS>, DecisionLevel>,

/// All incompatibilities expressing dependencies,
/// with common dependents merged.
Expand Down Expand Up @@ -62,7 +63,7 @@ impl<P: Package, VS: VersionSet, Priority: Ord + Clone> State<P, VS, Priority> {
root_package,
root_version,
incompatibilities,
contradicted_incompatibilities: rustc_hash::FxHashSet::default(),
contradicted_incompatibilities: Map::default(),
partial_solution: PartialSolution::empty(),
incompatibility_store,
unit_propagation_buffer: SmallVec::Empty,
Expand Down Expand Up @@ -111,7 +112,10 @@ impl<P: Package, VS: VersionSet, Priority: Ord + Clone> State<P, VS, Priority> {
let mut conflict_id = None;
// We only care about incompatibilities if it contains the current package.
for &incompat_id in self.incompatibilities[&current_package].iter().rev() {
if self.contradicted_incompatibilities.contains(&incompat_id) {
if self
.contradicted_incompatibilities
.contains_key(&incompat_id)
{
continue;
}
let current_incompat = &self.incompatibility_store[incompat_id];
Expand All @@ -135,10 +139,12 @@ impl<P: Package, VS: VersionSet, Priority: Ord + Clone> State<P, VS, Priority> {
&self.incompatibility_store,
);
// With the partial solution updated, the incompatibility is now contradicted.
self.contradicted_incompatibilities.insert(incompat_id);
self.contradicted_incompatibilities
.insert(incompat_id, self.partial_solution.current_decision_level());
}
Relation::Contradicted(_) => {
self.contradicted_incompatibilities.insert(incompat_id);
self.contradicted_incompatibilities
.insert(incompat_id, self.partial_solution.current_decision_level());
}
_ => {}
}
Expand All @@ -155,7 +161,8 @@ impl<P: Package, VS: VersionSet, Priority: Ord + Clone> State<P, VS, Priority> {
);
// After conflict resolution and the partial solution update,
// the root cause incompatibility is now contradicted.
self.contradicted_incompatibilities.insert(root_cause);
self.contradicted_incompatibilities
.insert(root_cause, self.partial_solution.current_decision_level());
}
}
// If there are no more changed packages, unit propagation is done.
Expand Down Expand Up @@ -220,7 +227,9 @@ impl<P: Package, VS: VersionSet, Priority: Ord + Clone> State<P, VS, Priority> {
) {
self.partial_solution
.backtrack(decision_level, &self.incompatibility_store);
self.contradicted_incompatibilities.clear();
// Remove contradicted incompatibilities that depend on decisions we just backtracked away.
self.contradicted_incompatibilities
.retain(|_, dl| *dl <= decision_level);
if incompat_changed {
self.merge_incompatibility(incompat);
}
Expand Down
4 changes: 4 additions & 0 deletions src/internal/partial_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ impl<P: Package, VS: VersionSet, Priority: Ord + Clone> PartialSolution<P, VS, P
.unwrap();
decision_level.max(DecisionLevel(1))
}

pub fn current_decision_level(&self) -> DecisionLevel {
self.current_decision_level
}
}

impl<P: Package, VS: VersionSet> PackageAssignments<P, VS> {
Expand Down

0 comments on commit 38f515f

Please sign in to comment.