Skip to content

Commit 178ee0b

Browse files
committed
address suggestions
1 parent d070f06 commit 178ee0b

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/cargo/core/resolver/conflict_cache.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ 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
7+
/// This is a Trie for storing a large number of Sets designed to
88
/// efficiently see if any of the stored Sets are a subset of a search Set.
9-
enum ConflictStore {
9+
enum ConflictStoreTrie {
1010
/// a Leaf is one of the stored Sets.
1111
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>),
12+
/// a Node is a map from an element to a subTrie where
13+
/// all the Sets in the subTrie contains that element.
14+
Node(HashMap<PackageId, ConflictStoreTrie>),
1515
}
1616

17-
impl ConflictStore {
17+
impl ConflictStoreTrie {
1818
/// Finds any known set of conflicts, if any,
1919
/// which are activated in `cx` and pass the `filter` specified?
2020
fn find_conflicting<F>(
@@ -26,26 +26,25 @@ impl ConflictStore {
2626
for<'r> F: Fn(&'r &BTreeMap<PackageId, ConflictReason>) -> bool,
2727
{
2828
match self {
29-
ConflictStore::Leaf(c) => {
29+
ConflictStoreTrie::Leaf(c) => {
3030
if filter(&c) {
31+
// is_conflicting checks that all the elements are active,
32+
// but we have checked each one by the recursion of this function.
33+
debug_assert!(cx.is_conflicting(None, c));
3134
Some(c)
3235
} else {
3336
None
3437
}
3538
}
36-
ConflictStore::Node(m) => {
39+
ConflictStoreTrie::Node(m) => {
3740
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.
41+
// if the key is active then we need to check all of the corresponding subTrie.
4142
if cx.is_active(pid) {
4243
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.
4644
return Some(o);
4745
}
48-
}
46+
} // else, if it is not active then there is no way any of the corresponding
47+
// subTrie will be conflicting.
4948
}
5049
None
5150
}
@@ -58,9 +57,9 @@ impl ConflictStore {
5857
con: BTreeMap<PackageId, ConflictReason>,
5958
) {
6059
if let Some(pid) = iter.next() {
61-
if let ConflictStore::Node(p) = self {
60+
if let ConflictStoreTrie::Node(p) = self {
6261
p.entry(pid.clone())
63-
.or_insert_with(|| ConflictStore::Node(HashMap::new()))
62+
.or_insert_with(|| ConflictStoreTrie::Node(HashMap::new()))
6463
.insert(iter, con);
6564
} // else, We already have a subset of this in the ConflictStore
6665
} else {
@@ -75,7 +74,14 @@ impl ConflictStore {
7574
// 3. self is a Leaf that is in the same spot in the structure as
7675
// the thing we are working on. So it is equivalent.
7776
// We can replace it with `Leaf(con)`.
78-
*self = ConflictStore::Leaf(con)
77+
if cfg!(debug_assertions) {
78+
if let ConflictStoreTrie::Leaf(c) = self {
79+
let a: Vec<_> = con.keys().collect();
80+
let b: Vec<_> = c.keys().collect();
81+
assert_eq!(a, b);
82+
}
83+
}
84+
*self = ConflictStoreTrie::Leaf(con)
7985
}
8086
}
8187
}
@@ -113,7 +119,7 @@ pub(super) struct ConflictCache {
113119
// as a global cache which we never delete from. Any entry in this map is
114120
// unconditionally true regardless of our resolution history of how we got
115121
// here.
116-
con_from_dep: HashMap<Dependency, ConflictStore>,
122+
con_from_dep: HashMap<Dependency, ConflictStoreTrie>,
117123
// `dep_from_pid` is an inverse-index of `con_from_dep`.
118124
// For every `PackageId` this lists the `Dependency`s that mention it in `dep_from_pid`.
119125
dep_from_pid: HashMap<PackageId, HashSet<Dependency>>,
@@ -153,7 +159,7 @@ impl ConflictCache {
153159
pub fn insert(&mut self, dep: &Dependency, con: &BTreeMap<PackageId, ConflictReason>) {
154160
self.con_from_dep
155161
.entry(dep.clone())
156-
.or_insert_with(|| ConflictStore::Node(HashMap::new()))
162+
.or_insert_with(|| ConflictStoreTrie::Node(HashMap::new()))
157163
.insert(con.keys(), con.clone());
158164

159165
trace!(

0 commit comments

Comments
 (0)