From f6f447dd59fa20d8378252ea4315935863542d29 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sun, 29 Nov 2026 12:22:54 +0800 Subject: [PATCH] Move the advanced child executor into the live job --- .../texrra-tex-core/src/execution/engine.rs | 30 ++++++++++--------- .../src/execution/engine/continuation.rs | 3 +- crates/texrra-tex-core/src/execution/mouth.rs | 5 ++++ src/native_checkpoint_guard.rs | 20 +++---------- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index c63ccc3..5b76284 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -331,8 +331,8 @@ pub struct CheckpointSource<'a> { /// validated and replayed the previous suffix into this revision's draft. pub enum CheckpointDecision { Continue, - /// Move to a validated later command boundary in the same input. - Advance(Arc), + /// Move to a validated later command boundary and select captures there. + Advance(Box), Reuse(Box), ReplayFailed(String), } @@ -557,13 +557,16 @@ impl EntryCheckpoint { } impl Executor { - fn rebind_checkpoint_source(&mut self, id: usize, file: SourceFile) { - let mouth = self.inputs.iter_mut().rev().find_map(|frame| match frame { + fn source_mouth_mut(&mut self, id: usize) -> &mut Mouth { + self.inputs.iter_mut().rev().find_map(|frame| match frame { InputFrame::Source { source, mouth, .. } if *source == id => Some(mouth), _ => None, - }).expect("checkpoint retains the active scanner"); + }).expect("checkpoint retains the source scanner") + } + + fn rebind_checkpoint_source(&mut self, id: usize, file: SourceFile) { if self.sources[id].as_ref() != &file { - mouth.rebind_loaded_prefix(file.data.clone()); + self.source_mouth_mut(id).rebind_loaded_prefix(file.data.clone()); self.sources[id] = Arc::new(file); } } @@ -3976,7 +3979,7 @@ impl Executor { mut select: impl FnMut(CheckpointSource<'_>, usize) -> Option, 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| { + move |engine, initial_depth, pages_processed, files| loop { if engine.job_ended { return CheckpointDecision::Continue; } @@ -4031,14 +4034,13 @@ impl Executor { }; match capture(slot, checkpoint, files) { CheckpointDecision::Advance(checkpoint) => { - let cancellation = engine.cancellation.take(); - let line_break_cache = engine.line_break_cache.take(); - *engine = checkpoint.executor.clone(); - engine.cancellation = cancellation; - engine.line_break_cache = line_break_cache; - CheckpointDecision::Continue + debug_assert!(checkpoint.completed_steps() > engine.steps); + let mut advanced = checkpoint.executor; + advanced.cancellation = engine.cancellation.take(); + advanced.line_break_cache = engine.line_break_cache.take(); + *engine = advanced; } - decision => decision, + decision => return decision, } } } diff --git a/crates/texrra-tex-core/src/execution/engine/continuation.rs b/crates/texrra-tex-core/src/execution/engine/continuation.rs index 3f870e0..575155a 100644 --- a/crates/texrra-tex-core/src/execution/engine/continuation.rs +++ b/crates/texrra-tex-core/src/execution/engine/continuation.rs @@ -140,7 +140,8 @@ impl EntryCheckpoint { return None; } let mut executor = later.executor.clone(); - executor.rebind_checkpoint_source(child, (**source).clone()); + executor.source_mouth_mut(child).share_loaded_source(self.source_mouth(child)); + executor.sources[child] = Arc::clone(source); executor.deferred_accounting = Some(Arc::new(DeferredAccounting { current: Arc::clone(self), basis, diff --git a/crates/texrra-tex-core/src/execution/mouth.rs b/crates/texrra-tex-core/src/execution/mouth.rs index 4fc6ca7..ef515e1 100644 --- a/crates/texrra-tex-core/src/execution/mouth.rs +++ b/crates/texrra-tex-core/src/execution/mouth.rs @@ -350,6 +350,11 @@ impl Mouth { debug_assert!(self.matches_loaded_prefix(&data)); self.data = Arc::new(data); } + pub(super) fn share_loaded_source(&mut self, current: &Self) { + debug_assert!(self.matches_loaded_prefix(¤t.data)); + self.data = Arc::clone(¤t.data); + } + pub(super) fn payload_bytes(&self) -> usize { self.data.len() } diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index 5c461f9..db0139a 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -687,10 +687,9 @@ impl PublishedCheckpoints { } impl CheckpointSet { - fn advance_child(&self, current: &CachedCheckpoint) -> Option<(EntryCheckpoint, Option)> { + fn advance_child(&self, current: &CachedCheckpoint) -> Option { let boundary = current.child_boundary.as_ref()?; let child = current.guard.nested_source.as_ref()?; - let Capture::Nested { file, interval: 0 } = current.slots else { return None; }; let previous = self.entries.iter().find(|candidate| { matches!(candidate.slots, Capture::Nested { interval: 0, .. }) && candidate.entry.frontier() == current.entry.frontier() @@ -707,14 +706,7 @@ impl CheckpointSet { { return None; } - let advanced = current.entry.advance_within_child(&previous.entry, &later.entry)?; - let bytes = usize::try_from(child.observed_size?).ok()?; - // A valid advance can land before the new midpoint. Leave the - // later slot available so the current run can capture it there. - let retain = (advanced.frontier() > bytes.div_ceil(NESTED_INTERVALS) - && advanced.frontier() < bytes) - .then_some(Capture::Nested { file, interval: 1 }); - Some((advanced, retain)) + current.entry.advance_within_child(&previous.entry, &later.entry) }) } @@ -937,16 +929,12 @@ impl SelectedCheckpoint { if allow_suffix && let Capture::Nested { file, interval: 0 } = slots && let Some(current) = captures.nested[file][0].as_ref() && Arc::ptr_eq(¤t.entry, &entry) - && let Some((advanced, slots)) = self.set.advance_child(current) + && let Some(advanced) = self.set.advance_child(current) { - let advanced = Arc::new(advanced); let reused = advanced.completed_steps() - entry.completed_steps(); - if let Some(slots) = slots { - captures.capture(slots, advanced.clone(), host); - } work.reused_steps += reused; work.reused_suffix_steps += reused; - return CheckpointDecision::Advance(advanced); + return CheckpointDecision::Advance(Box::new(advanced)); } let Capture::Settled { page, .. } = slots else { return CheckpointDecision::Continue;