Skip to content

Commit 7aa7fb1

Browse files
committed
Auto merge of #14663 - x-hgg-x:resolver-perf, r=Eh2406
Improve resolver speed ### What does this PR try to resolve? This PR improves the resolver speed after investigations from Eh2406/pubgrub-crates-benchmark#6. ### How should we test and review this PR? Commit 1 adds a test showing a slow case in the resolver, where resolving time is cubic over the number of versions of a crate. It can be used for benchmarking the resolver. Comparison of the resolving time with various values of `N=LAST_CRATE_VERSION_COUNT` and `C=TRANSITIVE_CRATES_COUNT`: | | N=100 | N=200 | N=400 | |------|-------|-------|-------| | C=1 | 0.25s | 0.5s | 1.4s | | C=2 | 7s | 44s | 314s | | C=3 | 12s | 77s | 537s | | C=6 | 30s | 149s | 1107s | | C=12 | 99s | 447s | 2393s | Commit 2 replaces the default hasher with the hasher from `rustc-hash`, decreasing resolving time by more than 50%. Performance comparison with the test from commit 1 by setting `LAST_CRATE_VERSION_COUNT = 100`: | commit | duration | |-----------------|----------| | master | 16s | | with rustc-hash | 6.7s | Firefox profiles, can be read with https://profiler.firefox.com: [perf.tar.gz](https://github.com/user-attachments/files/17318243/perf.tar.gz) r? Eh2406
2 parents 15fbd2f + 3d4d48b commit 7aa7fb1

File tree

8 files changed

+77
-24
lines changed

8 files changed

+77
-24
lines changed

