Skip to content

Commit 49f3f9d

Browse files
committed
use intermediate satisfier causes in priority statistics
1 parent 3bef331 commit 49f3f9d

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

src/internal/core.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<DP: DependencyProvider> State<DP> {
116116
&mut self,
117117
package: Id<DP::P>,
118118
) -> Result<SmallVec<(Id<DP::P>, IncompDpId<DP>)>, NoSolutionError<DP>> {
119-
let mut root_causes = SmallVec::default();
119+
let mut satisfier_causes = SmallVec::default();
120120
self.unit_propagation_buffer.clear();
121121
self.unit_propagation_buffer.push(package);
122122
while let Some(current_package) = self.unit_propagation_buffer.pop() {
@@ -169,12 +169,11 @@ impl<DP: DependencyProvider> State<DP> {
169169
}
170170
}
171171
if let Some(incompat_id) = conflict_id {
172-
let (package_almost, root_cause) =
173-
self.conflict_resolution(incompat_id)
174-
.map_err(|terminal_incompat_id| {
175-
self.build_derivation_tree(terminal_incompat_id)
176-
})?;
177-
root_causes.push((package, root_cause));
172+
let (package_almost, root_cause) = self
173+
.conflict_resolution(incompat_id, &mut satisfier_causes)
174+
.map_err(|terminal_incompat_id| {
175+
self.build_derivation_tree(terminal_incompat_id)
176+
})?;
178177
self.unit_propagation_buffer.clear();
179178
self.unit_propagation_buffer.push(package_almost);
180179
// Add to the partial solution with incompat as cause.
@@ -190,16 +189,46 @@ impl<DP: DependencyProvider> State<DP> {
190189
}
191190
}
192191
// If there are no more changed packages, unit propagation is done.
193-
Ok(root_causes)
192+
Ok(satisfier_causes)
194193
}
195194

196195
/// Return the root cause or the terminal incompatibility.
197196
/// CF <https://github.com/dart-lang/pub/blob/master/doc/solver.md#unit-propagation>
197+
///
198+
/// Usually by the time we have a conflict `unit_propagation` has done a lot of work.
199+
/// So the actual conflict we find is important, but not particularly actionable.
200+
/// It says something like "the dependency on package X and the dependency on package Y are incompatible".
201+
/// To make it actionable we want to track it back to decisions that made the dependency required.
202+
/// "The decision on B is incompatible with the decision on C,
203+
/// because unit propagation from just those decisions will lead to the conflict about X and Y"
204+
/// is much more actionable, backtrack until one of those decisions can be revisited.
205+
/// To make a practical, we really only need one of the terms to be a decision.
206+
/// We may as well leave the other terms general. Something like
207+
/// "the dependency on the package X is incompatible with the decision on C" tends to work out pretty well.
208+
/// Then if A turns out to also have a dependency on X the resulting root cause is still useful.
209+
/// Of course, this is more heuristics than science. If the output is too general, then `unit_propagation` will
210+
/// handle the confusion by calling us again with the next most specific conflict it comes across.
211+
/// If the output is to specific, then the outer `solver` loop will eventually end up calling us again
212+
/// until all possibilities are enumerated.
213+
///
214+
/// This function combines incompatibilities with things that make the problem inevitable to end up with a
215+
/// more useful incompatibility. For the correctness of the PubGrub algorithm only the final output is required.
216+
/// By banning the final output, unit propagation will prevent the intermediate steps from occurring again,
217+
/// at least prevent the exact same way. However, the statistics collected for `prioritize`may want
218+
/// to analyze those intermediate steps. For example we might start with "there is no version 1 of Z",
219+
/// and `conflict_resolution` may be able to determine that "that was inevitable when we picked version 1 of X"
220+
/// which was inevitable when picked W and ... and version 1 of B, which was depended on by version 1 of A.
221+
/// Therefore the root cause may simplify all the way down to "we cannot pick version 1 of A".
222+
/// This will prevent us going down this path again. However when we start looking at version 2 of A,
223+
/// and discover that it depends on version 2 of B, we will want to prioritize the chain of intermediate steps
224+
/// to confirm if it has a problem with the same shape.
225+
/// The `satisfier_causes` argument keeps track of these intermediate steps so that the caller can use.
198226
#[allow(clippy::type_complexity)]
199227
#[cold]
200228
fn conflict_resolution(
201229
&mut self,
202230
incompatibility: IncompDpId<DP>,
231+
satisfier_causes: &mut SmallVec<(Id<DP::P>, IncompDpId<DP>)>,
203232
) -> Result<(Id<DP::P>, IncompDpId<DP>), IncompDpId<DP>> {
204233
let mut current_incompat_id = incompatibility;
205234
let mut current_incompat_changed = false;
@@ -223,6 +252,7 @@ impl<DP: DependencyProvider> State<DP> {
223252
previous_satisfier_level,
224253
);
225254
log::info!("backtrack to {:?}", previous_satisfier_level);
255+
satisfier_causes.push((package, current_incompat_id));
226256
return Ok((package, current_incompat_id));
227257
}
228258
SatisfierSearch::SameDecisionLevels { satisfier_cause } => {
@@ -234,6 +264,7 @@ impl<DP: DependencyProvider> State<DP> {
234264
);
235265
log::info!("prior cause: {}", prior_cause.display(&self.package_store));
236266
current_incompat_id = self.incompatibility_store.alloc(prior_cause);
267+
satisfier_causes.push((package, current_incompat_id));
237268
current_incompat_changed = true;
238269
}
239270
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
//! and [SemanticVersion] for versions.
7272
//! This may be done quite easily by implementing the three following functions.
7373
//! ```
74-
//! # use pubgrub::{DependencyProvider, Dependencies, SemanticVersion, Ranges, DependencyConstraints, Map, PackageResolutionStatistics};
74+
//! # use pubgrub::{DependencyProvider, Dependencies, SemanticVersion, Ranges,
75+
//! # DependencyConstraints, Map, PackageResolutionStatistics};
7576
//! # use std::error::Error;
7677
//! # use std::borrow::Borrow;
7778
//! # use std::convert::Infallible;

