Reuse settled suffixes after verified closed child edits

This commit is contained in:
gloamxun committed 2026-12-02 01:53:20 +08:00
1 parent 55c497dccd
commit e4d6e049f8
11 files changed
+565 -72

No files matched your search

+82
View File
@@ -42,6 +42,75 @@ pub(super) struct RebindableSourceObservations {
pub(super) size: Option<u64>,
}
#[derive(Clone, Copy)]
pub(super) struct ClosedSourceUsageOffset {
previous: u64,
current: u64,
}
impl ClosedSourceUsageOffset {
pub(super) fn new(previous: u64, current: u64, reads: usize) -> Option<Self> {
let reads = u64::try_from(reads).ok()?;
Some(Self {
previous: previous.checked_mul(reads)?,
current: current.checked_mul(reads)?,
})
}
}
/// Return the entire byte-read count of a closed immutable child, including
/// typed size queries. Every other observation of the child rules out reuse.
pub(super) fn closed_source_reads(
version: &ResourceVersion,
trace: &ResourceTrace,
calls: &[Call],
) -> Option<usize> {
let path = &version.path;
if trace.stream_reads.iter().any(|read| &read.path == path)
|| trace.file_modifications.iter().any(|item| &item.path == path)
|| trace.generated_lookups.iter().any(|lookup| &lookup.target == path)
|| trace.format_input_bindings.iter().any(|(old, current)| old == path || current == path)
|| trace.read_errors.iter().any(|(read, _)| read == path)
|| !trace.namespace_capture_failures.is_empty()
{
return None;
}
let mut sources = 0usize;
let mut sizes = 0usize;
for call in calls {
match call {
Call::ReadSource(source) => {
if let Ok(read) = &source.expected
&& &read.path == path
{
if read != version {
return None;
}
sources += 1;
}
}
Call::FileSize(size) if size.read.as_ref().is_some_and(|read| &read.path == path) => {
// A size query can have been replayed from an equal-sized
// earlier source. Its exact path and numeric result are
// evidence, while the actual trace authenticates the bytes.
if call.observed_file_size() != Some((path.as_path(), version.bytes)) {
return None;
}
sizes += 1;
}
_ => {}
}
}
let count = sources.checked_add(sizes)?;
let mut actual = trace.reads.iter().filter(|read| &read.path == path);
if sources != 1 || actual.clone().count() != count || !actual.all(|read| read == version) {
return None;
}
let mut lookups = trace.lookups.iter().filter(|lookup| lookup.resolved.as_ref() == Some(path));
(lookups.clone().count() == count && lookups.all(|item| item.kind == ResourceKind::Tex))
.then_some(count)
}
impl Call {
pub(super) fn observed_file_size(&self) -> Option<(&Path, u64)> {
let Self::FileSize(read) = self else { return None };
@@ -178,6 +247,7 @@ impl<'a> RecordingHost<'a> {
&mut self,
calls: &[Call],
end_statistics: Option<&StatisticsReport>,
closed_source: Option<ClosedSourceUsageOffset>,
cancellation: &Cancellation,
) -> Result<(), String> {
let recorded = self.calls.as_mut().ok_or("host calls are unavailable")?;
@@ -196,6 +266,18 @@ impl<'a> RecordingHost<'a> {
Call::EndStatistics(Arc::new(translated.clone()))
}
}
Call::ResourceUsage(read) if closed_source.is_some_and(|bytes| bytes.previous != bytes.current) => {
let bytes = closed_source.expect("checked closed source adjustment");
let mut observed = read.observed.clone();
if let Some(usage) = &mut observed {
usage.rebind_entry_bytes(bytes.previous, bytes.current)
.ok_or("invalid replayed child resource usage")?;
}
Call::ResourceUsage(Arc::new(ResourceUsageRead {
observed,
entry_bytes: read.entry_bytes,
}))
}
call => call.clone(),
};
call.replay(self.files, None)?;