Skip to content

Commit

Permalink
Cache the query result.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Eh2406 committed Mar 2, 2018
1 parent 5927a1d commit 10db1be
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use util::errors::{CargoResult, CargoResultExt, CargoError};

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

/// The data underlying a Dependency.
#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
struct Inner {
name: String,
source_id: SourceId,
Expand All @@ -38,7 +38,7 @@ struct Inner {
platform: Option<Platform>,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
pub enum Platform {
Name(String),
Cfg(CfgExpr),
Expand Down Expand Up @@ -76,7 +76,7 @@ impl ser::Serialize for Dependency {
}
}

#[derive(PartialEq, Clone, Debug, Copy)]
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug, Copy)]
pub enum Kind {
Normal,
Development,
Expand Down
30 changes: 21 additions & 9 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,13 +1231,8 @@ impl<'a> Context<'a> {
// Next, transform all dependencies into a list of possible candidates
// which can satisfy that dependency.
let mut deps = deps.into_iter().map(|(dep, features)| {
let mut candidates = self.query(registry, &dep)?;
// When we attempt versions for a package, we'll want to start at
// the maximum version and work our way down.
candidates.sort_by(|a, b| {
b.summary.version().cmp(a.summary.version())
});
Ok((dep, Rc::new(candidates), Rc::new(features)))
let candidates = self.query(registry, &dep)?;
Ok((dep, candidates, Rc::new(features)))
}).collect::<CargoResult<Vec<DepInfo>>>()?;

// Attempt to resolve dependencies with fewer candidates before trying
Expand All @@ -1257,7 +1252,13 @@ impl<'a> Context<'a> {
/// return.
fn query(&self,
registry: &mut Registry,
dep: &Dependency) -> CargoResult<Vec<Candidate>> {
dep: &Dependency) -> CargoResult<Rc<Vec<Candidate>>> {
use ::std::cell::RefCell;
thread_local!(static CACHE: RefCell<BTreeMap<Dependency, Rc<Vec<Candidate>>>> = RefCell::new(BTreeMap::new()));
if let Some(out) = CACHE.with(|m| m.borrow().get(dep).cloned()) {
return Ok(out);
}

let mut ret = Vec::new();
registry.query(dep, &mut |s| {
ret.push(Candidate { summary: s, replace: None });
Expand Down Expand Up @@ -1318,7 +1319,18 @@ impl<'a> Context<'a> {

candidate.replace = replace;
}
Ok(ret)

// When we attempt versions for a package, we'll want to start at
// the maximum version and work our way down.
ret.sort_unstable_by(|a, b| {
b.summary.version().cmp(a.summary.version())
});

let out = Rc::new(ret);

CACHE.with(|m| m.borrow_mut().insert(dep.clone(), out.clone()));

Ok(out)
}

fn prev_active(&self, dep: &Dependency) -> &[Summary] {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::fmt;

use util::{CargoError, CargoResult};

#[derive(Clone, PartialEq, Debug)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
pub enum Cfg {
Name(String),
KeyPair(String, String),
}

#[derive(Clone, PartialEq, Debug)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
pub enum CfgExpr {
Not(Box<CfgExpr>),
All(Vec<CfgExpr>),
Expand Down

0 comments on commit 10db1be

Please sign in to comment.