Cargo.lock

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pulldown-cmark = { version = "0.11.0", default-features = false, features = ["ht
8080
rand = "0.8.5"
8181
regex = "1.10.5"
8282
rusqlite = { version = "0.32.0", features = ["bundled"] }
83+
rustc-hash = "2.0.0"
8384
rustfix = { version = "0.8.2", path = "crates/rustfix" }
8485
same-file = "1.0.6"
8586
security-framework = "2.11.1"
@@ -187,6 +188,7 @@ pathdiff.workspace = true
187188
rand.workspace = true
188189
regex.workspace = true
189190
rusqlite.workspace = true
191+
rustc-hash.workspace = true
190192
rustfix.workspace = true
191193
same-file.workspace = true
192194
semver.workspace = true

crates/resolver-tests/src/sat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl SatResolver {
352352

353353
let mut by_name: HashMap<InternedString, Vec<Summary>> = HashMap::new();
354354
for pkg in registry {
355-
by_name.entry(pkg.name()).or_default().push(pkg.clone())
355+
by_name.entry(pkg.name()).or_default().push(pkg.clone());
356356
}
357357

358358
let mut solver = varisat::Solver::new();

crates/resolver-tests/tests/resolve.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use cargo::util::GlobalContext;
44

55
use resolver_tests::{
66
helpers::{
7-
assert_contains, assert_same, dep, dep_kind, dep_loc, dep_req, loc_names, names, pkg_id,
8-
pkg_loc, registry, ToPkgId,
7+
assert_contains, assert_same, dep, dep_kind, dep_loc, dep_req, loc_names, names, pkg,
8+
pkg_dep, pkg_dep_with, pkg_id, pkg_loc, registry, ToDep, ToPkgId,
99
},
1010
pkg, resolve, resolve_with_global_context,
1111
};
@@ -937,6 +937,50 @@ fn large_conflict_cache() {
937937
let _ = resolve(root_deps, &reg);
938938
}
939939

940+
#[test]
941+
fn resolving_slow_case_missing_feature() {
942+
let mut reg = Vec::new();
943+
944+
const LAST_CRATE_VERSION_COUNT: usize = 50;
945+
946+
// increase in resolve time is at least cubic over `INTERMEDIATE_CRATES_VERSION_COUNT`.
947+
// it should be `>= LAST_CRATE_VERSION_COUNT` to reproduce slowdown.
948+
const INTERMEDIATE_CRATES_VERSION_COUNT: usize = LAST_CRATE_VERSION_COUNT + 5;
949+
950+
// should be `>= 2` to reproduce slowdown
951+
const TRANSITIVE_CRATES_COUNT: usize = 3;
952+
953+
reg.push(pkg_dep_with(("last", "1.0.0"), vec![], &[("f", &[])]));
954+
for v in 1..LAST_CRATE_VERSION_COUNT {
955+
reg.push(pkg(("last", format!("1.0.{v}"))));
956+
}
957+
958+
reg.push(pkg_dep(
959+
("dep", "1.0.0"),
960+
vec![
961+
dep("last"), // <-- needed to reproduce slowdown
962+
dep_req("intermediate-1", "1.0.0"),
963+
],
964+
));
965+
966+
for n in 0..INTERMEDIATE_CRATES_VERSION_COUNT {
967+
let version = format!("1.0.{n}");
968+
for c in 1..TRANSITIVE_CRATES_COUNT {
969+
reg.push(pkg_dep(
970+
(format!("intermediate-{c}"), &version),
971+
vec![dep_req(&format!("intermediate-{}", c + 1), &version)],
972+
));
973+
}
974+
reg.push(pkg_dep(
975+
(format!("intermediate-{TRANSITIVE_CRATES_COUNT}"), &version),
976+
vec![dep_req("last", "1.0.0").with(&["f"])],
977+
));
978+
}
979+
980+
let deps = vec![dep("dep")];
981+
let _ = resolve(deps, &reg);
982+
}
983+
940984
#[test]
941985
fn cyclic_good_error_message() {
942986
let input = vec![

src/cargo/core/resolver/conflict_cache.rs

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

3+
use rustc_hash::{FxHashMap, FxHashSet};
34
use tracing::trace;
45

56
use super::types::ConflictMap;
@@ -140,17 +141,17 @@ pub(super) struct ConflictCache {
140141
// as a global cache which we never delete from. Any entry in this map is
141142
// unconditionally true regardless of our resolution history of how we got
142143
// here.
143-
con_from_dep: HashMap<Dependency, ConflictStoreTrie>,
144+
con_from_dep: FxHashMap<Dependency, ConflictStoreTrie>,
144145
// `dep_from_pid` is an inverse-index of `con_from_dep`.
145146
// For every `PackageId` this lists the `Dependency`s that mention it in `dep_from_pid`.
146-
dep_from_pid: HashMap<PackageId, HashSet<Dependency>>,
147+
dep_from_pid: FxHashMap<PackageId, FxHashSet<Dependency>>,
147148
}
148149

149150
impl ConflictCache {
150151
pub fn new() -> ConflictCache {
151152
ConflictCache {
152-
con_from_dep: HashMap::new(),
153-
dep_from_pid: HashMap::new(),
153+
con_from_dep: HashMap::default(),
154+
dep_from_pid: HashMap::default(),
154155
}
155156
}
156157
pub fn find(
@@ -207,14 +208,11 @@ impl ConflictCache {
207208
);
208209

209210
for c in con.keys() {
210-
self.dep_from_pid
211-
.entry(*c)
212-
.or_insert_with(HashSet::new)
213-
.insert(dep.clone());
211+
self.dep_from_pid.entry(*c).or_default().insert(dep.clone());
214212
}
215213
}
216214

217-
pub fn dependencies_conflicting_with(&self, pid: PackageId) -> Option<&HashSet<Dependency>> {
215+
pub fn dependencies_conflicting_with(&self, pid: PackageId) -> Option<&FxHashSet<Dependency>> {
218216
self.dep_from_pid.get(&pid)
219217
}
220218
}

src/cargo/core/resolver/context.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ pub struct ResolverContext {
1919
pub age: ContextAge,
2020
pub activations: Activations,
2121
/// list the features that are activated for each package
22-
pub resolve_features: im_rc::HashMap<PackageId, FeaturesSet>,
22+
pub resolve_features: im_rc::HashMap<PackageId, FeaturesSet, rustc_hash::FxBuildHasher>,
2323
/// get the package that will be linking to a native library by its links attribute
24-
pub links: im_rc::HashMap<InternedString, PackageId>,
24+
pub links: im_rc::HashMap<InternedString, PackageId, rustc_hash::FxBuildHasher>,
2525

2626
/// a way to look up for a package in activations what packages required it
2727
/// and all of the exact deps that it fulfilled.
28-
pub parents: Graph<PackageId, im_rc::HashSet<Dependency>>,
28+
pub parents: Graph<PackageId, im_rc::HashSet<Dependency, rustc_hash::FxBuildHasher>>,
2929
}
3030

3131
/// When backtracking it can be useful to know how far back to go.
@@ -40,7 +40,9 @@ pub type ContextAge = usize;
4040
/// semver compatible version of each crate.
4141
/// This all so stores the `ContextAge`.
4242
pub type ActivationsKey = (InternedString, SourceId, SemverCompatibility);
43-
pub type Activations = im_rc::HashMap<ActivationsKey, (Summary, ContextAge)>;
43+
44+
pub type Activations =
45+
im_rc::HashMap<ActivationsKey, (Summary, ContextAge), rustc_hash::FxBuildHasher>;
4446

4547
/// A type that represents when cargo treats two Versions as compatible.
4648
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the
@@ -74,10 +76,10 @@ impl ResolverContext {
7476
pub fn new() -> ResolverContext {
7577
ResolverContext {
7678
age: 0,
77-
resolve_features: im_rc::HashMap::new(),
78-
links: im_rc::HashMap::new(),
79+
resolve_features: im_rc::HashMap::default(),
80+
links: im_rc::HashMap::default(),
7981
parents: Graph::new(),
80-
activations: im_rc::HashMap::new(),
82+
activations: im_rc::HashMap::default(),
8183
}
8284
}
8385

src/cargo/core/resolver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ fn activate(
679679
Rc::make_mut(
680680
cx.resolve_features
681681
.entry(candidate.package_id())
682-
.or_insert_with(Rc::default),
682+
.or_default(),
683683
)
684684
.extend(used_features);
685685
}

src/cargo/util/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<N: Eq + Ord + Clone, E: Default + Clone> Graph<N, E> {
2222
.entry(node)
2323
.or_insert_with(im_rc::OrdMap::new)
2424
.entry(child)
25-
.or_insert_with(Default::default)
25+
.or_default()
2626
}
2727

2828
/// Returns the graph obtained by reversing all edges.

0 commit comments

Comments
 (0)