-
Notifications
You must be signed in to change notification settings - Fork 739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge identical forks #5405
Merged
Merged
Merge identical forks #5405
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; | |
use tokio::sync::mpsc::{self, Receiver, Sender}; | ||
use tokio::sync::oneshot; | ||
use tokio_stream::wrappers::ReceiverStream; | ||
use tracing::{debug, instrument, trace, warn, Level}; | ||
use tracing::{debug, info, instrument, trace, warn, Level}; | ||
|
||
use distribution_types::{ | ||
BuiltDist, CompatibleDist, Dist, DistributionMetadata, IncompatibleDist, IncompatibleSource, | ||
|
@@ -392,6 +392,27 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag | |
} | ||
} | ||
|
||
// If another fork had the same resolution, merge into that fork instead. | ||
if let Some(existing_resolution) = resolutions | ||
.iter_mut() | ||
.find(|existing_resolution| resolution.same_graph(existing_resolution)) | ||
{ | ||
let ResolverMarkers::Fork(existing_markers) = &existing_resolution.markers | ||
else { | ||
panic!("A non-forking resolution exists in forking mode") | ||
}; | ||
let mut new_markers = existing_markers.clone(); | ||
new_markers.or(resolution | ||
.markers | ||
.fork_markers() | ||
.expect("A non-forking resolution exists in forking mode") | ||
.clone()); | ||
existing_resolution.markers = normalize(new_markers, None) | ||
.map(ResolverMarkers::Fork) | ||
.unwrap_or(ResolverMarkers::Universal); | ||
continue 'FORK; | ||
} | ||
|
||
resolutions.push(resolution); | ||
continue 'FORK; | ||
}; | ||
|
@@ -574,8 +595,20 @@ impl<InstalledPackages: InstalledPackagesProvider> ResolverState<InstalledPackag | |
} | ||
} | ||
} | ||
let mut combined = Resolution::default(); | ||
let mut combined = Resolution::universal(); | ||
if resolutions.len() > 1 { | ||
info!( | ||
"Solved your requirements for {} environments", | ||
resolutions.len() | ||
); | ||
} | ||
for resolution in resolutions { | ||
if let Some(markers) = resolution.markers.fork_markers() { | ||
debug!( | ||
"Distinct solution for ({markers}) with {} packages", | ||
resolution.nodes.len() | ||
); | ||
} | ||
combined.union(resolution); | ||
} | ||
Self::trace_resolution(&combined); | ||
|
@@ -2378,6 +2411,7 @@ impl ForkState { | |
nodes: packages, | ||
edges: dependencies, | ||
pins: self.pins, | ||
markers: self.markers, | ||
} | ||
} | ||
} | ||
|
@@ -2386,14 +2420,16 @@ impl ForkState { | |
/// | ||
/// Each package can have multiple versions and each edge between two packages can have multiple | ||
/// version specifiers to support diverging versions and requirements in different forks. | ||
#[derive(Debug, Default)] | ||
#[derive(Debug)] | ||
pub(crate) struct Resolution { | ||
pub(crate) nodes: FxHashMap<ResolutionPackage, FxHashSet<Version>>, | ||
/// The directed connections between the nodes, where the marker is the node weight. We don't | ||
/// store the requirement itself, but it can be retrieved from the package metadata. | ||
pub(crate) edges: FxHashSet<ResolutionDependencyEdge>, | ||
/// Map each package name, version tuple from `packages` to a distribution. | ||
pub(crate) pins: FilePins, | ||
/// The marker setting this resolution was found under. | ||
pub(crate) markers: ResolverMarkers, | ||
} | ||
|
||
/// Package representation we used during resolution where each extra and also the dev-dependencies | ||
|
@@ -2426,6 +2462,30 @@ pub(crate) struct ResolutionDependencyEdge { | |
} | ||
|
||
impl Resolution { | ||
fn universal() -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That method gets removed upstack |
||
Self { | ||
nodes: FxHashMap::default(), | ||
edges: FxHashSet::default(), | ||
pins: FilePins::default(), | ||
markers: ResolverMarkers::Universal, | ||
} | ||
} | ||
} | ||
|
||
impl Resolution { | ||
/// Whether we got two identical resolutions in two separate forks. | ||
/// | ||
/// Ignores pins since the which distribution we prioritized for each version doesn't matter. | ||
fn same_graph(&self, other: &Self) -> bool { | ||
// TODO(konsti): The edges being equal is not a requirement for the graph being equal. While | ||
// an exact solution is too much here, we should ignore different in edges that point to | ||
// nodes that are always installed. Example: root requires foo, root requires bar. bar | ||
// forks, and one for the branches has bar -> foo while the other doesn't. The resolution | ||
// is still the same graph since the presence or absence of the bar -> foo edge cannot | ||
// change which packages and versions are installed. | ||
self.nodes == other.nodes && self.edges == other.edges | ||
} | ||
|
||
fn union(&mut self, other: Resolution) { | ||
for (other_package, other_versions) in other.nodes { | ||
self.nodes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I buy this, but two thoughts come to mind.
Firstly, is it possible for more than one duplicate resolution to exist at any given point in time? If so, this would I believe only find one of them. But, I do not think this is case, since this is run for every resolution before it is "saved." So it should never be the case that more than one duplicate resolution appears.
Secondly, this is doing an exhaustive search over all existing resolutions to find a possible duplicate. And I suspect that the
Resolution::same_graph
routine is itself not especially cheap. I think this ends up being quadratic in the number of forks (which are themselves exponential in the number of dependencies I think? or possibly in the depth in the dependency tree). I don't have a good feel for how big of an issue that is in practice. Do we have a sense of what the common case is? I would guess the common case is that there aren't any duplicates. So perhaps we can optimize for that path. (To be clear, I don't mean to suggest that be done in this PR.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We fork every time we see conflicting markers, but in many of those cases the requirements themselves are not conflicting (say
numpy >= 1.16
for one andnumpy >= 1.19
for the other). When forking, we can't yet know whether we'll find a compatiblenumpy
for both of. I've also seen cases where we end up rejecting the package version we forked on in both branches, removing the conflicting requirements. By copying over preferences from previous forks, we try to coerce two forks to resolving the same package version. Basically, our strategy is to fork often to avoid failing on avoidable conflicts, but still having a solution with as few divergences as possibles.Re perf: I agree that this is potentially costly, but i think we have to do this to get desirable resolution. We have some short-cuts that we get from std that makes this cheaper: When two forks have a different number of packages, the check is a single
usize
comparison. We also usually have a small number of forks (and the more specific a fork is, the more likely it is we skip future fork points because we're already more specific), so i see this more as a fixed cost of maybe 10^2/2=50 checks. There are of course pathological cases, for those i think we just have to be a bit slow here avoid redundant forks in the lockfile.