Skip to content

Commit 79714ba

Browse files
committed
combine the caching structs
1 parent 623863e commit 79714ba

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

src/cargo/core/resolver/dep_cache.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! There are 2 sources of facts for the resolver:
22
//!
3-
//! - The Registry tells us for a Dependency what versions are available to fulfil it.
4-
//! - The Summary tells us for a version (and features) what dependencies need to be fulfilled for it to be activated.
3+
//! - The `Registry` tells us for a `Dependency` what versions are available to fulfil it.
4+
//! - The `Summary` tells us for a version (and features) what dependencies need to be fulfilled for it to be activated.
55
//!
66
//! These constitute immutable facts, the soled ground truth that all other inference depends on.
77
//! Theoretically this could all be enumerated ahead of time, but we want to be lazy and only
88
//! look up things we need to. The compromise is to cache the results as they are computed.
99
//!
10-
//! The structs in this module impl that cache in all the gory details
10+
//! This module impl that cache in all the gory details
1111
1212
use std::cmp::Ordering;
1313
use std::collections::{BTreeSet, HashMap, HashSet};
@@ -26,11 +26,18 @@ pub struct RegistryQueryer<'a> {
2626
pub registry: &'a mut (dyn Registry + 'a),
2727
replacements: &'a [(PackageIdSpec, Dependency)],
2828
try_to_use: &'a HashSet<PackageId>,
29-
// If set the list of dependency candidates will be sorted by minimal
30-
// versions first. That allows `cargo update -Z minimal-versions` which will
31-
// specify minimum dependency versions to be used.
29+
/// If set the list of dependency candidates will be sorted by minimal
30+
/// versions first. That allows `cargo update -Z minimal-versions` which will
31+
/// specify minimum dependency versions to be used.
3232
minimal_versions: bool,
33-
cache: HashMap<Dependency, Rc<Vec<Candidate>>>,
33+
/// a cache of `Candidate`s that fulfil a `Dependency`
34+
registry_cache: HashMap<Dependency, Rc<Vec<Candidate>>>,
35+
/// a cache of `Dependency`s that are required for a `Summary`
36+
summary_cache: HashMap<
37+
(Option<PackageId>, Summary, Method),
38+
Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>,
39+
>,
40+
/// all the cases we ended up using a supplied replacement
3441
used_replacements: HashMap<PackageId, PackageId>,
3542
}
3643

