Reuse native checkpoints for unchanged file-size observations

This commit is contained in:
gloamxun committed 2026-11-29 09:24:06 +08:00
1 parent 061e484001
commit 4162c88826
5 files changed
+177 -17

No files matched your search

+61 -6
View File
@@ -305,11 +305,11 @@ impl<'a> CapturedEntries<'a> {
fn capture(&mut self, slots: Capture, entry: Arc<EntryCheckpoint>, host: &RecordingHost) {
let slots = if let Capture::Settled { page, regular } = slots {
if let Some((_, call_count)) = host.prefix() {
if let Some((_, calls)) = host.prefix() {
self.settled.insert(page, SettledCheckpoint {
entry: entry.clone(),
host: host.files().replay_boundary(),
call_count,
call_count: calls.len(),
});
self.changed = true;
}
@@ -318,7 +318,7 @@ impl<'a> CapturedEntries<'a> {
} else {
slots
};
let (Some((trace, replay_count)), Some(path), Some(initial)) = (
let (Some((trace, calls)), Some(path), Some(initial)) = (
host.prefix(),
self.entry_path.as_deref(),
self.initial.as_ref(),
@@ -329,6 +329,7 @@ impl<'a> CapturedEntries<'a> {
self.request,
path,
trace,
calls,
host.generated_outputs(),
initial.clone(),
) else {
@@ -344,7 +345,7 @@ impl<'a> CapturedEntries<'a> {
Arc::new(CachedCheckpoint {
entry,
guard,
replay_count,
replay_count: calls.len(),
slots,
}),
);
@@ -394,11 +395,12 @@ impl<'a> CapturedEntries<'a> {
// Only automatic sessions retain completed jobs, and only when the
// recorder reached actual closeout without filling its call budget.
let completion = refresh_on_resume.then(|| {
let (trace, _) = recording.prefix()?;
let (trace, calls) = recording.prefix()?;
let guard = PrefixInputGuard::capture(
self.request,
self.entry_path.as_deref()?,
trace,
calls,
recording.generated_outputs(),
self.initial?,
).ok()?;
@@ -942,6 +944,7 @@ struct PrefixInputGuard {
entry: PathBuf,
nested_source: Option<NestedSourceGuard>,
immutable: BTreeSet<PathBuf>,
immutable_sizes: BTreeMap<PathBuf, u64>,
immutable_stream_prefixes: BTreeMap<PathBuf, usize>,
generated: BTreeSet<PathBuf>,
lookups: Vec<ResourceLookup>,
@@ -950,6 +953,54 @@ struct PrefixInputGuard {
clock_environment: Option<[Option<OsString>; 3]>,
}
fn size_only_inputs(
project: &ProjectSnapshot,
trace: &ResourceTrace,
calls: &[Call],
) -> BTreeMap<PathBuf, u64> {
#[derive(Default)]
struct Observations {
sizes: usize,
reads: usize,
lookups: usize,
}
let mut counts = BTreeMap::<&Path, Observations>::new();
for (path, size) in calls.iter().filter_map(Call::observed_file_size) {
if project.file(path).is_some_and(|file| file.version.bytes == size) {
counts.entry(path).or_default().sizes += 1;
}
}
if counts.is_empty() {
return BTreeMap::new();
}
// A size call records one complete host read and one successful lookup.
// Any other consumer keeps its own stronger dependency on this path.
for version in &trace.reads {
if let Some(count) = counts.get_mut(version.path.as_path()) {
count.reads += 1;
}
}
for path in trace.lookups.iter().filter_map(|item| item.resolved.as_deref()) {
if let Some(count) = counts.get_mut(path) {
count.lookups += 1;
}
}
for path in trace.stream_reads.iter().map(|read| &read.path)
.chain(trace.file_modifications.iter().map(|read| &read.path))
.chain(trace.generated_lookups.iter().map(|read| &read.target))
.chain(trace.format_input_bindings.values())
{
counts.remove(path.as_path());
}
counts.into_iter()
.filter(|(_, count)| count.sizes == count.reads && count.sizes == count.lookups)
.map(|(path, _)| (path.to_owned(), project.file(path).unwrap().version.bytes))
.collect()
}
fn date_environment() -> [Option<OsString>; 3] {
["TZ", "SOURCE_DATE_EPOCH", "FORCE_SOURCE_DATE"].map(std::env::var_os)
}
@@ -959,6 +1010,7 @@ impl PrefixInputGuard {
request: &CompileRequest,
entry: &Path,
trace: &ResourceTrace,
calls: &[Call],
written: &BTreeSet<PathBuf>,
initial: GeneratedInitialState,
) -> Result<Self, String> {
@@ -1044,6 +1096,7 @@ impl PrefixInputGuard {
let prefix = immutable_stream_prefixes.entry(range.path.clone()).or_default();
*prefix = (*prefix).max(end);
}
let immutable_sizes = size_only_inputs(project, trace, calls);
Ok(Self {
original: AttemptRequest::from(request),
project: project.clone(),
@@ -1051,6 +1104,7 @@ impl PrefixInputGuard {
entry: entry.to_path_buf(),
nested_source: None,
immutable,
immutable_sizes,
immutable_stream_prefixes,
generated,
lookups: trace.lookups.clone(),
@@ -1121,6 +1175,7 @@ impl PrefixInputGuard {
|(old, new)| {
old.version == new.version
|| self.nested_source.as_ref().is_some_and(|source| &source.path == path)
|| self.immutable_sizes.get(path) == Some(&new.version.bytes)
|| self.immutable_stream_prefixes.get(path).is_some_and(|&end| {
new.data.get(..end) == Some(&old.data[..end])
})
@@ -1307,7 +1362,7 @@ fn prefix_guard_checks_actual_dependencies_and_preserves_live_artifacts_on_rejec
.iter()
.any(|lookup| lookup.name == "optional.tex" && lookup.resolved.is_none())
);
let guard = PrefixInputGuard::capture(&request, &entry, &trace, &written, initial).unwrap();
let guard = PrefixInputGuard::capture(&request, &entry, &trace, &calls, &written, initial).unwrap();
assert!(guard.generated.contains(&aux));
assert!(!guard.generated.contains(&seed));
assert!(guard.immutable.contains(&library));
+20 -9
View File
@@ -1,8 +1,8 @@
//! Capture and verify the native host calls made by an entry prefix.
use super::*;
use std::{collections::BTreeSet, fmt::Debug};
use std::{collections::BTreeSet, fmt::Debug, path::Path};
#[cfg(test)]
use std::{fs, path::Path};
use std::fs;
#[cfg(test)]
use texrra_tex_core::source_map::SourceMap;
use texrra_tex_core::{
@@ -36,16 +36,27 @@ pub(super) struct RebindableSourceObservations {
}
impl Call {
pub(super) fn observed_file_size(&self) -> Option<(&Path, u64)> {
let Self::FileSize(read) = self else { return None };
let version = read.read.as_ref()?;
(read.observed == Ok(Some(version.bytes))).then_some((&version.path, version.bytes))
}
pub(super) fn replay(&self, files: &mut Files, partial_source: Option<&ResourceVersion>) -> Result<(), String> {
match self {
Self::Checked(call) => call(files),
Self::FileSize(read) => {
let before = files.resource_trace().reads.len();
let observed = files.file_size(&read.name, &read.from);
if observed == read.observed {
Ok(())
} else {
Err(format!("file size changed: expected {:?}, observed {observed:?}", read.observed))
if observed != read.observed {
return Err(format!("file size changed: expected {:?}, observed {observed:?}", read.observed));
}
if let Some(expected) = &read.read
&& !matches!(&files.resource_trace().reads[before..], [actual] if actual.path == expected.path)
{
return Err(format!("file size lookup changed: {}", expected.path.display()));
}
Ok(())
}
Self::ReadSource(read) => {
let SourceRead { name, from, expected } = read.as_ref();
@@ -104,9 +115,9 @@ impl<'a> RecordingHost<'a> {
Some((self.calls.take()?, trace))
}
pub(super) fn prefix(&self) -> Option<(&ResourceTrace, usize)> {
let count = self.calls.as_ref()?.len();
(count < MAX_REPLAY_CALLS).then(|| (self.files.resource_trace(), count))
pub(super) fn prefix(&self) -> Option<(&ResourceTrace, &[Call])> {
let calls = self.calls.as_deref()?;
(calls.len() < MAX_REPLAY_CALLS).then(|| (self.files.resource_trace(), calls))
}
pub(super) fn rebindable_source_observations(