@@ -8,16 +8,13 @@ use tracing::{error, warn};
88
99type Loaded < W > = ( Option < W > , ExtractionStep ) ;
1010
11- /// Workspaces loaded from manifests, keeping the `capacity` most recently used ones so that they
12- /// need not be reloaded when extracting dependencies. Optionally, the manifest that is going to be
13- /// requested next is loaded in the background meanwhile. At most `capacity + 1` workspaces are
14- /// held at any time.
11+ /// The workspace loaded from the last requested manifest, kept so that it need not be reloaded
12+ /// if requested again. Optionally, the manifest that is going to be requested next is loaded in
13+ /// the background meanwhile, so at most two workspaces are held at any time.
1514pub struct LoadedWorkspaces < ' m , W , L > {
16- /// Most recently used last.
17- loaded : Vec < ( & ' m ProjectManifest , W ) > ,
15+ current : Option < ( & ' m ProjectManifest , W ) > ,
1816 prefetched : Option < ( & ' m ProjectManifest , JoinHandle < Loaded < W > > ) > ,
1917 load : Arc < L > ,
20- capacity : usize ,
2118 prefetch : bool ,
2219}
2320
@@ -26,19 +23,17 @@ where
2623 W : Send + ' static ,
2724 L : Fn ( & ProjectManifest ) -> Option < W > + Send + Sync + ' static ,
2825{
29- pub fn new ( capacity : usize , prefetch : bool , load : L ) -> Self {
30- assert ! ( capacity > 0 ) ;
26+ pub fn new ( prefetch : bool , load : L ) -> Self {
3127 Self {
32- loaded : Vec :: with_capacity ( capacity ) ,
28+ current : None ,
3329 prefetched : None ,
3430 load : Arc :: new ( load) ,
35- capacity,
3631 prefetch,
3732 }
3833 }
3934
4035 pub fn contains ( & self , manifest : & ProjectManifest ) -> bool {
41- self . loaded . iter ( ) . any ( |( m, _) | * m == manifest)
36+ self . current . as_ref ( ) . is_some_and ( |( m, _) | * m == manifest)
4237 }
4338
4439 /// Returns the workspace of `manifest`, loading it unless still loaded, or `None` if it cannot
@@ -49,27 +44,17 @@ where
4944 next : Option < & ' m ProjectManifest > ,
5045 steps : & mut Vec < ExtractionStep > ,
5146 ) -> Option < & W > {
52- let found = if let Some ( index) = self . loaded . iter ( ) . position ( |( m, _) | * m == manifest) {
53- let entry = self . loaded . remove ( index) ;
54- self . loaded . push ( entry) ;
55- true
56- } else {
47+ if !self . contains ( manifest) {
5748 let prefetched = self . take_prefetched ( manifest, steps) ;
49+ self . current = None ;
5850 let ( workspace, step) = prefetched. unwrap_or_else ( || load_timed ( & * self . load , manifest) ) ;
5951 steps. push ( step) ;
60- workspace. is_some_and ( |workspace| {
61- // evict before starting the next prefetch to bound the number of workspaces held
62- if self . loaded . len ( ) == self . capacity {
63- self . loaded . remove ( 0 ) ;
64- }
65- self . loaded . push ( ( manifest, workspace) ) ;
66- true
67- } )
68- } ;
52+ self . current = workspace. map ( |workspace| ( manifest, workspace) ) ;
53+ }
6954 if let Some ( next) = next {
7055 self . start_prefetch ( next) ;
7156 }
72- let ( _, workspace) = self . loaded . last ( ) . filter ( |_| found ) ?;
57+ let ( _, workspace) = self . current . as_ref ( ) ?;
7358 Some ( workspace)
7459 }
7560
@@ -132,29 +117,42 @@ mod tests {
132117 use super :: * ;
133118 use std:: sync:: Mutex ;
134119 use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
120+ use std:: sync:: mpsc:: { Receiver , Sender , channel} ;
121+ use std:: time:: Duration ;
135122
136- #[ derive( Default ) ]
137123 struct Stats {
138124 loads : Mutex < Vec < String > > ,
139125 live : AtomicUsize ,
140126 peak : AtomicUsize ,
127+ /// Background loads wait for a message on this, so that tests can check that `get`
128+ /// returns while the next workspace is still loading.
129+ gate : Mutex < Receiver < ( ) > > ,
141130 }
142131
143- struct Workspace ( String , Arc < Stats > ) ;
132+ struct Workspace ( Arc < Stats > ) ;
144133
145134 impl Drop for Workspace {
146135 fn drop ( & mut self ) {
147- self . 1 . live . fetch_sub ( 1 , Ordering :: SeqCst ) ;
136+ self . 0 . live . fetch_sub ( 1 , Ordering :: SeqCst ) ;
148137 }
149138 }
150139
151- fn manifests ( names : & [ & str ] ) -> Vec < ProjectManifest > {
140+ fn stats ( ) -> ( Arc < Stats > , Sender < ( ) > ) {
141+ let ( sender, receiver) = channel ( ) ;
142+ let stats = Stats {
143+ loads : Mutex :: default ( ) ,
144+ live : AtomicUsize :: default ( ) ,
145+ peak : AtomicUsize :: default ( ) ,
146+ gate : Mutex :: new ( receiver) ,
147+ } ;
148+ ( Arc :: new ( stats) , sender)
149+ }
150+
151+ fn manifests < const N : usize > ( names : [ & str ; N ] ) -> [ ProjectManifest ; N ] {
152152 let root = crate :: cwd ( ) . unwrap ( ) ;
153- names
154- . iter ( )
155- . map ( |name| ProjectManifest :: from_manifest_file ( root. join ( name) . join ( "Cargo.toml" ) ) )
156- . collect :: < Result < _ , _ > > ( )
157- . unwrap ( )
153+ names. map ( |name| {
154+ ProjectManifest :: from_manifest_file ( root. join ( name) . join ( "Cargo.toml" ) ) . unwrap ( )
155+ } )
158156 }
159157
160158 /// Loads workspaces named after their directory, failing for `broken`.
@@ -164,45 +162,55 @@ mod tests {
164162 let name = manifest. manifest_path ( ) . parent ( ) . file_name ( ) ?. to_owned ( ) ;
165163 // test threads are named, background loading ones are not
166164 let background = std:: thread:: current ( ) . name ( ) . is_none ( ) ;
165+ if background {
166+ let gate = stats. gate . lock ( ) . unwrap ( ) ;
167+ gate. recv_timeout ( Duration :: from_secs ( 10 ) )
168+ . expect ( "background load not released" ) ;
169+ }
167170 let entry = format ! ( "{name}{}" , if background { " (background)" } else { "" } ) ;
168171 stats. loads . lock ( ) . unwrap ( ) . push ( entry) ;
169172 if name == "broken" {
170173 return None ;
171174 }
172175 let live = stats. live . fetch_add ( 1 , Ordering :: SeqCst ) + 1 ;
173176 stats. peak . fetch_max ( live, Ordering :: SeqCst ) ;
174- Some ( Workspace ( name , Arc :: clone ( & stats) ) )
177+ Some ( Workspace ( Arc :: clone ( & stats) ) )
175178 }
176179 }
177180
178181 #[ test]
179- fn reuses_most_recently_used_workspaces ( ) {
180- let stats = Arc :: default ( ) ;
181- let [ a, b, c , broken] : & [ _ ; 4 ] = & manifests ( & [ "a" , "b" , "c" , " broken"] ) . try_into ( ) . unwrap ( ) ;
182- let mut loaded = LoadedWorkspaces :: new ( 2 , false , loader ( & stats) ) ;
182+ fn reuses_last_workspace ( ) {
183+ let ( stats, _ ) = stats ( ) ;
184+ let [ a, b, broken] = & manifests ( [ "a" , "b" , "broken" ] ) ;
185+ let mut loaded = LoadedWorkspaces :: new ( false , loader ( & stats) ) ;
183186 let mut steps = Vec :: new ( ) ;
184- for manifest in [ a, b , a, c , b , broken, broken] {
185- let got = loaded. get ( manifest, None , & mut steps) . map ( |w| w . 0 . clone ( ) ) ;
187+ for manifest in [ a, a, b , a , broken, broken] {
188+ let got = loaded. get ( manifest, None , & mut steps) ;
186189 assert_eq ! ( got. is_some( ) , manifest != broken) ;
187190 }
188- // `c` evicts `b`, the least recently used; failed loads are not kept
191+ // failed loads are not kept
189192 assert_eq ! (
190193 * stats. loads. lock( ) . unwrap( ) ,
191- [ "a" , "b" , "c" , "b ", "broken" , "broken" ]
194+ [ "a" , "b" , "a " , "broken" , "broken" ]
192195 ) ;
193- assert_eq ! ( steps. len( ) , 6 ) ;
194- assert ! ( loaded. contains( c) && loaded. contains( b) && !loaded. contains( a) ) ;
196+ assert_eq ! ( steps. len( ) , 5 ) ;
197+ assert ! ( !loaded. contains( a) ) ;
198+ assert_eq ! ( stats. peak. load( Ordering :: SeqCst ) , 1 ) ;
195199 }
196200
197201 #[ test]
198- fn prefetches_next_workspace_within_bound ( ) {
199- let stats = Arc :: default ( ) ;
200- let all = manifests ( & [ "a" , "b" , "broken" , "c" , "d" ] ) ;
201- let mut loaded = LoadedWorkspaces :: new ( 2 , true , loader ( & stats) ) ;
202+ fn loads_next_workspace_in_background ( ) {
203+ let ( stats, release ) = stats ( ) ;
204+ let all = manifests ( [ "a" , "b" , "broken" , "c" , "d" ] ) ;
205+ let mut loaded = LoadedWorkspaces :: new ( true , loader ( & stats) ) ;
202206 let mut steps = Vec :: new ( ) ;
203207 for ( i, manifest) in all. iter ( ) . enumerate ( ) {
204- let got = loaded. get ( manifest, all. get ( i + 1 ) , & mut steps) ;
208+ let next = all. get ( i + 1 ) ;
209+ let got = loaded. get ( manifest, next, & mut steps) ;
205210 assert_eq ! ( got. is_some( ) , i != 2 ) ;
211+ if next. is_some ( ) {
212+ release. send ( ( ) ) . unwrap ( ) ;
213+ }
206214 }
207215 assert_eq ! (
208216 * stats. loads. lock( ) . unwrap( ) ,
@@ -215,7 +223,7 @@ mod tests {
215223 ]
216224 ) ;
217225 assert_eq ! ( steps. len( ) , 5 ) ;
218- assert ! ( stats. peak. load( Ordering :: SeqCst ) <= 3 ) ;
226+ assert_eq ! ( stats. peak. load( Ordering :: SeqCst ) , 2 ) ;
219227 drop ( loaded) ;
220228 assert_eq ! ( stats. live. load( Ordering :: SeqCst ) , 0 ) ;
221229 }
0 commit comments