@@ -46,7 +53,8 @@ impl<'a> RegistryQueryer<'a> {
4653
replacements,
4754
try_to_use,
4855
minimal_versions,
49-
cache: HashMap::new(),
56+
registry_cache: HashMap::new(),
57+
summary_cache: HashMap::new(),
5058
used_replacements: HashMap::new(),
5159
}
5260
}
@@ -62,7 +70,7 @@ impl<'a> RegistryQueryer<'a> {
6270
/// applied by performing a second query for what the override should
6371
/// return.
6472
pub fn query(&mut self, dep: &Dependency) -> CargoResult<Rc<Vec<Candidate>>> {
65-
if let Some(out) = self.cache.get(dep).cloned() {
73+
if let Some(out) = self.registry_cache.get(dep).cloned() {
6674
return Ok(out);
6775
}
6876

@@ -182,27 +190,10 @@ impl<'a> RegistryQueryer<'a> {
182190

183191
let out = Rc::new(ret);
184192

185-
self.cache.insert(dep.clone(), out.clone());
193+
self.registry_cache.insert(dep.clone(), out.clone());
186194

187195
Ok(out)
188196
}
189-
}
190-
191-
pub struct DepsCache<'a> {
192-
pub registry: RegistryQueryer<'a>,
193-
cache: HashMap<
194-
(Option<PackageId>, Summary, Method),
195-
Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>,
196-
>,
197-
}
198-
199-
impl<'a> DepsCache<'a> {
200-
pub fn new(registry: RegistryQueryer<'a>) -> Self {
201-
DepsCache {
202-
registry,
203-
cache: HashMap::new(),
204-
}
205-
}
206197

207198
/// Find out what dependencies will be added by activating `candidate`,
208199
/// with features described in `method`. Then look up in the `registry`
@@ -217,7 +208,7 @@ impl<'a> DepsCache<'a> {
217208
// if we have calculated a result before, then we can just return it,
218209
// as it is a "pure" query of its arguments.
219210
if let Some(out) = self
220-
.cache
211+
.summary_cache
221212
.get(&(parent, candidate.clone(), method.clone()))
222213
.cloned()
223214
{
@@ -233,7 +224,7 @@ impl<'a> DepsCache<'a> {
233224
let mut deps = deps
234225
.into_iter()
235226
.map(|(dep, features)| {
236-
let candidates = self.registry.query(&dep)?;
227+
let candidates = self.query(&dep)?;
237228
Ok((dep, candidates, features))
238229
})
239230
.collect::<CargoResult<Vec<DepInfo>>>()?;
@@ -248,7 +239,7 @@ impl<'a> DepsCache<'a> {
248239

249240
// If we succeed we add the result to the cache so we can use it again next time.
250241
// We dont cache the failure cases as they dont impl Clone.
251-
self.cache
242+
self.summary_cache
252243
.insert((parent, candidate.clone(), method.clone()), out.clone());
253244

254245
Ok(out)

src/cargo/core/resolver/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::util::errors::CargoResult;
6161
use crate::util::profile;
6262

6363
use self::context::{Activations, Context};
64-
use self::dep_cache::{DepsCache, RegistryQueryer};
64+
use self::dep_cache::RegistryQueryer;
6565
use self::types::{Candidate, ConflictMap, ConflictReason, DepsFrame};
6666
use self::types::{FeaturesSet, RcVecIter, RemainingDeps, ResolverProgress};
6767

@@ -133,8 +133,7 @@ pub fn resolve(
133133
Some(config) => config.cli_unstable().minimal_versions,
134134
None => false,
135135
};
136-
let registry = RegistryQueryer::new(registry, replacements, try_to_use, minimal_versions);
137-
let mut registry = DepsCache::new(registry);
136+
let mut registry = RegistryQueryer::new(registry, replacements, try_to_use, minimal_versions);
138137
let cx = activate_deps_loop(cx, &mut registry, summaries, config)?;
139138

140139
let mut cksums = HashMap::new();
@@ -144,7 +143,7 @@ pub fn resolve(
144143
}
145144
let resolve = Resolve::new(
146145
cx.graph(),
147-
cx.resolve_replacements(&registry.registry),
146+
cx.resolve_replacements(&registry),
148147
cx.resolve_features
149148
.iter()
150149
.map(|(k, v)| (*k, v.iter().map(|x| x.to_string()).collect()))
@@ -168,7 +167,7 @@ pub fn resolve(
168167
/// dependency graph, cx.resolve is returned.
169168
fn activate_deps_loop(
170169
mut cx: Context,
171-
registry: &mut DepsCache<'_>,
170+
registry: &mut RegistryQueryer<'_>,
172171
summaries: &[(Summary, Method)],
173172
config: Option<&Config>,
174173
) -> CargoResult<Context> {
@@ -328,7 +327,7 @@ fn activate_deps_loop(
328327
debug!("no candidates found");
329328
Err(errors::activation_error(
330329
&cx,
331-
registry.registry.registry,
330+
registry.registry,
332331
&parent,
333332
&dep,
334333
&conflicting_activations,
@@ -594,7 +593,7 @@ fn activate_deps_loop(
594593
/// iterate through next.
595594
fn activate(
596595
cx: &mut Context,
597-
registry: &mut DepsCache<'_>,
596+
registry: &mut RegistryQueryer<'_>,
598597
parent: Option<(&Summary, &Dependency)>,
599598
candidate: Candidate,
600599
method: Method,
@@ -858,7 +857,7 @@ impl RemainingCandidates {
858857
/// Panics if the input conflict is not all active in `cx`.
859858
fn generalize_conflicting(
860859
cx: &Context,
861-
registry: &mut DepsCache<'_>,
860+
registry: &mut RegistryQueryer<'_>,
862861
past_conflicting_activations: &mut conflict_cache::ConflictCache,
863862
parent: &Summary,
864863
dep: &Dependency,
@@ -897,7 +896,6 @@ fn generalize_conflicting(
897896
// Thus, if all the things it can resolve to have already ben determined
898897
// to be conflicting, then we can just say that we conflict with the parent.
899898
if registry
900-
.registry
901899
.query(critical_parents_dep)
902900
.expect("an already used dep now error!?")
903901
.iter()

0 commit comments

Comments
 (0)