Skip to content

Commit 623863e

Browse files
committed
add lots of comments
1 parent 5ae13e3 commit 623863e

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

src/cargo/core/resolver/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::{BTreeSet, HashMap};
1+
use std::collections::HashMap;
22
use std::num::NonZeroU64;
33
use std::rc::Rc;
44

@@ -13,7 +13,7 @@ use crate::util::CargoResult;
1313
use crate::util::Graph;
1414

1515
use super::dep_cache::RegistryQueryer;
16-
use super::types::{ConflictMap, Method};
16+
use super::types::{ConflictMap, FeaturesSet, Method};
1717

1818
pub use super::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
1919
pub use super::encode::{Metadata, WorkspaceResolve};
@@ -27,7 +27,7 @@ pub use super::resolve::Resolve;
2727
pub struct Context {
2828
pub activations: Activations,
2929
/// list the features that are activated for each package
30-
pub resolve_features: im_rc::HashMap<PackageId, Rc<BTreeSet<InternedString>>>,
30+
pub resolve_features: im_rc::HashMap<PackageId, FeaturesSet>,
3131
/// get the package that will be linking to a native library by its links attribute
3232
pub links: im_rc::HashMap<InternedString, PackageId>,
3333
/// for each package the list of names it can see,

src/cargo/core/resolver/dep_cache.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
//! There are 2 sources of facts for the resolver:
2+
//!
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.
5+
//!
6+
//! These constitute immutable facts, the soled ground truth that all other inference depends on.
7+
//! Theoretically this could all be enumerated ahead of time, but we want to be lazy and only
8+
//! look up things we need to. The compromise is to cache the results as they are computed.
9+
//!
10+
//! The structs in this module impl that cache in all the gory details
11+
112
use std::cmp::Ordering;
213
use std::collections::{BTreeSet, HashMap, HashSet};
314
use std::rc::Rc;
@@ -8,7 +19,7 @@ use crate::core::interning::InternedString;
819
use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec, Registry, Summary};
920
use crate::util::errors::CargoResult;
1021

11-
use crate::core::resolver::types::{Candidate, ConflictReason, DepInfo};
22+
use crate::core::resolver::types::{Candidate, ConflictReason, DepInfo, FeaturesSet};
1223
use crate::core::resolver::{ActivateResult, Method};
1324

1425
pub struct RegistryQueryer<'a> {
@@ -193,12 +204,18 @@ impl<'a> DepsCache<'a> {
193204
}
194205
}
195206

207+
/// Find out what dependencies will be added by activating `candidate`,
208+
/// with features described in `method`. Then look up in the `registry`
209+
/// the candidates that will fulfil each of these dependencies, as it is the
210+
/// next obvious question.
196211
pub fn build_deps(
197212
&mut self,
198213
parent: Option<PackageId>,
199214
candidate: &Summary,
200215
method: &Method,
201216
) -> ActivateResult<Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>> {
217+
// if we have calculated a result before, then we can just return it,
218+
// as it is a "pure" query of its arguments.
202219
if let Some(out) = self
203220
.cache
204221
.get(&(parent, candidate.clone(), method.clone()))
@@ -217,7 +234,7 @@ impl<'a> DepsCache<'a> {
217234
.into_iter()
218235
.map(|(dep, features)| {
219236
let candidates = self.registry.query(&dep)?;
220-
Ok((dep, candidates, Rc::new(features)))
237+
Ok((dep, candidates, features))
221238
})
222239
.collect::<CargoResult<Vec<DepInfo>>>()?;
223240

@@ -229,22 +246,22 @@ impl<'a> DepsCache<'a> {
229246

230247
let out = Rc::new((used_features, Rc::new(deps)));
231248

249+
// If we succeed we add the result to the cache so we can use it again next time.
250+
// We dont cache the failure cases as they dont impl Clone.
232251
self.cache
233252
.insert((parent, candidate.clone(), method.clone()), out.clone());
234253

235254
Ok(out)
236255
}
237256
}
238257

239-
/// Returns all dependencies and the features we want from them.
258+
/// Returns the features we ended up using and
259+
/// all dependencies and the features we want from each of them.
240260
pub fn resolve_features<'b>(
241261
parent: Option<PackageId>,
242262
s: &'b Summary,
243263
method: &'b Method,
244-
) -> ActivateResult<(
245-
HashSet<InternedString>,
246-
Vec<(Dependency, BTreeSet<InternedString>)>,
247-
)> {
264+
) -> ActivateResult<(HashSet<InternedString>, Vec<(Dependency, FeaturesSet)>)> {
248265
let dev_deps = match *method {
249266
Method::Everything => true,
250267
Method::Required { dev_deps, .. } => dev_deps,
@@ -303,7 +320,7 @@ pub fn resolve_features<'b>(
303320
.into());
304321
}
305322
}
306-
ret.push((dep.clone(), base));
323+
ret.push((dep.clone(), Rc::new(base)));
307324
}
308325

