Skip to content

Commit 4a57b3d

Browse files
committed
Cache result of check_path_modifications
1 parent 1abf419 commit 4a57b3d

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/bootstrap/src/core/config/config.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::io::IsTerminal;
1010
use std::path::{Path, PathBuf, absolute};
1111
use std::process::Command;
1212
use std::str::FromStr;
13-
use std::sync::OnceLock;
13+
use std::sync::{LazyLock, Mutex, OnceLock};
1414
use std::{cmp, env, fs};
1515

1616
use build_helper::ci::CiEnv;
@@ -3130,17 +3130,34 @@ impl Config {
31303130
}
31313131

31323132
/// 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 {
31343134
match self.check_path_modifications(paths) {
31353135
PathFreshness::LastModifiedUpstream { .. } => false,
31363136
PathFreshness::HasLocalModifications { .. } | PathFreshness::MissingUpstream => true,
31373137
}
31383138
}
31393139

31403140
/// 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()
31433149
.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()
31443161
}
31453162
}
31463163

src/build_helper/src/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn output_result(cmd: &mut Command) -> Result<String, String> {
3232

3333
/// Represents the result of checking whether a set of paths
3434
/// have been modified locally or not.
35-
#[derive(PartialEq, Debug)]
35+
#[derive(PartialEq, Debug, Clone)]
3636
pub enum PathFreshness {
3737
/// Artifacts should be downloaded from this upstream commit,
3838
/// there are no local modifications.

0 commit comments

Comments
 (0)