Skip to content

Commit

Permalink
conflict_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Dec 5, 2024
1 parent 3741e3b commit 446fadf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/caching_dependency_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl<DP: DependencyProvider<M = String>> DependencyProvider for CachingDependenc

type Priority = DP::Priority;

fn prioritize(&self, package: &DP::P, ranges: &DP::VS) -> Self::Priority {
self.remote_dependencies.prioritize(package, ranges)
fn prioritize(&self, package: &DP::P, ranges: &DP::VS, conflict_count: u32) -> Self::Priority {
self.remote_dependencies.prioritize(package, ranges, conflict_count)
}

type Err = DP::Err;
Expand Down
6 changes: 6 additions & 0 deletions src/internal/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub(crate) struct State<DP: DependencyProvider> {
#[allow(clippy::type_complexity)]
merged_dependencies: Map<(Id<DP::P>, Id<DP::P>), SmallVec<IncompDpId<DP>>>,

pub(crate) conflict_count: Map<Id<DP::P>, u32>,

/// Partial solution.
/// TODO: remove pub.
pub(crate) partial_solution: PartialSolution<DP>,
Expand Down Expand Up @@ -69,6 +71,7 @@ impl<DP: DependencyProvider> State<DP> {
package_store,
unit_propagation_buffer: SmallVec::Empty,
merged_dependencies: Map::default(),
conflict_count: Map::default(),
}
}

Expand Down Expand Up @@ -163,6 +166,9 @@ impl<DP: DependencyProvider> State<DP> {
}
}
if let Some(incompat_id) = conflict_id {
for (p, _) in self.incompatibility_store[incompat_id].iter() {
*self.conflict_count.entry(*p).or_default() += 1;
}
let (package_almost, root_cause) =
self.conflict_resolution(incompat_id)
.map_err(|terminal_incompat_id| {
Expand Down
19 changes: 11 additions & 8 deletions src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ impl<P: Package, VS: VersionSet> DependencyProvider for OfflineDependencyProvide
.and_then(|versions| versions.keys().rev().find(|v| range.contains(v)).cloned()))
}

type Priority = Reverse<usize>;
type Priority = (u32, Reverse<u32>);

#[inline]
fn prioritize(&self, package: &P, range: &VS) -> Self::Priority {
Reverse(
self.dependencies
.get(package)
.map(|versions| versions.keys().filter(|v| range.contains(v)).count())
.unwrap_or(0),
)
fn prioritize(&self, package: &P, range: &VS, conflict_count: u32) -> Self::Priority {
let version_count = self
.dependencies
.get(package)
.map(|versions| versions.keys().filter(|v| range.contains(v)).count())
.unwrap_or(0);
if version_count == 0 {
return (u32::MAX, Reverse(0));
}
(conflict_count, Reverse(version_count as u32))
}

#[inline]
Expand Down
13 changes: 11 additions & 2 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ pub fn resolve<DP: DependencyProvider>(

let Some(highest_priority_pkg) =
state.partial_solution.pick_highest_priority_pkg(|p, r| {
dependency_provider.prioritize(&state.package_store[p], r)
dependency_provider.prioritize(
&state.package_store[p],
r,
state.conflict_count.get(&p).cloned().unwrap_or_default(),
)
})
else {
return Ok(state
Expand Down Expand Up @@ -254,7 +258,12 @@ pub trait DependencyProvider {
///
/// Note: the resolver may call this even when the range has not changed,
/// if it is more efficient for the resolvers internal data structures.
fn prioritize(&self, package: &Self::P, range: &Self::VS) -> Self::Priority;
fn prioritize(
&self,
package: &Self::P,
range: &Self::VS,
conflict_count: u32,
) -> Self::Priority;
/// The type returned from `prioritize`. The resolver does not care what type this is
/// as long as it can pick a largest one and clone it.
///
Expand Down
8 changes: 4 additions & 4 deletions tests/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl<P: Package, VS: VersionSet> DependencyProvider for OldestVersionsDependency

type Priority = <OfflineDependencyProvider<P, VS> as DependencyProvider>::Priority;

fn prioritize(&self, package: &P, range: &VS) -> Self::Priority {
self.0.prioritize(package, range)
fn prioritize(&self, package: &P, range: &VS, conflict_count: u32) -> Self::Priority {
self.0.prioritize(package, range, conflict_count)
}

type Err = Infallible;
Expand Down Expand Up @@ -104,8 +104,8 @@ impl<DP: DependencyProvider> DependencyProvider for TimeoutDependencyProvider<DP

type Priority = DP::Priority;

fn prioritize(&self, package: &DP::P, range: &DP::VS) -> Self::Priority {
self.dp.prioritize(package, range)
fn prioritize(&self, package: &DP::P, range: &DP::VS, conflict_count: u32) -> Self::Priority {
self.dp.prioritize(package, range, conflict_count)
}

type Err = DP::Err;
Expand Down

0 comments on commit 446fadf

Please sign in to comment.