Reuse verified native suffixes after settled output routines
This commit is contained in:
1 parent
f387b1db61
commit
0117449023
30 files changed
+791
-77
No files matched your search
@@ -39,6 +39,7 @@ mod scanning;
|
||||
mod scheduling;
|
||||
mod specials;
|
||||
mod statistics;
|
||||
mod continuation;
|
||||
mod xdv_annotation;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
@@ -57,7 +58,7 @@ impl Default for Limits {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
struct InputToken {
|
||||
token: Token,
|
||||
frozen: bool,
|
||||
@@ -131,7 +132,7 @@ enum InputFrame {
|
||||
},
|
||||
Boundary,
|
||||
}
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum TokenInputKind {
|
||||
Inserted,
|
||||
BackedUp,
|
||||
@@ -144,7 +145,7 @@ enum TheValue {
|
||||
Tokens(Vec<Token>),
|
||||
Text(std::string::String),
|
||||
}
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
struct Conditional {
|
||||
primitive: Primitive,
|
||||
inverted: bool,
|
||||
@@ -241,6 +242,8 @@ pub struct Executor {
|
||||
in_csname: bool,
|
||||
command_trace_mode: Option<&'static str>,
|
||||
events: ExecutionEvents,
|
||||
// Set before operations that inspect earlier events beyond their count.
|
||||
reads_event_history: bool,
|
||||
steps: usize,
|
||||
ligature_carry: super::ligature::Carry,
|
||||
current: Span,
|
||||
@@ -314,6 +317,15 @@ pub struct CheckpointSource<'a> {
|
||||
pub bytes: usize,
|
||||
pub frontier: usize,
|
||||
pub is_entry: bool,
|
||||
pub output_returned: bool,
|
||||
}
|
||||
|
||||
/// Called only at a selected boundary. Reuse requires that the caller has
|
||||
/// validated and replayed the previous suffix into this revision's draft.
|
||||
pub enum CheckpointDecision {
|
||||
Continue,
|
||||
Reuse(Box<Execution>),
|
||||
ReplayFailed(String),
|
||||
}
|
||||
|
||||
/// A live, in-memory kernel continuation at an entry-file command boundary.
|
||||
@@ -387,7 +399,9 @@ impl EntryCheckpoint {
|
||||
files: &mut impl SourceProvider,
|
||||
cancellation: Cancellation,
|
||||
) -> Option<Result<Execution, ExecutionFailure>> {
|
||||
self.resume_observed(source, None, files, cancellation, None, None, |_, _, _, _| {})
|
||||
self.resume_observed(source, None, files, cancellation, None, None,
|
||||
|_, _, _, _| CheckpointDecision::Continue)
|
||||
.expect("ordinary resume cannot reject a cached suffix")
|
||||
}
|
||||
|
||||
pub(super) fn resume_capturing_sources_after_host_replay<F: SourceProvider, S>(
|
||||
@@ -399,7 +413,7 @@ impl EntryCheckpoint {
|
||||
line_break_cache: std::sync::Arc<super::LineBreakCache>,
|
||||
previous: Option<&Execution>,
|
||||
select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
|
||||
capture: impl FnMut(S, EntryCheckpoint, &mut F),
|
||||
mut capture: impl FnMut(S, EntryCheckpoint, &mut F),
|
||||
) -> Option<Result<Execution, ExecutionFailure>> {
|
||||
self.resume_observed(
|
||||
source,
|
||||
@@ -408,8 +422,27 @@ impl EntryCheckpoint {
|
||||
cancellation,
|
||||
Some(line_break_cache),
|
||||
previous,
|
||||
Executor::entry_observer(self.incoming_state, select, capture),
|
||||
Executor::entry_observer(self.incoming_state, select, |slot, entry, files| {
|
||||
capture(slot, entry, files);
|
||||
CheckpointDecision::Continue
|
||||
}),
|
||||
)
|
||||
.expect("ordinary resume cannot reject a cached suffix")
|
||||
}
|
||||
|
||||
pub(super) fn resume_deciding_suffix<F: SourceProvider, S>(
|
||||
&self,
|
||||
source: SourceFile,
|
||||
nested: Option<SourceFile>,
|
||||
files: &mut F,
|
||||
cancellation: Cancellation,
|
||||
line_break_cache: Arc<super::LineBreakCache>,
|
||||
previous: Option<&Execution>,
|
||||
select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
|
||||
capture: impl FnMut(S, EntryCheckpoint, &mut F) -> CheckpointDecision,
|
||||
) -> Result<Option<Result<Execution, ExecutionFailure>>, String> {
|
||||
self.resume_observed(source, nested, files, cancellation, Some(line_break_cache), previous,
|
||||
Executor::entry_observer(self.incoming_state, select, capture))
|
||||
}
|
||||
|
||||
fn resume_observed<F: SourceProvider>(
|
||||
@@ -420,15 +453,15 @@ impl EntryCheckpoint {
|
||||
cancellation: Cancellation,
|
||||
line_break_cache: Option<std::sync::Arc<super::LineBreakCache>>,
|
||||
previous: Option<&Execution>,
|
||||
capture_boundary: impl FnMut(&mut Executor, usize, bool, &mut F),
|
||||
) -> Option<Result<Execution, ExecutionFailure>> {
|
||||
capture_boundary: impl FnMut(&mut Executor, usize, bool, &mut F) -> CheckpointDecision,
|
||||
) -> Result<Option<Result<Execution, ExecutionFailure>>, String> {
|
||||
if !self.accepts_entry(&source)
|
||||
|| match &nested {
|
||||
Some(nested) => !self.accepts_nested_source(&nested.name, &nested.data),
|
||||
None => self.nested_source().is_some(),
|
||||
}
|
||||
{
|
||||
return None;
|
||||
return Ok(None);
|
||||
}
|
||||
let mut executor = self.executor.clone().with_cancellation(cancellation);
|
||||
executor.line_break_cache = line_break_cache;
|
||||
@@ -446,8 +479,16 @@ impl EntryCheckpoint {
|
||||
capture_boundary,
|
||||
)
|
||||
});
|
||||
let result = match result {
|
||||
Ok(CheckpointDecision::Reuse(execution)) => {
|
||||
return Ok(Some(Ok(*execution)));
|
||||
}
|
||||
Ok(CheckpointDecision::ReplayFailed(error)) => return Err(error),
|
||||
Ok(CheckpointDecision::Continue) => Ok(()),
|
||||
Err(error) => Err(error),
|
||||
};
|
||||
let result = executor.finish_observed_input(result, files);
|
||||
Some(executor.finish_execution(result, self.incoming_state, previous))
|
||||
Ok(Some(executor.finish_execution(result, self.incoming_state, previous)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -496,6 +537,7 @@ impl Executor {
|
||||
in_csname: false,
|
||||
command_trace_mode: None,
|
||||
events: ExecutionEvents::default(),
|
||||
reads_event_history: false,
|
||||
steps: 0,
|
||||
ligature_carry: super::ligature::Carry::default(),
|
||||
current: Span::default(),
|
||||
@@ -3491,6 +3533,7 @@ impl Executor {
|
||||
reads,
|
||||
events: self.events.into_vec(),
|
||||
steps: self.steps,
|
||||
reads_event_history: self.reads_event_history,
|
||||
})
|
||||
}
|
||||
fn check_input_character(&self, code: i32) -> Result<(), Error> {
|
||||
@@ -3785,7 +3828,7 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<Execution, ExecutionFailure> {
|
||||
let incoming_state = self.state.fingerprint();
|
||||
let result = self.run_observed(file, files, |_, _, _, _| {});
|
||||
let result = self.run_observed(file, files, |_, _, _, _| CheckpointDecision::Continue);
|
||||
self.finish_execution(result, incoming_state, None)
|
||||
}
|
||||
/// Capture the first eligible entry command boundary at or beyond the
|
||||
@@ -3841,13 +3884,16 @@ impl Executor {
|
||||
file: SourceFile,
|
||||
files: &mut F,
|
||||
select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
|
||||
capture: impl FnMut(S, EntryCheckpoint, &mut F),
|
||||
mut capture: impl FnMut(S, EntryCheckpoint, &mut F),
|
||||
) -> Result<Execution, ExecutionFailure> {
|
||||
let incoming_state = self.state.fingerprint();
|
||||
let result = self.run_observed(
|
||||
file,
|
||||
files,
|
||||
Self::entry_observer(incoming_state, select, capture),
|
||||
Self::entry_observer(incoming_state, select, |slot, entry, files| {
|
||||
capture(slot, entry, files);
|
||||
CheckpointDecision::Continue
|
||||
}),
|
||||
);
|
||||
self.finish_execution(result, incoming_state, None)
|
||||
}
|
||||
@@ -3855,17 +3901,17 @@ impl Executor {
|
||||
fn entry_observer<F: SourceProvider, S>(
|
||||
incoming_state: u64,
|
||||
mut select: impl FnMut(CheckpointSource<'_>, usize) -> Option<S>,
|
||||
mut capture: impl FnMut(S, EntryCheckpoint, &mut F),
|
||||
) -> impl FnMut(&mut Self, usize, bool, &mut F) {
|
||||
mut capture: impl FnMut(S, EntryCheckpoint, &mut F) -> CheckpointDecision,
|
||||
) -> impl FnMut(&mut Self, usize, bool, &mut F) -> CheckpointDecision {
|
||||
move |engine, initial_depth, pages_processed, files| {
|
||||
if engine.job_ended {
|
||||
return;
|
||||
return CheckpointDecision::Continue;
|
||||
}
|
||||
let Some(InputFrame::Source {
|
||||
source: entry, eof_seen: false, pseudo_trace: None, ..
|
||||
}) = engine.inputs.first()
|
||||
else {
|
||||
return;
|
||||
return CheckpointDecision::Continue;
|
||||
};
|
||||
// Retain the active scanner plus its suspended parents. A pseudo
|
||||
// input or an EOF hook has no replaceable physical byte prefix.
|
||||
@@ -3873,10 +3919,10 @@ impl Executor {
|
||||
source, mouth, eof_seen: false, pseudo_trace: None, ..
|
||||
}) = engine.inputs.iter().rev().find(|frame| matches!(frame, InputFrame::Source { .. }))
|
||||
else {
|
||||
return;
|
||||
return CheckpointDecision::Continue;
|
||||
};
|
||||
let Some(frontier) = mouth.physical_frontier() else {
|
||||
return;
|
||||
return CheckpointDecision::Continue;
|
||||
};
|
||||
let (entry, active_source) = (*entry, *source);
|
||||
let file = &engine.sources[active_source];
|
||||
@@ -3886,14 +3932,15 @@ impl Executor {
|
||||
bytes: file.data.len(),
|
||||
frontier,
|
||||
is_entry: active_source == entry,
|
||||
output_returned: engine.output_frame.is_none(),
|
||||
};
|
||||
let Some(slot) = select(position, engine.shipped_pages) else {
|
||||
return;
|
||||
return CheckpointDecision::Continue;
|
||||
};
|
||||
if engine.sources.iter().enumerate().any(|(index, other)| {
|
||||
index != active_source && other.name == file.name
|
||||
}) {
|
||||
return;
|
||||
return CheckpointDecision::Continue;
|
||||
}
|
||||
engine.events.seal_prefix();
|
||||
let mut executor = engine.clone();
|
||||
@@ -3908,7 +3955,7 @@ impl Executor {
|
||||
frontier,
|
||||
pages_processed,
|
||||
};
|
||||
capture(slot, checkpoint, files);
|
||||
capture(slot, checkpoint, files)
|
||||
}
|
||||
}
|
||||
fn finish_execution(
|
||||
@@ -3989,19 +4036,22 @@ impl Executor {
|
||||
reads,
|
||||
incoming_state,
|
||||
steps: self.steps,
|
||||
reads_event_history: self.reads_event_history,
|
||||
})
|
||||
}
|
||||
fn run_observed<F: SourceProvider>(
|
||||
&mut self,
|
||||
file: SourceFile,
|
||||
files: &mut F,
|
||||
capture_boundary: impl FnMut(&mut Self, usize, bool, &mut F),
|
||||
capture_boundary: impl FnMut(&mut Self, usize, bool, &mut F) -> CheckpointDecision,
|
||||
) -> Result<(), Error> {
|
||||
self.check_cancellation()?;
|
||||
self.change_interaction_mode(self.interaction_mode, files)?;
|
||||
let result = self.prepare_input(file, files).and_then(|initial_depth| {
|
||||
self.emit_format_image_warnings(files)?;
|
||||
self.run_active_input(initial_depth, false, files, capture_boundary)
|
||||
self.run_active_input(initial_depth, false, files, capture_boundary).map(|action| {
|
||||
debug_assert!(matches!(action, CheckpointDecision::Continue));
|
||||
})
|
||||
});
|
||||
self.finish_observed_input(result, files)
|
||||
}
|
||||
@@ -4116,20 +4166,26 @@ impl Executor {
|
||||
initial_depth: usize,
|
||||
mut pages_processed: bool,
|
||||
files: &mut F,
|
||||
mut capture_boundary: impl FnMut(&mut Self, usize, bool, &mut F),
|
||||
) -> Result<(), Error> {
|
||||
mut capture_boundary: impl FnMut(&mut Self, usize, bool, &mut F) -> CheckpointDecision,
|
||||
) -> Result<CheckpointDecision, Error> {
|
||||
let xe = self.state.input_profile() == InputProfile::XeTeX;
|
||||
loop {
|
||||
if !std::mem::take(&mut pages_processed) {
|
||||
self.check_box_capacity(None)?;
|
||||
capture_boundary(self, initial_depth, false, files);
|
||||
match capture_boundary(self, initial_depth, false, files) {
|
||||
CheckpointDecision::Continue => {}
|
||||
decision => return Ok(decision),
|
||||
}
|
||||
let shipped_pages = self.shipped_pages;
|
||||
self.process_native_pages(files)?;
|
||||
self.check_box_capacity(None)?;
|
||||
if self.shipped_pages != shipped_pages {
|
||||
// A following expandable command can read an input before
|
||||
// the next main-loop iteration.
|
||||
capture_boundary(self, initial_depth, true, files);
|
||||
match capture_boundary(self, initial_depth, true, files) {
|
||||
CheckpointDecision::Continue => {}
|
||||
decision => return Ok(decision),
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some((input, meaning)) = self.expanded_with_meaning(files, false)? else {
|
||||
@@ -4611,7 +4667,7 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(CheckpointDecision::Continue)
|
||||
}
|
||||
fn assignment(
|
||||
&mut self,
|
||||
|
||||
Reference in new issue
Block a user