src/provider.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use std::cmp::Reverse;
22
use std::collections::BTreeMap;
33
use std::convert::Infallible;
44

5-
use crate::solver::PackageResolutionStatistics;
6-
use crate::{Dependencies, DependencyConstraints, DependencyProvider, Map, Package, VersionSet};
5+
use crate::{
6+
Dependencies, DependencyConstraints, DependencyProvider, Map, Package,
7+
PackageResolutionStatistics, VersionSet,
8+
};
79

810
/// A basic implementation of [DependencyProvider].
911
#[derive(Debug, Clone, Default)]
@@ -102,15 +104,15 @@ impl<P: Package, VS: VersionSet> DependencyProvider for OfflineDependencyProvide
102104
range: &Self::VS,
103105
package_statistics: &PackageResolutionStatistics,
104106
) -> Self::Priority {
105-
(
106-
package_statistics.conflict_count(),
107-
Reverse(
108-
self.dependencies
109-
.get(package)
110-
.map(|versions| versions.keys().filter(|v| range.contains(v)).count())
111-
.unwrap_or(0),
112-
),
113-
)
107+
let version_count = self
108+
.dependencies
109+
.get(package)
110+
.map(|versions| versions.keys().filter(|v| range.contains(v)).count())
111+
.unwrap_or(0);
112+
if version_count == 0 {
113+
return (u32::MAX, Reverse(0));
114+
}
115+
(package_statistics.conflict_count(), Reverse(version_count))
114116
}
115117

116118
#[inline]

src/solver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ pub fn resolve<DP: DependencyProvider>(
126126
"unit_propagation: {:?} = '{}'",
127127
&next, state.package_store[next]
128128
);
129-
let root_causes = state.unit_propagation(next)?;
130-
for (affected, incompat) in root_causes {
129+
let satisfier_causes = state.unit_propagation(next)?;
130+
for (affected, incompat) in satisfier_causes {
131131
conflict_tracker
132132
.entry(affected)
133133
.or_default()

0 commit comments

Comments
 (0)