@@ -10,7 +10,7 @@ use std::io::IsTerminal;
10
10
use std:: path:: { Path , PathBuf , absolute} ;
11
11
use std:: process:: Command ;
12
12
use std:: str:: FromStr ;
13
- use std:: sync:: OnceLock ;
13
+ use std:: sync:: { LazyLock , Mutex , OnceLock } ;
14
14
use std:: { cmp, env, fs} ;
15
15
16
16
use build_helper:: ci:: CiEnv ;
@@ -3130,17 +3130,34 @@ impl Config {
3130
3130
}
3131
3131
3132
3132
/// Returns true if any of the `paths` have been modified locally.
3133
- pub fn has_changes_from_upstream ( & self , paths : & [ & str ] ) -> bool {
3133
+ pub fn has_changes_from_upstream ( & self , paths : & [ & ' static str ] ) -> bool {
3134
3134
match self . check_path_modifications ( paths) {
3135
3135
PathFreshness :: LastModifiedUpstream { .. } => false ,
3136
3136
PathFreshness :: HasLocalModifications { .. } | PathFreshness :: MissingUpstream => true ,
3137
3137
}
3138
3138
}
3139
3139
3140
3140
/// Checks whether any of the given paths have been modified w.r.t. upstream.
3141
- pub fn check_path_modifications ( & self , paths : & [ & str ] ) -> PathFreshness {
3142
- check_path_modifications ( Some ( & self . src ) , & self . git_config ( ) , paths, CiEnv :: current ( ) )
3141
+ pub fn check_path_modifications ( & self , paths : & [ & ' static str ] ) -> PathFreshness {
3142
+ // Checking path modifications through git can be relatively expensive (>100ms).
3143
+ // We do not assume that the sources would change during bootstrap's execution,
3144
+ // so we can cache the results here.
3145
+ static MODIFICATION_CACHE : LazyLock < Mutex < HashMap < Vec < & ' static str > , PathFreshness > > > =
3146
+ LazyLock :: new ( || Mutex :: new ( Default :: default ( ) ) ) ;
3147
+ MODIFICATION_CACHE
3148
+ . lock ( )
3143
3149
. unwrap ( )
3150
+ . entry ( paths. to_vec ( ) )
3151
+ . or_insert_with ( || {
3152
+ check_path_modifications (
3153
+ Some ( & self . src ) ,
3154
+ & self . git_config ( ) ,
3155
+ paths,
3156
+ CiEnv :: current ( ) ,
3157
+ )
3158
+ . unwrap ( )
3159
+ } )
3160
+ . clone ( )
3144
3161
}
3145
3162
}
3146
3163
0 commit comments