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+
112use std:: cmp:: Ordering ;
213use std:: collections:: { BTreeSet , HashMap , HashSet } ;
314use std:: rc:: Rc ;
@@ -8,7 +19,7 @@ use crate::core::interning::InternedString;
819use crate :: core:: { Dependency , FeatureValue , PackageId , PackageIdSpec , Registry , Summary } ;
920use crate :: util:: errors:: CargoResult ;
1021
11- use crate :: core:: resolver:: types:: { Candidate , ConflictReason , DepInfo } ;
22+ use crate :: core:: resolver:: types:: { Candidate , ConflictReason , DepInfo , FeaturesSet } ;
1223use crate :: core:: resolver:: { ActivateResult , Method } ;
1324
1425pub 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.
240260pub 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
0 commit comments