Skip to content

Commit d070f06

Browse files
committed
add comments and pick better names
1 parent 53b535f commit d070f06

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/cargo/core/resolver/conflict_cache.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ use super::types::ConflictReason;
44
use core::resolver::Context;
55
use core::{Dependency, PackageId};
66

7+
/// This is a data structure for storing a large number of Sets designed to
8+
/// efficiently see if any of the stored Sets are a subset of a search Set.
79
enum ConflictStore {
8-
Con(BTreeMap<PackageId, ConflictReason>),
9-
Map(HashMap<PackageId, ConflictStore>),
10+
/// a Leaf is one of the stored Sets.
11+
Leaf(BTreeMap<PackageId, ConflictReason>),
12+
/// a Node is a map from an element to a subset of the stored data
13+
/// where all the Sets in the subset contains that element.
14+
Node(HashMap<PackageId, ConflictStore>),
1015
}
1116

1217
impl ConflictStore {
1318
/// Finds any known set of conflicts, if any,
1419
/// which are activated in `cx` and pass the `filter` specified?
15-
pub fn find_conflicting<F>(
20+
fn find_conflicting<F>(
1621
&self,
1722
cx: &Context,
1823
filter: &F,
@@ -21,17 +26,23 @@ impl ConflictStore {
2126
for<'r> F: Fn(&'r &BTreeMap<PackageId, ConflictReason>) -> bool,
2227
{
2328
match self {
24-
ConflictStore::Con(c) => {
29+
ConflictStore::Leaf(c) => {
2530
if filter(&c) {
2631
Some(c)
2732
} else {
2833
None
2934
}
3035
}
31-
ConflictStore::Map(m) => {
36+
ConflictStore::Node(m) => {
3237
for (pid, store) in m {
38+
// if the key is active then we need to check all of the corresponding subset,
39+
// but if it is not active then there is no way any of the corresponding subset
40+
// will be conflicting.
3341
if cx.is_active(pid) {
3442
if let Some(o) = store.find_conflicting(cx, filter) {
43+
debug_assert!(cx.is_conflicting(None, o));
44+
// is_conflicting checks that all the elements are active,
45+
// but we have checked each one by the recursion of this function.
3546
return Some(o);
3647
}
3748
}
@@ -41,20 +52,30 @@ impl ConflictStore {
4152
}
4253
}
4354

44-
pub fn insert<'a>(
55+
fn insert<'a>(
4556
&mut self,
4657
mut iter: impl Iterator<Item = &'a PackageId>,
4758
con: BTreeMap<PackageId, ConflictReason>,
4859
) {
4960
if let Some(pid) = iter.next() {
50-
if let ConflictStore::Map(p) = self {
61+
if let ConflictStore::Node(p) = self {
5162
p.entry(pid.clone())
52-
.or_insert_with(|| ConflictStore::Map(HashMap::new()))
63+
.or_insert_with(|| ConflictStore::Node(HashMap::new()))
5364
.insert(iter, con);
54-
}
55-
// else, We already have a subset of this in the ConflictStore
65+
} // else, We already have a subset of this in the ConflictStore
5666
} else {
57-
*self = ConflictStore::Con(con)
67+
// we are at the end of the set we are adding, there are 3 cases for what to do next:
68+
// 1. self is a empty dummy Node inserted by `or_insert_with`
69+
// in witch case we should replace it with `Leaf(con)`.
70+
// 2. self is a Node because we previously inserted a superset of
71+
// the thing we are working on (I don't know if this happens in practice)
72+
// but the subset that we are working on will
73+
// always match any time the larger set would have
74+
// in witch case we can replace it with `Leaf(con)`.
75+
// 3. self is a Leaf that is in the same spot in the structure as
76+
// the thing we are working on. So it is equivalent.
77+
// We can replace it with `Leaf(con)`.
78+
*self = ConflictStore::Leaf(con)
5879
}
5980
}
6081
}
@@ -85,7 +106,7 @@ pub(super) struct ConflictCache {
85106
// This is used to make sure we don't queue work we know will fail. See the
86107
// discussion in https://github.com/rust-lang/cargo/pull/5168 for why this
87108
// is so important. The nested HashMaps act as a kind of btree, that lets us
88-
// look up a witch entry's are still active without
109+
// look up which entries are still active without
89110
// linearly scanning through the full list.
90111
//
91112
// Also, as a final note, this map is *not* ever removed from. This remains
@@ -132,7 +153,7 @@ impl ConflictCache {
132153
pub fn insert(&mut self, dep: &Dependency, con: &BTreeMap<PackageId, ConflictReason>) {
133154
self.con_from_dep
134155
.entry(dep.clone())
135-
.or_insert_with(|| ConflictStore::Map(HashMap::new()))
156+
.or_insert_with(|| ConflictStore::Node(HashMap::new()))
136157
.insert(con.keys(), con.clone());
137158

138159
trace!(

0 commit comments

Comments
 (0)