Reuse verified native suffixes after settled output routines

This commit is contained in:
gloamxun committed 2026-11-29 07:51:37 +08:00
1 parent f387b1db61
commit 0117449023
30 files changed
+791 -77

No files matched your search

@@ -4,7 +4,7 @@
use super::super::boxes::{self, PackSpec, VerticalBuilder};
use super::*;
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Boundary {
Prefix,
Entry,
@@ -0,0 +1,286 @@
use super::*;
impl InputFrame {
fn same_at_shipout(&self, other: &Self) -> bool {
match (self, other) {
(
Self::Source {
source: a,
mouth: am,
eof_seen: ae,
pseudo_trace: at,
group_boundary: ag,
conditional_boundary: ac,
},
Self::Source {
source: b,
mouth: bm,
eof_seen: be,
pseudo_trace: bt,
group_boundary: bg,
conditional_boundary: bc,
},
) => (a, ae, at, ag, ac) == (b, be, bt, bg, bc) && am.same_unread_input(bm),
(
Self::Tokens {
tokens: a,
cursor: ac,
kind: ak,
},
Self::Tokens {
tokens: b,
cursor: bc,
kind: bk,
},
) => (ac, ak) == (bc, bk) && a.iter().eq(b.iter()),
(
Self::Macro {
definition: a,
name: an,
cursor: ac,
arguments: aa,
call: aspan,
},
Self::Macro {
definition: b,
name: bn,
cursor: bc,
arguments: ba,
call: bspan,
},
) => (a, an, ac, aa, aspan) == (b, bn, bc, ba, bspan),
(
Self::Argument {
tokens: a,
cursor: ac,
call: aspan,
},
Self::Argument {
tokens: b,
cursor: bc,
call: bspan,
},
) => (a, ac, aspan) == (b, bc, bspan),
(
Self::Hook {
parameter: a,
tokens: at,
cursor: ac,
},
Self::Hook {
parameter: b,
tokens: bt,
cursor: bc,
},
) => (a, at, ac) == (b, bt, bc),
(Self::Boundary, Self::Boundary) => true,
_ => false,
}
}
}
impl EntryCheckpoint {
/// Construct an execution with the current prefix and a previously
/// completed suffix. The host must still validate and replay that suffix.
pub fn splice_settled_suffix(&self, prior: &Self, completed: &Execution) -> Option<Execution> {
if self.entry != self.active_source
|| prior.entry != prior.active_source
|| self.entry != prior.entry
|| self.initial_depth != prior.initial_depth
|| self.incoming_state != prior.incoming_state
|| self.frontier != prior.frontier
|| self.pages_processed != prior.pages_processed
|| completed.incoming_state != prior.incoming_state
|| completed.reads_event_history
|| completed.format_dumped
|| completed.steps <= prior.completed_steps()
{
return None;
}
let current = &self.executor;
let old = &prior.executor;
if !same_kernel_continuation(current, old) {
return None;
}
if current.sources.len() != old.sources.len()
|| current
.sources
.iter()
.zip(&old.sources)
.enumerate()
.any(|(id, (new, old))| {
if id == self.entry {
new.name != old.name || new.data.len() != old.data.len()
} else {
new != old
}
})
|| completed.sources.get(self.entry)? != &old.sources[self.entry]
{
return None;
}
let mut sources = completed.sources.clone();
sources[self.entry] = current.sources[self.entry].clone();
let tail = completed.events.get(old.events.len()..)?;
let mut events = current.events.clone().into_vec();
events.extend_from_slice(tail);
Some(Execution {
format_dumped: completed.format_dumped,
format_pdf_images: completed.format_pdf_images.clone(),
job_name: completed.job_name.clone(),
interaction_mode: completed.interaction_mode,
native_page: completed.native_page.clone(),
split_discards: completed.split_discards.clone(),
after_assignment: completed.after_assignment.clone(),
events,
state: completed.state.clone(),
io: completed.io.clone(),
input_position: completed.input_position,
paragraph_mode: completed.paragraph_mode,
sources,
stream_sources: completed.stream_sources.clone(),
generated_inputs: completed.generated_inputs.clone(),
reads: completed.reads.clone(),
incoming_state: completed.incoming_state,
outgoing_state: completed.outgoing_state,
steps: completed.steps,
reads_event_history: false,
})
}
}
fn same_kernel_continuation(current: &Executor, prior: &Executor) -> bool {
// Every field belongs to one of these checks. Adding kernel state requires
// an explicit decision here before a later execution may be skipped.
macro_rules! same_values {
([$($value:ident),+ $(,)?], [$($planned:ident),+ $(,)?], [$($runtime:ident),+ $(,)?]) => {{
let Executor {
$($value,)+
$($planned: _,)+
$($runtime: _,)+
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pdf_last_match: _,
} = current;
true $(&& *$value == prior.$value)+
}};
}
if !same_values!(
[
state,
limits,
interaction_mode,
stream_sources,
generated_inputs,
reads,
reads_tracing_macros,
conditionals,
in_csname,
command_trace_mode,
steps,
ligature_carry,
current,
expression_depth,
scanner_depth,
scanner_status,
expansion_depth,
io,
pdf_colors,
xdv_colors,
pdf_running_links,
pdf_interword_spaces,
pdf_space_font,
pdf_loaded_space_font,
pdf_virtual_fonts,
pdf_catalog_tokens,
pdf_outlines,
pdf_outline_payload,
pdf_info_tokens,
restored_pdf_image_count,
format_image_warnings,
pdf_force_pagebox_warned,
input_position,
vertical,
box_nodes_created,
material_charge,
mark_cells_created,
diagnostic_bytes,
after_assignment,
after_group_tokens,
math_tokens_processed,
native_page,
split_discards,
pending_shipouts,
shipped_pages,
job_ended,
fresh_job,
job_name,
format_loaded,
format_dumped,
writing,
stack_peaks,
alignment_balance,
],
[
inputs,
sources,
meaning_reads,
events,
reads_event_history,
xdv_annotations,
xdv_text,
pdf_navigation_geometry,
pdf_link_stack,
pdf_active_thread,
pdf_space_probe,
pdf_images,
paragraph,
box_frames,
math,
display,
output_frame,
alignments,
],
[cancellation, line_break_cache]
) {
return false;
}
let idle = |e: &Executor| {
e.output_frame.is_none()
&& e.paragraph.is_none()
&& e.box_frames.is_empty()
&& e.math.is_empty()
&& e.display.is_none()
&& e.alignments.is_empty()
&& e.pdf_link_stack.is_empty()
&& e.pdf_active_thread.is_none()
&& e.pdf_space_probe.is_none()
&& e.pdf_images.is_empty()
&& !e.reads_event_history
};
if !idle(current) || !idle(prior) {
return false;
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
if current.pdf_last_match.as_deref() != prior.pdf_last_match.as_deref() {
return false;
}
current
.xdv_annotations
.same_idle_state(&prior.xdv_annotations)
&& current.xdv_text.same_state(&prior.xdv_text)
&& current
.pdf_navigation_geometry
.same_state(&prior.pdf_navigation_geometry)
&& current.events.len() == prior.events.len()
&& current.meaning_reads.len() == prior.meaning_reads.len()
&& current
.meaning_reads
.keys()
.all(|name| prior.meaning_reads.contains_key(name))
&& current.inputs.len() == prior.inputs.len()
&& current
.inputs
.iter()
.zip(&prior.inputs)
.all(|(a, b)| a.same_at_shipout(b))
}
@@ -1089,6 +1089,7 @@ impl Executor {
.map_err(|e| self.error(e, self.current))?;
let mut shared = self.pdf_space_probe.take().unwrap_or_default();
let prior = Arc::make_mut(&mut shared);
self.reads_event_history = true;
for event in self.events.iter().skip(prior.observed_events) {
let Event::Shipout(shipped) = event else { continue };
self.check_cancellation()?;
@@ -17,6 +17,13 @@ pub(super) struct NavigationGeometry {
last_pdf_rect: [i64; 4],
}
impl NavigationGeometry {
pub(super) fn same_state(&self, other: &Self) -> bool {
self.last_pdf_rect == other.last_pdf_rect
&& self.matrix.map(|m| m.0.map(f64::to_bits)) == other.matrix.map(|m| m.0.map(f64::to_bits))
}
}
#[derive(Clone)]
pub(super) struct ActiveLink {
layout: LinkStart,
@@ -4,6 +4,7 @@ use std::mem::MaybeUninit;
use std::ops::Range;
use std::ptr;
#[derive(PartialEq, Eq)]
pub(super) struct PdfMatch {
input: Vec<u8>,
captures: Vec<Option<Range<usize>>>,
@@ -25,6 +25,7 @@ impl Executor {
}
// Packing can move existing lists or replace an old register. Recount
// actual storage here, counting each shared list allocation once.
self.reads_event_history = true;
let usage = self.collect_usage(shipping, None, Collector::for_capacity());
self.material_charge = usage
.active_material
@@ -36,10 +37,11 @@ impl Executor {
Ok(())
}
fn resource_usage(
&self,
&mut self,
shipping: Option<&NativeBox>,
host: Option<HostResourceUsage>,
) -> ResourceUsage {
self.reads_event_history = true;
self.collect_usage(shipping, host, Collector::default())
}