Reuse native checkpoints for unread stream changes

This commit is contained in:
gloamxun committed 2026-11-29 01:54:35 +08:00
1 parent 6a47d450f3
commit e3be6dcd09
3 files changed
+142 -13

No files matched your search

+51 -13
View File
@@ -2,15 +2,15 @@
use super::checkpoint_probe::{Call, RecordingHost};
use super::*;
#[cfg(test)]
use std::{collections::BTreeMap, fs};
use std::fs;
use std::{
collections::{BTreeSet, VecDeque},
collections::{BTreeMap, BTreeSet, VecDeque},
ffi::OsString,
path::Path,
};
use texrra_tex_core::{
execution::EntryCheckpoint,
resources::{ResourceKind, ResourceLookup},
resources::{ResourceKind, ResourceLookup, ResourceReadRange},
};
#[cfg(test)]
use texrra_tex_core::{execution::HostResourceUsage, resources::InputPolicy};
@@ -463,13 +463,7 @@ impl PublishedCheckpoints {
continue 'retry;
}
}
if !checkpoint
.guard
.project
.immutable_paths()
.eq(project.immutable_paths())
&& let Err(error) = checkpoint.guard.verify_replayed_lookups(&prepared.files)
{
if let Err(error) = checkpoint.guard.verify_replayed_inputs(&prepared.files, project) {
replay_rejection.get_or_insert(error);
continue;
}
@@ -644,8 +638,10 @@ struct PrefixInputGuard {
initial: GeneratedInitialState,
entry: PathBuf,
immutable: BTreeSet<PathBuf>,
immutable_stream_prefixes: BTreeMap<PathBuf, usize>,
generated: BTreeSet<PathBuf>,
lookups: Vec<ResourceLookup>,
stream_reads: Vec<ResourceReadRange>,
format_input_bindings: BTreeMap<PathBuf, PathBuf>,
clock_environment: Option<[Option<OsString>; 3]>,
}
@@ -720,14 +716,40 @@ impl PrefixInputGuard {
}
immutable.insert(observed.path.clone());
}
// A stream can have unconsumed bytes even though the host pinned a
// complete buffer. Whole-file consumers, dates and observed EOF
// still require the immutable input version.
let whole_files: BTreeSet<_> = trace
.reads
.iter()
.map(|version| version.path.as_path())
.chain(trace.file_modifications.iter().map(|item| item.path.as_path()))
.chain(trace.stream_reads.iter().filter(|item| item.eof).map(|item| item.path.as_path()))
.collect();
let mut immutable_stream_prefixes = BTreeMap::<PathBuf, usize>::new();
for range in &trace.stream_reads {
if !immutable.contains(&range.path) || whole_files.contains(range.path.as_path()) {
continue;
}
let end = range
.offset
.checked_add(range.bytes)
.and_then(|end| usize::try_from(end).ok())
.filter(|&end| project.file(&range.path).is_some_and(|file| end <= file.data.len()))
.ok_or("checkpoint stream range exceeds its captured input")?;
let prefix = immutable_stream_prefixes.entry(range.path.clone()).or_default();
*prefix = (*prefix).max(end);
}
Ok(Self {
original: AttemptRequest::from(request),
project: project.clone(),
initial,
entry: entry.to_path_buf(),
immutable,
immutable_stream_prefixes,
generated,
lookups: trace.lookups.clone(),
stream_reads: trace.stream_reads.clone(),
format_input_bindings: trace.format_input_bindings.clone(),
clock_environment: (!trace.file_modifications.is_empty()).then(date_environment),
})
@@ -759,9 +781,15 @@ impl PrefixInputGuard {
return Err("checkpoint lookup configuration changed".into());
}
for path in &self.immutable {
let old = self.project.file(path).map(|file| &file.version);
let new = current.file(path).map(|file| &file.version);
if old != new || self.project.modification_time(path) != current.modification_time(path)
let same_bytes = self.project.file(path).zip(current.file(path)).is_some_and(
|(old, new)| {
old.version == new.version
|| self.immutable_stream_prefixes.get(path).is_some_and(|&end| {
new.data.get(..end) == Some(&old.data[..end])
})
},
);
if !same_bytes || self.project.modification_time(path) != current.modification_time(path)
{
return Err(format!(
"checkpoint observed input changed: {}",
@@ -779,6 +807,16 @@ impl PrefixInputGuard {
Ok(current)
}
fn verify_replayed_inputs(&self, files: &Files, current: &ProjectSnapshot) -> Result<(), CompileError> {
if files.resource_trace().stream_reads != self.stream_reads {
return Err(CompileError::Host("checkpoint replayed stream reads changed".into()));
}
if !self.project.immutable_paths().eq(current.immutable_paths()) {
self.verify_replayed_lookups(files)?;
}
Ok(())
}
fn verify_replayed_lookups(&self, files: &Files) -> Result<(), CompileError> {
let trace = files.resource_trace();
if trace.lookups != self.lookups