Skip to content

Commit 10db1be

Browse files
committed
Cache the query result.
In a test on rust-lang#4810 (comment) Before we got to 1700000 ticks in ~97 sec After we got to 1700000 ticks in ~92 sec And query disappears from the flame graph
1 parent 5927a1d commit 10db1be

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/cargo/core/dependency.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ use util::errors::{CargoResult, CargoResultExt, CargoError};
1212

1313
/// Information about a dependency requested by a Cargo manifest.
1414
/// Cheap to copy.
15-
#[derive(PartialEq, Clone, Debug)]
15+
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
1616
pub struct Dependency {
1717
inner: Rc<Inner>,
1818
}
1919

2020
/// The data underlying a Dependency.
21-
#[derive(PartialEq, Clone, Debug)]
21+
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
2222
struct Inner {
2323
name: String,
2424
source_id: SourceId,
@@ -38,7 +38,7 @@ struct Inner {
3838
platform: Option<Platform>,
3939
}
4040

41-
#[derive(Clone, Debug, PartialEq)]
41+
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
4242
pub enum Platform {
4343
Name(String),
4444
Cfg(CfgExpr),
@@ -76,7 +76,7 @@ impl ser::Serialize for Dependency {
7676
}
7777
}
7878

79-
#[derive(PartialEq, Clone, Debug, Copy)]
79+
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug, Copy)]
8080
pub enum Kind {
8181
Normal,
8282
Development,

src/cargo/core/resolver/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,13 +1231,8 @@ impl<'a> Context<'a> {
12311231
// Next, transform all dependencies into a list of possible candidates
12321232
// which can satisfy that dependency.
12331233
let mut deps = deps.into_iter().map(|(dep, features)| {
1234-
let mut candidates = self.query(registry, &dep)?;
1235-
// When we attempt versions for a package, we'll want to start at
1236-
// the maximum version and work our way down.
1237-
candidates.sort_by(|a, b| {
1238-
b.summary.version().cmp(a.summary.version())
1239-
});
1240-
Ok((dep, Rc::new(candidates), Rc::new(features)))
1234+
let candidates = self.query(registry, &dep)?;
1235+
Ok((dep, candidates, Rc::new(features)))
12411236
}).collect::<CargoResult<Vec<DepInfo>>>()?;
12421237

12431238
// Attempt to resolve dependencies with fewer candidates before trying
@@ -1257,7 +1252,13 @@ impl<'a> Context<'a> {
12571252
/// return.
12581253
fn query(&self,
12591254
registry: &mut Registry,
1260-
dep: &Dependency) -> CargoResult<Vec<Candidate>> {
1255+
dep: &Dependency) -> CargoResult<Rc<Vec<Candidate>>> {
1256+
use ::std::cell::RefCell;
1257+
thread_local!(static CACHE: RefCell<BTreeMap<Dependency, Rc<Vec<Candidate>>>> = RefCell::new(BTreeMap::new()));
1258+
if let Some(out) = CACHE.with(|m| m.borrow().get(dep).cloned()) {
1259+
return Ok(out);
1260+
}
1261+
12611262
let mut ret = Vec::new();
12621263
registry.query(dep, &mut |s| {
12631264
ret.push(Candidate { summary: s, replace: None });
@@ -1318,7 +1319,18 @@ impl<'a> Context<'a> {
13181319

13191320
candidate.replace = replace;
13201321
}
1321-
Ok(ret)
1322+
1323+
// When we attempt versions for a package, we'll want to start at
1324+
// the maximum version and work our way down.
1325+
ret.sort_unstable_by(|a, b| {
1326+
b.summary.version().cmp(a.summary.version())
1327+
});
1328+
1329+
let out = Rc::new(ret);
1330+
1331+
CACHE.with(|m| m.borrow_mut().insert(dep.clone(), out.clone()));
1332+
1333+
Ok(out)
13221334
}
13231335

13241336
fn prev_active(&self, dep: &Dependency) -> &[Summary] {

src/cargo/util/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use std::fmt;
44

55
use util::{CargoError, CargoResult};
66

7-
#[derive(Clone, PartialEq, Debug)]
7+
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
88
pub enum Cfg {
99
Name(String),
1010
KeyPair(String, String),
1111
}
1212

13-
#[derive(Clone, PartialEq, Debug)]
13+
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
1414
pub enum CfgExpr {
1515
Not(Box<CfgExpr>),
1616
All(Vec<CfgExpr>),

0 commit comments

Comments
 (0)