Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions benches/preprocessor_cache_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,15 @@ fn preprocessor_cache_entry() -> (tempfile::TempDir, PreprocessorCacheEntry) {
/// direct-mode cache hit.
#[divan::bench]
fn preprocessor_cache_lookup(bencher: Bencher) {
let (_dir, mut entry) = preprocessor_cache_entry();
let (_dir, entry) = preprocessor_cache_entry();
let config = PreprocessorCacheModeConfig::activated();
// Measure the steady state of rebuilds: the headers were already looked
// at by an earlier build. CodSpeed may run the benchmark only once, which
// would otherwise measure a first lookup instead.
let mut updated = false;
assert!(
entry
.lookup_result_digest(config, None, &mut updated)
.is_some()
);
assert!(entry.lookup_result_digest(config, None).is_some());

bencher.bench_local(|| {
let mut updated = false;
let digest = entry.lookup_result_digest(config, None, &mut updated);
let digest = entry.lookup_result_digest(config, None);
assert!(digest.is_some());
black_box(digest)
});
Expand Down
168 changes: 113 additions & 55 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::cache::{FileObjectSource, Storage};
use crate::compiler::preprocessor_cache::{
compilation_tree_root, preprocessor_cache_entry_hash_key,
compilation_tree_root, preprocessor_cache_entry_hash_key, time_macro_adjusted_digest,
};
use crate::compiler::{
Cacheable, ColorMode, Compilation, CompileCommand, Compiler, CompilerArguments, CompilerHasher,
Expand Down Expand Up @@ -524,62 +524,41 @@ where
&& let Some(mut seekable) = storage
.get_preprocessor_cache_entry(preprocessor_key)
.await?
&& let Some(mut preprocessor_cache_entry) =
&& let Some(preprocessor_cache_entry) =
read_preprocessor_cache_entry(&mut seekable, preprocessor_key)
{
let mut updated = false;
let hit = preprocessor_cache_entry.lookup_result_digest(
preprocessor_cache_mode_config,
tree_root.as_deref(),
&mut updated,
);

let mut update_failed = false;
if updated {
// Time macros have been found, we need to update
// the preprocessor cache entry. See [`PreprocessorCacheEntry::result_matches`].
debug!("Preprocessor cache updated because of time macros: {preprocessor_key}");

if let Err(e) = storage
.put_preprocessor_cache_entry(preprocessor_key, preprocessor_cache_entry)
.await
{
debug!("Failed to update preprocessor cache: {}", e);
update_failed = true;
}
}

if !update_failed {
if let Some(key) = hit {
debug!("Preprocessor cache hit: {preprocessor_key}");
// A compiler binary may be a symlink to another and
// so has the same digest, but that means
// the toolchain will not contain the correct path
// to invoke the compiler! Add the compiler
// executable path to try and prevent this
let weak_toolchain_key = format!(
"{}-{}",
self.executable.to_string_lossy(),
self.executable_digest
);
return Ok(HashResult {
key,
compilation: Box::new(CCompilation {
parsed_args: self.parsed_args.clone(),
is_locally_preprocessed: false,
preprocessor_stderr: Vec::new(),
#[cfg(feature = "dist-client")]
preprocessed_input: PREPROCESSING_SKIPPED_COMPILE_POISON.to_vec(),
executable: self.executable.clone(),
compiler: self.compiler.to_owned(),
cwd: cwd.clone(),
env_vars: env_vars.clone(),
}),
weak_toolchain_key,
});
} else {
debug!("Preprocessor cache miss: {preprocessor_key}");
}
let hit = preprocessor_cache_entry
.lookup_result_digest(preprocessor_cache_mode_config, tree_root.as_deref());

if let Some(key) = hit {
debug!("Preprocessor cache hit: {preprocessor_key}");
// A compiler binary may be a symlink to another and
// so has the same digest, but that means
// the toolchain will not contain the correct path
// to invoke the compiler! Add the compiler
// executable path to try and prevent this
let weak_toolchain_key = format!(
"{}-{}",
self.executable.to_string_lossy(),
self.executable_digest
);
return Ok(HashResult {
key,
compilation: Box::new(CCompilation {
parsed_args: self.parsed_args.clone(),
is_locally_preprocessed: false,
preprocessor_stderr: Vec::new(),
#[cfg(feature = "dist-client")]
preprocessed_input: PREPROCESSING_SKIPPED_COMPILE_POISON.to_vec(),
executable: self.executable.clone(),
compiler: self.compiler.to_owned(),
cwd: cwd.clone(),
env_vars: env_vars.clone(),
}),
weak_toolchain_key,
});
} else {
debug!("Preprocessor cache miss: {preprocessor_key}");
}
}

Expand Down Expand Up @@ -1235,6 +1214,11 @@ fn remember_include_file(
return Ok(false);
}

let Some(file_digest) = time_macro_adjusted_digest(file_digest, &finder, &path, meta.modified)
else {
return Ok(false);
};

included_files.insert(path, file_digest);

Ok(true)
Expand Down Expand Up @@ -2093,6 +2077,80 @@ mod test {
t("Cu");
}

#[test]
fn test_remembered_time_macro_header_matches_on_lookup() {
let dir = tempfile::tempdir().unwrap();
let header = dir.path().join("date.h");
std::fs::write(&header, b"static const char *d = __DATE__ __TIMESTAMP__;\n").unwrap();
let config = PreprocessorCacheModeConfig::activated();
let later = std::time::SystemTime::now() + std::time::Duration::from_secs(60);
let mut included_files = HashMap::new();
assert!(
remember_include_file(
header.to_str().unwrap().as_bytes(),
Path::new("input.c"),
dir.path(),
&mut included_files,
&mut Digest::new(),
false,
config,
later,
&StandardFsAbstraction,
)
.unwrap()
);
let mut entry = PreprocessorCacheEntry::new();
let files = included_files
.into_iter()
.map(|(path, digest)| (digest, path));
entry.add_result(later, "result", files, &[], None);
assert_eq!(
entry.lookup_result_digest(config, None).as_deref(),
Some("result")
);
}

#[test]
fn test_timestamp_header_whose_mtime_cannot_be_read_is_not_remembered() {
let dir = tempfile::tempdir().unwrap();
let header = dir.path().join("timestamp.h");
std::fs::write(&header, b"static const char *t = __TIMESTAMP__;\n").unwrap();
let meta = PreprocessorFileMetadata {
is_dir: false,
is_file: true,
modified: None,
ctime_or_creation: None,
};
let fs_impl = TestFs {
metadata_results: Mutex::new([(header.clone(), meta)].into_iter().collect()),
open_results: Mutex::new(
[(
header.clone(),
Box::new(&b"static const char *t = __TIMESTAMP__;\n"[..])
as Box<dyn std::io::Read>,
)]
.into_iter()
.collect(),
),
};
let mut included_files = HashMap::new();
assert!(
!remember_include_file(
header.to_str().unwrap().as_bytes(),
Path::new("input.c"),
dir.path(),
&mut included_files,
&mut Digest::new(),
false,
PreprocessorCacheModeConfig::activated(),
std::time::SystemTime::now(),
&fs_impl,
)
.unwrap()
);
assert!(included_files.is_empty());
}

#[test]
fn test_process_preprocessed_file() {
env_logger::builder()
Expand Down
Loading
Loading