309326
// Any entries in `reqs.dep` which weren't used are bugs in that the

src/cargo/core/resolver/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@
4747
//! that we're implementing something that probably shouldn't be allocating all
4848
//! over the place.
4949
50-
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
50+
use std::collections::{BTreeMap, HashMap, HashSet};
5151
use std::mem;
5252
use std::rc::Rc;
5353
use std::time::{Duration, Instant};
5454

5555
use log::{debug, trace};
5656

57-
use crate::core::interning::InternedString;
5857
use crate::core::PackageIdSpec;
5958
use crate::core::{Dependency, PackageId, Registry, Summary};
6059
use crate::util::config::Config;
@@ -64,7 +63,7 @@ use crate::util::profile;
6463
use self::context::{Activations, Context};
6564
use self::dep_cache::{DepsCache, RegistryQueryer};
6665
use self::types::{Candidate, ConflictMap, ConflictReason, DepsFrame};
67-
use self::types::{RcVecIter, RemainingDeps, ResolverProgress};
66+
use self::types::{FeaturesSet, RcVecIter, RemainingDeps, ResolverProgress};
6867

6968
pub use self::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
7069
pub use self::encode::{Metadata, WorkspaceResolve};
@@ -712,7 +711,7 @@ struct BacktrackFrame {
712711
remaining_candidates: RemainingCandidates,
713712
parent: Summary,
714713
dep: Dependency,
715-
features: Rc<BTreeSet<InternedString>>,
714+
features: FeaturesSet,
716715
conflicting_activations: ConflictMap,
717716
}
718717

src/cargo/core/resolver/types.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,22 @@ impl ResolverProgress {
8989
}
9090
}
9191

92+
/// The preferred way to store the set of activated features for a package.
93+
/// This is sorted so that it impls Hash, and owns its contents,
94+
/// needed so it can be part of the key for caching in the `DepsCache`.
95+
/// It is also cloned often as part of `Context`, hence the `RC`.
96+
/// `im-rs::OrdSet` was slower of small sets like this,
97+
/// but this can change with improvements to std, im, or llvm.
98+
/// Using a consistent type for this allows us to use the highly
99+
/// optimized comparison operators like `is_subset` at the interfaces.
100+
pub type FeaturesSet = Rc<BTreeSet<InternedString>>;
101+
92102
#[derive(Clone, Eq, PartialEq, Hash)]
93103
pub enum Method {
94104
Everything, // equivalent to Required { dev_deps: true, all_features: true, .. }
95105
Required {
96106
dev_deps: bool,
97-
features: Rc<BTreeSet<InternedString>>,
107+
features: FeaturesSet,
98108
all_features: bool,
99109
uses_default_features: bool,
100110
},
@@ -221,7 +231,7 @@ impl RemainingDeps {
221231
/// Information about the dependencies for a crate, a tuple of:
222232
///
223233
/// (dependency info, candidates, features activated)
224-
pub type DepInfo = (Dependency, Rc<Vec<Candidate>>, Rc<BTreeSet<InternedString>>);
234+
pub type DepInfo = (Dependency, Rc<Vec<Candidate>>, FeaturesSet);
225235

226236
/// All possible reasons that a package might fail to activate.
227237
///

0 commit comments

Comments
 (0)