-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Faster resolver: use a inverse-index to not activate the causes of conflict #5213
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
66e3d1c
Add a test for https://github.com/rust-lang/cargo/issues/4810#issueco…
Eh2406 816203e
A slow and ugly solution, but it works.
Eh2406 efefe00
problematic extension and solution
Eh2406 0f42afa
maintain an inverse-index
Eh2406 91c062e
test the new backtracking does all of its bookkeeping
Eh2406 f86346c
remove some loops
Eh2406 aac04b6
add comments
Eh2406 997b3dc
Move caching to a support struct and file
Eh2406 a7a1341
address suggestions
Eh2406 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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use core::{Dependency, PackageId}; | ||
use core::resolver::{ConflictReason, Context}; | ||
|
||
pub(super) struct ConflictCache { | ||
// `con_from_dep` is a cache of the reasons for each time we | ||
// backtrack. For example after several backtracks we may have: | ||
// | ||
// con_from_dep[`foo = "^1.0.2"`] = vec![ | ||
// map!{`foo=1.0.1`: Semver}, | ||
// map!{`foo=1.0.0`: Semver}, | ||
// ]; | ||
// | ||
// This can be read as "we cannot find a candidate for dep `foo = "^1.0.2"` | ||
// if either `foo=1.0.1` OR `foo=1.0.0` are activated". | ||
// | ||
// Another example after several backtracks we may have: | ||
// | ||
// con_from_dep[`foo = ">=0.8.2, <=0.9.3"`] = vec![ | ||
// map!{`foo=0.8.1`: Semver, `foo=0.9.4`: Semver}, | ||
// ]; | ||
// | ||
// This can be read as "we cannot find a candidate for dep `foo = ">=0.8.2, | ||
// <=0.9.3"` if both `foo=0.8.1` AND `foo=0.9.4` are activated". | ||
// | ||
// This is used to make sure we don't queue work we know will fail. See the | ||
// discussion in https://github.com/rust-lang/cargo/pull/5168 for why this | ||
// is so important, and there can probably be a better data structure here | ||
// but for now this works well enough! | ||
// | ||
// Also, as a final note, this map is *not* ever removed from. This remains | ||
// as a global cache which we never delete from. Any entry in this map is | ||
// unconditionally true regardless of our resolution history of how we got | ||
// here. | ||
con_from_dep: HashMap<Dependency, Vec<HashMap<PackageId, ConflictReason>>>, | ||
// `past_conflict_triggers` is an | ||
// of `past_conflicting_activations`. | ||
// For every `PackageId` this lists the `Dependency`s that mention it in `past_conflicting_activations`. | ||
dep_from_pid: HashMap<PackageId, HashSet<Dependency>>, | ||
} | ||
|
||
impl ConflictCache { | ||
pub fn new() -> ConflictCache { | ||
ConflictCache { | ||
con_from_dep: HashMap::new(), | ||
dep_from_pid: HashMap::new(), | ||
} | ||
} | ||
/// Finds any known set of conflicts, if any, | ||
/// which are activated in `cx` and pass the `filter` specified? | ||
pub fn find_conflicting<F>( | ||
&self, | ||
cx: &Context, | ||
dep: &Dependency, | ||
filter: F, | ||
) -> Option<&HashMap<PackageId, ConflictReason>> | ||
where | ||
for<'r> F: FnMut(&'r &HashMap<PackageId, ConflictReason>) -> bool, | ||
{ | ||
self.con_from_dep | ||
.get(dep)? | ||
.iter() | ||
.filter(filter) | ||
.find(|conflicting| cx.is_conflicting(None, conflicting)) | ||
} | ||
pub fn conflicting( | ||
&self, | ||
cx: &Context, | ||
dep: &Dependency, | ||
) -> Option<&HashMap<PackageId, ConflictReason>> { | ||
self.find_conflicting(cx, dep, |_| true) | ||
} | ||
|
||
/// Add to the cache a conflict of the form: | ||
/// `dep` is known to be unresolvable if | ||
/// all the `PackageId` entries are activated | ||
pub fn insert(&mut self, dep: &Dependency, con: &HashMap<PackageId, ConflictReason>) { | ||
let past = self.con_from_dep | ||
.entry(dep.clone()) | ||
.or_insert_with(Vec::new); | ||
if !past.contains(con) { | ||
trace!("{} adding a skip {:?}", dep.name(), con); | ||
past.push(con.clone()); | ||
for c in con.keys() { | ||
self.dep_from_pid | ||
.entry(c.clone()) | ||
.or_insert_with(HashSet::new) | ||
.insert(dep.clone()); | ||
} | ||
} | ||
} | ||
pub fn dependencies_conflicting_with(&self, pid: &PackageId) -> Option<&HashSet<Dependency>> { | ||
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. As a random drive-by-comment, it might be neat to store an empty self.dep_from_pid.get(pid).unwrap_or(&self.empty) |
||
self.dep_from_pid.get(pid) | ||
} | ||
} |
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
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
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 think this
pub(super)
can change topub
like the ones below, right?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.
If I remove it from the type, I get:
private type `core::resolver::ConflictReason` in public interface
on the methods.I could just make everything
pub
, but these are all just implementation details ofcore::resolver
so I thinkpub(super)
is appropriate.