Skip to content

Commit e1ff48b

Browse files
committed
remove the O(n) insertion, but loos the hack for lookup
1 parent 1e90c0a commit e1ff48b

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

src/cargo/core/resolver/conflict_cache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
22

33
use super::types::ConflictReason;
44
use core::resolver::Context;
@@ -34,7 +34,7 @@ pub(super) struct ConflictCache {
3434
// as a global cache which we never delete from. Any entry in this map is
3535
// unconditionally true regardless of our resolution history of how we got
3636
// here.
37-
con_from_dep: HashMap<Dependency, Vec<HashMap<PackageId, ConflictReason>>>,
37+
con_from_dep: HashMap<Dependency, BTreeSet<BTreeMap<PackageId, ConflictReason>>>,
3838
// `dep_from_pid` is an inverse-index of `con_from_dep`.
3939
// For every `PackageId` this lists the `Dependency`s that mention it in `dep_from_pid`.
4040
dep_from_pid: HashMap<PackageId, HashSet<Dependency>>,
@@ -54,9 +54,9 @@ impl ConflictCache {
5454
cx: &Context,
5555
dep: &Dependency,
5656
filter: F,
57-
) -> Option<&HashMap<PackageId, ConflictReason>>
57+
) -> Option<&BTreeMap<PackageId, ConflictReason>>
5858
where
59-
for<'r> F: FnMut(&'r &HashMap<PackageId, ConflictReason>) -> bool,
59+
for<'r> F: FnMut(&'r &BTreeMap<PackageId, ConflictReason>) -> bool,
6060
{
6161
self.con_from_dep
6262
.get(dep)?
@@ -69,26 +69,26 @@ impl ConflictCache {
6969
&self,
7070
cx: &Context,
7171
dep: &Dependency,
72-
) -> Option<&HashMap<PackageId, ConflictReason>> {
72+
) -> Option<&BTreeMap<PackageId, ConflictReason>> {
7373
self.find_conflicting(cx, dep, |_| true)
7474
}
7575

7676
/// Add to the cache a conflict of the form:
7777
/// `dep` is known to be unresolvable if
7878
/// all the `PackageId` entries are activated
79-
pub fn insert(&mut self, dep: &Dependency, con: &HashMap<PackageId, ConflictReason>) {
79+
pub fn insert(&mut self, dep: &Dependency, con: &BTreeMap<PackageId, ConflictReason>) {
8080
let past = self
8181
.con_from_dep
8282
.entry(dep.clone())
83-
.or_insert_with(Vec::new);
83+
.or_insert_with(BTreeSet::new);
8484
if !past.contains(con) {
8585
trace!(
8686
"{} = \"{}\" adding a skip {:?}",
8787
dep.package_name(),
8888
dep.version_req(),
8989
con
9090
);
91-
past.push(con.clone());
91+
past.insert(con.clone());
9292
for c in con.keys() {
9393
self.dep_from_pid
9494
.entry(c.clone())

src/cargo/core/resolver/context.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::collections::{HashMap, HashSet};
1+
use std::collections::{BTreeMap, HashMap, HashSet};
22
use std::rc::Rc;
33

44
use core::interning::InternedString;
55
use core::{Dependency, FeatureValue, PackageId, SourceId, Summary};
66
use util::CargoResult;
77
use util::Graph;
88

9-
use super::types::{ConflictReason, DepInfo, GraphNode, Method, RcList, RegistryQueryer};
109
use super::errors::ActivateResult;
10+
use super::types::{ConflictReason, DepInfo, GraphNode, Method, RcList, RegistryQueryer};
1111

1212
pub use super::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
1313
pub use super::encode::{Metadata, WorkspaceResolve};
@@ -145,7 +145,7 @@ impl Context {
145145
pub fn is_conflicting(
146146
&self,
147147
parent: Option<&PackageId>,
148-
conflicting_activations: &HashMap<PackageId, ConflictReason>,
148+
conflicting_activations: &BTreeMap<PackageId, ConflictReason>,
149149
) -> bool {
150150
conflicting_activations
151151
.keys()
@@ -186,10 +186,11 @@ impl Context {
186186
// name.
187187
let base = reqs.deps.get(&dep.name_in_toml()).unwrap_or(&default_dep);
188188
used_features.insert(dep.name_in_toml());
189-
let always_required = !dep.is_optional() && !s
190-
.dependencies()
191-
.iter()
192-
.any(|d| d.is_optional() && d.name_in_toml() == dep.name_in_toml());
189+
let always_required = !dep.is_optional()
190+
&& !s
191+
.dependencies()
192+
.iter()
193+
.any(|d| d.is_optional() && d.name_in_toml() == dep.name_in_toml());
193194
if always_required && base.0 {
194195
self.warnings.push(format!(
195196
"Package `{}` does not have feature `{}`. It has a required dependency \

src/cargo/core/resolver/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::BTreeMap;
22
use std::fmt;
33

44
use core::{Dependency, PackageId, Registry, Summary};
@@ -73,7 +73,7 @@ pub(super) fn activation_error(
7373
registry: &mut Registry,
7474
parent: &Summary,
7575
dep: &Dependency,
76-
conflicting_activations: &HashMap<PackageId, ConflictReason>,
76+
conflicting_activations: &BTreeMap<PackageId, ConflictReason>,
7777
candidates: &[Candidate],
7878
config: Option<&Config>,
7979
) -> ResolveError {

src/cargo/core/resolver/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn activate_deps_loop(
247247
//
248248
// This is a map of package id to a reason why that packaged caused a
249249
// conflict for us.
250-
let mut conflicting_activations = HashMap::new();
250+
let mut conflicting_activations = BTreeMap::new();
251251

252252
// When backtracking we don't fully update `conflicting_activations`
253253
// especially for the cases that we didn't make a backtrack frame in the
@@ -641,7 +641,7 @@ struct BacktrackFrame {
641641
parent: Summary,
642642
dep: Dependency,
643643
features: Rc<Vec<InternedString>>,
644-
conflicting_activations: HashMap<PackageId, ConflictReason>,
644+
conflicting_activations: BTreeMap<PackageId, ConflictReason>,
645645
}
646646

647647
/// A helper "iterator" used to extract candidates within a current `Context` of
@@ -688,7 +688,7 @@ impl RemainingCandidates {
688688
/// original list for the reason listed.
689689
fn next(
690690
&mut self,
691-
conflicting_prev_active: &mut HashMap<PackageId, ConflictReason>,
691+
conflicting_prev_active: &mut BTreeMap<PackageId, ConflictReason>,
692692
cx: &Context,
693693
dep: &Dependency,
694694
) -> Option<(Candidate, bool)> {
@@ -781,7 +781,7 @@ fn find_candidate(
781781
backtrack_stack: &mut Vec<BacktrackFrame>,
782782
parent: &Summary,
783783
backtracked: bool,
784-
conflicting_activations: &HashMap<PackageId, ConflictReason>,
784+
conflicting_activations: &BTreeMap<PackageId, ConflictReason>,
785785
) -> Option<(Candidate, bool, BacktrackFrame)> {
786786
while let Some(mut frame) = backtrack_stack.pop() {
787787
let next = frame.remaining_candidates.next(

0 commit comments

Comments
 (0)