Relocate source provenance when native suffixes move

This commit is contained in:
gloamxun committed 2026-12-01 22:17:10 +08:00
1 parent 6acd82dbfa
commit 41a459b4ca
16 files changed
+1507 -79

No files matched your search

@@ -1,9 +1,11 @@
use super::super::source_relocation::SourceRelocation;
use super::*;
mod material;
mod relocate;
use material::MaterialReuse;
impl InputFrame {
fn same_at_shipout(&self, other: &Self) -> bool {
fn same_at_shipout(&self, other: &Self, relocation: Option<&SourceRelocation>) -> bool {
match (self, other) {
(
Self::Source {
@@ -22,7 +24,11 @@ impl InputFrame {
group_boundary: bg,
conditional_boundary: bc,
},
) => (a, ae, at, ag, ac) == (b, be, bt, bg, bc) && am.same_unread_input(bm),
) => (a, ae, at, ag, ac) == (b, be, bt, bg, bc)
&& match relocation.filter(|map| map.source() == *a) {
Some(map) => am.same_unread_input_relocated(bm, map),
None => am.same_unread_input(bm),
},
(
Self::Tokens {
tokens: a,
@@ -90,7 +96,6 @@ impl EntryCheckpoint {
|| 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
@@ -101,9 +106,6 @@ impl EntryCheckpoint {
}
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
@@ -112,7 +114,7 @@ impl EntryCheckpoint {
.enumerate()
.any(|(id, (new, old))| {
if id == self.entry {
new.name != old.name || new.data.len() != old.data.len()
new.name != old.name
} else {
new != old
}
@@ -121,6 +123,24 @@ impl EntryCheckpoint {
{
return None;
}
let previous_bytes = &old.sources[self.entry].data;
let current_bytes = &current.sources[self.entry].data;
let entry_sizes = (previous_bytes.len() != current_bytes.len())
.then_some((previous_bytes.len(), current_bytes.len()));
let relocation = match entry_sizes {
Some(_) => {
let map = SourceRelocation::new(self.entry, previous_bytes, current_bytes)?;
if map.relocate_offset(prior.frontier) != Some(self.frontier) {
return None;
}
Some(map)
}
None if self.frontier == prior.frontier => None,
None => return None,
};
if !same_kernel_continuation(current, old, relocation.as_ref()) {
return None;
}
let offset = StepOffset::new(old.steps, current.steps);
let steps = offset.apply(completed.steps)?;
// Kernel work only advances the step counter. A successful suffix
@@ -131,20 +151,14 @@ impl EntryCheckpoint {
let mut sources = completed.sources.clone();
sources[self.entry] = current.sources[self.entry].clone();
let tail = completed.events.get(old.events.len()..)?;
if completed.accounts_event_history && !current.equivalent_accounting_history(old) {
if completed.accounts_event_history
&& !current.equivalent_accounting_history_with_entry_sizes(old, entry_sizes)
{
return None;
}
// The joined events are published results. Resumption always starts
// from a live checkpoint, whose original shared allocations stay intact.
let mut events = current.events.clone().into_vec();
if offset.is_identity() {
events.extend_from_slice(tail);
} else {
for event in tail {
events.push(offset_event_steps(event, offset)?);
}
}
Some(Execution {
let mut execution = Execution {
format_dumped: completed.format_dumped,
format_pdf_images: completed.format_pdf_images.clone(),
job_name: completed.job_name.clone(),
@@ -152,7 +166,7 @@ impl EntryCheckpoint {
native_page: completed.native_page.clone(),
split_discards: completed.split_discards.clone(),
after_assignment: completed.after_assignment.clone(),
events,
events: current.events.clone().into_vec(),
state: completed.state.clone(),
io: completed.io.clone(),
input_position: completed.input_position,
@@ -166,7 +180,17 @@ impl EntryCheckpoint {
steps,
reads_event_history: false,
accounts_event_history: completed.accounts_event_history,
})
};
if let Some((map, sizes)) = relocation.as_ref().zip(entry_sizes) {
relocate::append_suffix(&mut execution, tail, offset, map, sizes, old.sources.len())?;
} else if offset.is_identity() {
execution.events.extend_from_slice(tail);
} else {
for event in tail {
execution.events.push(offset_event_steps(event, offset)?);
}
}
Some(execution)
}
/// Carry later checkpoints from the suffix accepted by
/// splice_settled_suffix into this revision. The host must still replay
@@ -176,6 +200,12 @@ impl EntryCheckpoint {
prior: &'a Self,
completed: &Execution,
) -> Option<impl FnMut(&Self) -> Option<Self> + 'a> {
// A carried executor still contains scanner positions from the old source.
if self.executor.sources[self.entry].data.len()
!= prior.executor.sources[prior.entry].data.len()
{
return None;
}
// A restored checkpoint may later be asked to account for its
// history, even if the job that furnished the suffix did not.
if !completed.accounts_event_history
@@ -306,7 +336,11 @@ fn is_idle_continuation(e: &Executor) -> bool {
&& !e.reads_event_history
}
fn same_kernel_continuation(current: &Executor, prior: &Executor) -> bool {
fn same_kernel_continuation(
current: &Executor,
prior: &Executor,
relocation: Option<&SourceRelocation>,
) -> 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 {
@@ -321,20 +355,19 @@ fn same_kernel_continuation(current: &Executor, prior: &Executor) -> bool {
true $(&& *$value == prior.$value)+
}};
}
if !is_idle_continuation(current) || !is_idle_continuation(prior) {
return false;
}
if !same_values!(
[
state,
limits,
interaction_mode,
stream_sources,
generated_inputs,
reads,
reads_tracing_macros,
conditionals,
in_csname,
command_trace_mode,
ligature_carry,
current,
expression_depth,
scanner_depth,
scanner_status,
@@ -347,25 +380,17 @@ fn same_kernel_continuation(current: &Executor, prior: &Executor) -> bool {
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,
@@ -378,6 +403,18 @@ fn same_kernel_continuation(current: &Executor, prior: &Executor) -> bool {
accounts_event_history,
],
[
state,
generated_inputs,
conditionals,
current,
pdf_catalog_tokens,
pdf_outlines,
pdf_info_tokens,
vertical,
after_assignment,
native_page,
split_discards,
pending_shipouts,
inputs,
sources,
steps,
@@ -402,30 +439,49 @@ fn same_kernel_continuation(current: &Executor, prior: &Executor) -> bool {
) {
return false;
}
if !is_idle_continuation(current) || !is_idle_continuation(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)
if 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()
{
return false;
}
let relocated;
let prior = if let Some(map) = relocation {
relocated = match relocate::kernel_continuation(prior, map) {
Some(executor) => executor,
None => return false,
};
&relocated
} else {
prior
};
current.state == prior.state
&& current.generated_inputs == prior.generated_inputs
&& current.conditionals == prior.conditionals
&& current.current == prior.current
&& current.pdf_catalog_tokens == prior.pdf_catalog_tokens
&& current.pdf_outlines == prior.pdf_outlines
&& current.pdf_info_tokens == prior.pdf_info_tokens
&& current.vertical == prior.vertical
&& current.after_assignment == prior.after_assignment
&& current.native_page == prior.native_page
&& current.split_discards == prior.split_discards
&& current.pending_shipouts == prior.pending_shipouts
&& 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))
.all(|(a, b)| a.same_at_shipout(b, relocation))
}
@@ -0,0 +1,148 @@
use super::*;
use crate::execution::{
source_relocation_events::EventRelocation, state::source_relocation::relocate_macro,
};
pub(super) fn kernel_continuation(prior: &Executor, map: &SourceRelocation) -> Option<Executor> {
let mut prior = prior.clone();
let mut relocation = EventRelocation::new(map);
prior.current = map.relocate_span(prior.current)?;
prior.state = prior.state.relocate_source(&mut relocation)?;
relocate_generated(&mut prior.generated_inputs, map)?;
for conditional in &mut prior.conditionals {
conditional.span = map.relocate_span(conditional.span)?;
}
let mut macros = std::collections::HashMap::new();
for frame in &mut prior.inputs {
match frame {
InputFrame::Tokens { tokens, .. } => match tokens {
InputTokens::One(token) => token.token = relocation.relocate_token(&token.token)?,
InputTokens::Many(tokens) => {
for token in tokens {
token.token = relocation.relocate_token(&token.token)?;
}
}
},
InputFrame::Macro {
definition,
arguments,
call,
..
} => {
*definition = relocate_macro(definition, &mut relocation, &mut macros)?;
for tokens in arguments {
*tokens = relocation.relocate_tokens(tokens)?;
}
*call = map.relocate_span(*call)?;
}
InputFrame::Argument { tokens, call, .. } => {
*tokens = relocation.relocate_tokens(tokens)?;
*call = map.relocate_span(*call)?;
}
InputFrame::Hook { tokens, .. } => *tokens = relocation.relocate_tokens(tokens)?,
// Keep the old raw scanners for the byte and decoder comparison.
InputFrame::Source { .. } | InputFrame::Boundary => {}
}
}
relocate_tokens(&mut prior.pdf_catalog_tokens, &mut relocation)?;
relocate_tokens(&mut prior.pdf_info_tokens, &mut relocation)?;
for outline in &mut prior.pdf_outlines {
relocate_tokens(&mut outline.attributes, &mut relocation)?;
}
if let Some(token) = &mut prior.after_assignment {
*token = relocation.relocate_token(token)?;
}
relocate_vertical(&mut prior.vertical.nodes, &mut relocation)?;
relocate_vertical(&mut prior.split_discards, &mut relocation)?;
relocate_page(&mut prior.native_page, &mut relocation)?;
for value in &mut prior.pending_shipouts {
*value = relocation.relocate_box(value)?;
}
Some(prior)
}
pub(super) fn append_suffix(
execution: &mut Execution,
tail: &[Event],
offset: StepOffset,
map: &SourceRelocation,
(previous_bytes, current_bytes): (usize, usize),
source_count: usize,
) -> Option<()> {
let mut relocation = EventRelocation::new(map);
for event in tail {
if let Event::Statistics(report) = event {
if let StatisticsPhase::Shipout { before, after } = &report.phase {
// The active entry was the only source frame at convergence.
// If a report follows any later input open, its mouth payload
// alone cannot prove whether that frame is still present.
if before.source_files != source_count || after.source_files != source_count {
return None;
}
}
let mut report = report
.with_step_offset(offset)?
.with_suffix_entry_resize(previous_bytes, current_bytes)?;
report.span = map.relocate_span(report.span)?;
execution.events.push(Event::Statistics(Box::new(report)));
} else {
execution.events.push(relocation.relocate_event(event)?);
}
}
let state = execution.state.relocate_source(&mut relocation)?;
if state != execution.state {
execution.outgoing_state = state.fingerprint();
}
execution.state = state;
relocate_page(&mut execution.native_page, &mut relocation)?;
relocate_vertical(&mut execution.split_discards, &mut relocation)?;
if let Some(token) = &mut execution.after_assignment {
*token = relocation.relocate_token(token)?;
}
relocate_generated(&mut execution.generated_inputs, map)?;
for image in &mut execution.format_pdf_images {
relocate_tokens(&mut image.attributes, &mut relocation)?;
}
Some(())
}
fn relocate_generated(
generated: &mut BTreeMap<usize, GeneratedInput>,
map: &SourceRelocation,
) -> Option<()> {
for input in generated.values_mut() {
input.origin = map.relocate_span(input.origin)?;
}
Some(())
}
fn relocate_page(
page: &mut Option<NativePageState>,
relocation: &mut EventRelocation<'_>,
) -> Option<()> {
if let Some(page) = page {
relocate_vertical(&mut page.nodes, relocation)?;
relocate_vertical(page.contributions.make_contiguous(), relocation)?;
relocate_vertical(&mut page.discards, relocation)?;
relocate_vertical(&mut page.insertions.held, relocation)?;
}
Some(())
}
fn relocate_tokens(tokens: &mut [Token], relocation: &mut EventRelocation<'_>) -> Option<()> {
for token in tokens {
*token = relocation.relocate_token(token)?;
}
Some(())
}
fn relocate_vertical(
nodes: &mut [NativeVerticalNode],
relocation: &mut EventRelocation<'_>,
) -> Option<()> {
for node in nodes {
*node = relocation.relocate_vertical(node)?;
}
Some(())
}
@@ -200,6 +200,14 @@ impl Executor {
}
pub(super) fn equivalent_accounting_history(&self, old: &Self) -> bool {
self.equivalent_accounting_history_with_entry_sizes(old, None)
}
pub(super) fn equivalent_accounting_history_with_entry_sizes(
&self,
old: &Self,
entry_sizes: Option<(usize, usize)>,
) -> bool {
// These containers may reintroduce previously retained allocations
// that are outside the accounting walk at this boundary.
let fully_visited = |engine: &Self| {
@@ -219,6 +227,13 @@ impl Executor {
let old_live = old.collect_live_usage(None, None, collector().tracking_aliases());
// The splice translates steps separately and checks the job limit.
live.usage.steps = old_live.usage.steps;
if let Some((previous, current)) = entry_sizes {
let restore = |bytes: usize| bytes.checked_sub(current)?.checked_add(previous);
let Some(source) = restore(live.usage.source_payload_bytes) else { return false; };
let Some(mouth) = restore(live.usage.mouth_payload_bytes) else { return false; };
live.usage.source_payload_bytes = source;
live.usage.mouth_payload_bytes = mouth;
}
live.has_equivalent_history(&old_live, &history, &old_history)
})
}