Reuse settled TeX suffixes after physical line edits

This commit is contained in:
gloamxun committed 2026-12-01 23:49:39 +08:00
1 parent 5b86a7ac7e
commit ac5f691a48
15 files changed
+518 -30

No files matched your search

@@ -811,6 +811,7 @@ impl Executor {
};
let (b, (missing, extra)) = result.map_err(|e| self.error(e, frame.span))?;
if missing != 0 || extra != 0 {
self.observe_line_number(self.current.source);
let line = self.error("", self.current).line;
files
.observe_message(&format!(
@@ -193,6 +193,25 @@ impl EntryCheckpoint {
None if self.frontier == prior.frontier => None,
None => return None,
};
let observes_entry_line = completed.line_observations.as_ref().is_none_or(|observations| {
observations.get(&self.entry) != old.line_observations.get(&self.entry)
});
if observes_entry_line
&& relocation.as_ref().map_or_else(
|| !SourceRelocation::same_line_breaks(previous_bytes, current_bytes),
SourceRelocation::changes_line_numbers,
)
{
// The old line number can be embedded in outputs. Equal byte
// offsets can also mean different lines if a newline moved.
return None;
}
let line_observations = match &completed.line_observations {
Some(later) => Some(splice_line_observations(
&current.line_observations, &old.line_observations, later,
)?),
None => None,
};
// Comparison and publication visit some of the same original
// allocations. Keep the checked relocations for this donor only.
let mut moved = relocation.as_ref().map(EventRelocation::new);
@@ -240,6 +259,7 @@ impl EntryCheckpoint {
stream_sources: completed.stream_sources.clone(),
generated_inputs: completed.generated_inputs.clone(),
reads: completed.reads.clone(),
line_observations,
incoming_state: completed.incoming_state,
outgoing_state: completed.outgoing_state,
steps,
@@ -344,6 +364,9 @@ impl EntryCheckpoint {
}
let mut executor = next.clone();
executor.line_observations = splice_line_observations(
&current.line_observations, &old.line_observations, &next.line_observations,
)?;
let mut events = prefix.clone();
// Checkpoint capture seals the event log. Share the old suffix
// chunks so all carried checkpoints keep the same allocation graph.
@@ -400,6 +423,26 @@ fn measure<T>(elapsed: Option<&mut Duration>, action: impl FnOnce() -> T) -> T {
value
}
// Counts describe reads observed on a particular execution history.
fn splice_line_observations(
current: &BTreeMap<usize, usize>,
prior: &BTreeMap<usize, usize>,
later: &BTreeMap<usize, usize>,
) -> Option<BTreeMap<usize, usize>> {
if prior.keys().any(|source| !later.contains_key(source)) {
return None;
}
let mut combined = current.clone();
for (&source, &count) in later {
let suffix = count.checked_sub(prior.get(&source).copied().unwrap_or_default())?;
if suffix != 0 {
let total = combined.entry(source).or_default();
*total = total.checked_add(suffix)?;
}
}
Some(combined)
}
fn offset_event_steps(event: &Event, offset: StepOffset) -> Option<Event> {
match event {
Event::Statistics(report) => Some(Event::Statistics(Box::new(report.with_step_offset(offset)?))),
@@ -469,7 +512,6 @@ fn same_kernel_continuation(
restored_pdf_image_count,
format_image_warnings,
pdf_force_pagebox_warned,
input_position,
box_nodes_created,
material_charge,
mark_cells_created,
@@ -492,6 +534,8 @@ fn same_kernel_continuation(
generated_inputs,
conditionals,
current,
input_position,
line_observations,
pdf_catalog_tokens,
pdf_outlines,
pdf_info_tokens,
@@ -548,6 +592,7 @@ fn same_kernel_continuation(
&& current.generated_inputs == prior.generated_inputs
&& current.conditionals == prior.conditionals
&& current.current == prior.current
&& current.input_position == prior.input_position
&& current.pdf_catalog_tokens == prior.pdf_catalog_tokens
&& current.pdf_outlines == prior.pdf_outlines
&& current.pdf_info_tokens == prior.pdf_info_tokens
@@ -7,7 +7,9 @@ pub(super) fn same_kernel_provenance(
relocation: &mut EventRelocation<'_>,
) -> Option<bool> {
let map = relocation.map();
if current.current != map.relocate_span(prior.current)? {
if current.current != map.relocate_span(prior.current)?
|| current.input_position != map.relocate_position(prior.input_position)?
{
return Some(false);
}
for (current, prior) in current.inputs.iter().zip(&prior.inputs) {
@@ -58,6 +60,7 @@ pub(super) fn same_kernel_provenance(
let mut conditionals = prior.conditionals.clone();
for conditional in &mut conditionals {
conditional.span = map.relocate_span(conditional.span)?;
conditional.origin = map.relocate_position(conditional.origin)?;
}
let mut catalog = prior.pdf_catalog_tokens.clone();
let mut info = prior.pdf_info_tokens.clone();
@@ -128,6 +131,7 @@ pub(super) fn append_suffix(
execution.events.push(relocation.relocate_event(event)?);
}
}
execution.input_position = map.relocate_position(execution.input_position)?;
execution.state.relocate_source(relocation)?;
relocate_page(&mut execution.native_page, relocation)?;
relocation.relocate_vertical(&mut execution.split_discards)?;
@@ -43,6 +43,7 @@ impl Executor {
record.push_str(&format!("(level {level})"));
if let Some((source, line)) = origin {
self.reads.insert(Dependency::InputPosition(source));
self.observe_line_number(source);
if line != 0 {
record.push_str(&format!(" entered on line {line}"));
}
@@ -208,6 +209,11 @@ impl Executor {
self.reads.insert(Dependency::EscapeChar);
self.reads.insert(Dependency::NewLineChar);
let context = self.input_context();
for frame in &context.frames {
if let ContextKind::Source { source, .. } = frame.kind {
self.observe_line_number(source);
}
}
self.reads.extend(context.reads.iter().cloned());
context
}
@@ -457,6 +463,11 @@ impl Executor {
let (body, dependencies) = self
.activity_lists(breadth, depth, wrap)
.map_err(|message| self.error(message, span))?;
for dependency in &dependencies {
if let Dependency::InputPosition(source) = dependency {
self.observe_line_number(*source);
}
}
self.reads.extend(dependencies);
body
}
@@ -523,6 +534,7 @@ impl Executor {
}
if let Some((source, line)) = frame.origin.filter(|(_, line)| *line != 0) {
self.reads.insert(Dependency::InputPosition(source));
self.observe_line_number(source);
description.push_str(&format!(" entered on line {line}"));
}
format!("### level {}: {description}", index + 1)
@@ -547,6 +559,7 @@ impl Executor {
);
if let Some((source, line)) = origin.filter(|(_, line)| *line != 0) {
self.reads.insert(Dependency::InputPosition(source));
*self.line_observations.entry(source).or_default() += 1;
text.push_str(&format!(" entered at line {line}"));
}
let detail = if let Some(frame) =
@@ -124,7 +124,8 @@ impl Executor {
Err((span, text)) => Next::Error(span, text),
};
if !warnings.is_empty() {
self.observe_xe_utf8_warnings(warnings, files)?;
let source = *source;
self.observe_xe_utf8_warnings(Some(source), warnings, files)?;
}
next
}
@@ -252,9 +253,13 @@ impl Executor {
pub(super) fn observe_xe_utf8_warnings(
&mut self,
source: Option<usize>,
warnings: Vec<(usize, usize)>,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if !warnings.is_empty() && let Some(source) = source {
self.observe_line_number(source);
}
for (line, count) in warnings {
self.observe_xe_input_warning(
&format!("\nInvalid UTF-8 byte or sequence at line {line} replaced by U+FFFD.\n"),
@@ -121,6 +121,7 @@ impl Executor {
);
if let Some((source, line)) = origin {
self.reads.insert(Dependency::InputPosition(source));
self.observe_line_number(source);
if line != 0 {
name.push_str(&format!(" entered at line {line}"));
}
@@ -146,6 +147,7 @@ impl Executor {
}
if let Some((source, line)) = frame.origin {
self.reads.insert(Dependency::InputPosition(source));
self.observe_line_number(source);
if line != 0 {
name.push_str(&format!(" entered on line {line}"));
}
@@ -108,6 +108,7 @@ impl Executor {
let mut message = format!("{{{action} {name} group (level {level})");
if let Some((source, line)) = origin {
self.reads.insert(Dependency::InputPosition(source));
self.observe_line_number(source);
if line != 0 {
let location = if entering {
"at line"