From 51db480b34c2c70ea8c4af9471221439e57704d8 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sun, 29 Nov 2026 11:10:17 +0800 Subject: [PATCH] Advance replay checkpoints through completed entry lines --- .../texrra-tex-core/src/execution/engine.rs | 18 +++++ crates/texrra-tex-core/src/execution/mouth.rs | 11 ++++ docs/P2_M1_PROGRESS.md | 33 ++++++++++ src/native_checkpoint_guard.rs | 62 ++++++++++++++---- tests/native_pipeline.rs | 65 +++++++++++++++++++ 5 files changed, 175 insertions(+), 14 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/engine.rs b/crates/texrra-tex-core/src/execution/engine.rs index 11f46a9..2d826d3 100644 --- a/crates/texrra-tex-core/src/execution/engine.rs +++ b/crates/texrra-tex-core/src/execution/engine.rs @@ -322,6 +322,7 @@ pub struct CheckpointSource<'a> { pub bytes: usize, pub frontier: usize, pub is_entry: bool, + pub line_consumed: bool, pub output_returned: bool, } @@ -355,6 +356,22 @@ impl EntryCheckpoint { self.executor.steps } + /// The later anchor may replace an ordinary checkpoint on the same + /// already loaded physical line when it can accept the same source edits. + pub fn can_advance_entry_anchor(&self, earlier: &Self) -> bool { + self.active_source == self.entry + && earlier.active_source == earlier.entry + && self.frontier == earlier.frontier + && self.executor.steps > earlier.executor.steps + && self.executor.sources[self.entry].name == earlier.executor.sources[earlier.entry].name + && self.source_mouth(self.entry) + .same_loaded_prefix_constraint(earlier.source_mouth(earlier.entry)) + && (!self.executor.reads.observed_statistics() + || (earlier.executor.reads.observed_statistics() + && self.executor.sources[self.entry].data.len() + == earlier.executor.sources[earlier.entry].data.len())) + } + pub fn accepts_entry(&self, source: &SourceFile) -> bool { self.accepts_entry_bytes(&source.name, &source.data) } @@ -3945,6 +3962,7 @@ impl Executor { bytes: file.data.len(), frontier, is_entry: active_source == entry, + line_consumed: mouth.line_consumed(), output_returned: engine.output_frame.is_none(), }; let Some(slot) = select(position, engine.shipped_pages) else { diff --git a/crates/texrra-tex-core/src/execution/mouth.rs b/crates/texrra-tex-core/src/execution/mouth.rs index 6ef5af5..67ffbea 100644 --- a/crates/texrra-tex-core/src/execution/mouth.rs +++ b/crates/texrra-tex-core/src/execution/mouth.rs @@ -316,6 +316,17 @@ impl Mouth { (matches!(self.line_breaks, LineBreaks::Physical) && !self.end_after_line && self.offset > 0) .then_some(self.offset) } + pub(super) fn line_consumed(&self) -> bool { + self.cursor >= self.line.len() + } + + pub(super) fn same_loaded_prefix_constraint(&self, other: &Self) -> bool { + self.offset == other.offset + && self.data[..self.offset] == other.data[..other.offset] + && self.xe.as_ref().map(|xe| (xe.terminated, &xe.autodetected)) + == other.xe.as_ref().map(|xe| (xe.terminated, &xe.autodetected)) + } + pub(super) fn matches_loaded_prefix(&self, data: &[u8]) -> bool { let Some(offset) = self.physical_frontier() else { return false; diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 155f824..7368fce 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,39 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | +## 2026-11-29 推进已读取物理行的恢复点 + +自动会话在普通入口检查点对应的物理行执行完毕后,可保留该行上更晚 +的命令边界。只有文件读取与解析约束、已记录的宿主调用和已出页数 +保持一致时,才替换原普通恢复点。XeTeX 仍校验实际编码判定及已 +读取的字节,资源统计造成的文件长度依赖也继续生效。保留原有 +检查点槽位及出页收敛优先级。文件大小变化时继续执行实际查询。 +新增见证用真实子文件从 7 字节变至 19 字节驱动规则线宽度,复用 +此前同一物理行的寄存器操作,更新后与独立全量一致。 + +原样 A4 的 7 次修订各只更新受影响的一页。完整执行、PDF、绘制 +列表、SVG、来源映射、读取轨迹、aux 和日志均与独立全量一致。 +同一格式和项目的配对执行步数如下。 + +| 编辑 | 原执行步数 | 新执行步数 | +| --- | ---: | ---: | +| 主文件等长及恢复 | 55,847 | 53,706 | +| 主文件异长 | 55,870 | 53,729 | +| 子文件等长 | 56,886 | 56,886 | +| 子文件异长 | 79,031 | 78,606 | +| 子文件恢复 | 79,001 | 78,576 | + +子文件较早一行的新增等长编辑从子文件的第 40 字节恢复,复用 +838,928 步。晚一行的等长编辑从第 100 字节恢复,复用 846,445 步。 +异长修改在入口文件恢复,复用 824,755 步。从该入口位置至第一个 +可用的子文件检查点相隔 14,173 步。下一批从这段输入定位和早期 +展开入手,验证不同大小查询结果之后能否达到相同执行状态。正式 +编辑到页面可见的时延验收仍待完成。 + +优化构建和 7 项定向回归通过。证据在 +target/p4-finished-line-checkpoints/。SHA256SUMS 指纹为 +0d51e4a4cce9312adcb59bd303e1b4fb91380b77b44153ba78d7baa4d375cecf。 + ## 2026-11-29 按命令分区刷新含义缓存 执行器与实际命令绑定表共用同一次散列。命令定义和出组恢复更新 diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index 60f35a9..9bddbc7 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -96,6 +96,7 @@ struct CaptureSchedule { next_page: usize, fixed_frontier: Option, pending_edit_frontier: Option, + pending_line_end: Option<(usize, usize, usize)>, settled_page: usize, } @@ -114,6 +115,7 @@ const NESTED_INTERVALS: usize = 2; #[derive(Clone, Copy)] enum Capture { Entry(CaptureSlots), + LineEnd { interval: usize }, Nested { file: usize, interval: usize }, Settled { page: usize, regular: Option }, } @@ -164,19 +166,36 @@ impl CaptureSchedule { next_page: 1, fixed_frontier, pending_edit_frontier: edit_frontier, + pending_line_end: None, settled_page: 0, } } fn select_source(&mut self, source: CheckpointSource<'_>, pages: usize) -> Option { let regular = self.select(source.frontier, pages); + let mut line_end = None; + if self.fixed_frontier.is_none() { + if let Some(slots) = regular.filter(|slots| slots.early) { + self.pending_line_end = (!source.line_consumed) + .then_some((source.frontier, slots.interval, pages)); + } else if let Some((frontier, interval, before_pages)) = self.pending_line_end { + if frontier != source.frontier { + self.pending_line_end = None; + } else if source.line_consumed { + self.pending_line_end = None; + if pages == before_pages { + line_end = Some(Capture::LineEnd { interval }); + } + } + } + } if self.fixed_frontier.is_none() && source.output_returned && pages > self.settled_page && pages <= 8 { self.settled_page = pages; return Some(Capture::Settled { page: pages, regular }); } - regular.map(Capture::Entry) + regular.map(Capture::Entry).or(line_end) } fn take_edit_frontier(&mut self, frontier: usize, ordinary_capture: bool) -> bool { @@ -286,7 +305,9 @@ impl<'a> CapturedEntries<'a> { fn retain(&mut self, slots: Capture, checkpoint: Arc) { let slots = match slots { Capture::Entry(slots) => slots, - Capture::Settled { .. } => unreachable!("settled observation is handled before retention"), + Capture::LineEnd { .. } | Capture::Settled { .. } => { + unreachable!("observation is handled before retention") + } Capture::Nested { file, interval } => { self.nested[file][interval] = Some(checkpoint); return; @@ -304,19 +325,32 @@ impl<'a> CapturedEntries<'a> { } fn capture(&mut self, slots: Capture, entry: Arc, host: &RecordingHost) { - let slots = if let Capture::Settled { page, regular } = slots { - if let Some((_, calls)) = host.prefix() { - self.settled.insert(page, SettledCheckpoint { - entry: entry.clone(), - host: host.files().replay_boundary(), - call_count: calls.len(), - }); - self.changed = true; + let slots = match slots { + Capture::Settled { page, regular } => { + if let Some((_, calls)) = host.prefix() { + self.settled.insert(page, SettledCheckpoint { + entry: entry.clone(), + host: host.files().replay_boundary(), + call_count: calls.len(), + }); + self.changed = true; + } + let Some(regular) = regular else { return; }; + Capture::Entry(regular) } - let Some(regular) = regular else { return; }; - Capture::Entry(regular) - } else { - slots + Capture::LineEnd { interval } => { + let Some(earlier) = &self.early[interval] else { return; }; + // With the same host prefix, finishing an already loaded + // line narrows no resource dependency or replay opportunity. + if !host.prefix().is_some_and(|(_, calls)| calls.len() == earlier.replay_count) + || !entry.can_advance_entry_anchor(&earlier.entry) + { + return; + } + let Capture::Entry(slots) = earlier.slots else { unreachable!() }; + Capture::Entry(CaptureSlots { page: false, ..slots }) + } + other => other, }; let (Some((trace, calls)), Some(path), Some(initial)) = ( host.prefix(), diff --git a/tests/native_pipeline.rs b/tests/native_pipeline.rs index c7b8f0e..c28e6c3 100644 --- a/tests/native_pipeline.rs +++ b/tests/native_pipeline.rs @@ -2796,6 +2796,71 @@ fn relocated_format_checks_current_sources_and_consumed_stream_ranges() { fs::remove_dir_all(root).unwrap(); } +#[test] +fn finished_entry_line_is_reused_after_a_child_size_change() { + let cwd = directory("finished-entry-line").canonicalize().unwrap(); + let entry = cwd.join("main.tex"); + let child = cwd.join("child.tex"); + let mut world = WorldConfig::new(&cwd); + world.input_policy = texrra_tex_core::resources::InputPolicy::Tex; + let entry_bytes = format!( + "\\catcode123=1 \\catcode125=2\n{}{{}}\n\\edef\\widthfromsize{{\\pdffilesize{{child.tex}}}}\\input child.tex\n\\shipout\\hbox{{\\vrule width\\widthfromsize pt height2pt}}\\end\n", + "\\advance\\count0 by 1\\relax ".repeat(320), + ); + let initial_child = b"\\relax\n"; + let edited_child = b"\\relax\\relax\\relax\n"; + let project = ProjectSnapshot::new(ProjectDefinition { + configuration: world.clone(), + revision: 1, + roots: [cwd.clone()].into(), + files: [ + (entry, Arc::from(entry_bytes.into_bytes())), + (child.clone(), Arc::from(initial_child.as_slice())), + ] + .into(), + directories: Default::default(), + aliases: Default::default(), + generated_roots: [cwd.clone()].into(), + generated: Default::default(), + }) + .unwrap(); + let mut request = CompileRequest::new( + world, + "main.tex", + "job.pdf", + JobTime::new(2026, 11, 29, 0) + .unwrap() + .with_pdf_clock(0, 0) + .unwrap(), + ) + .observe_generated_initial(); + request.entry = Entry::Initex; + request.etex = true; + let session = NativeCompiler::default().with_automatic_entry_checkpoints(); + let original = session + .compile(request.clone().with_project(project.clone())) + .unwrap(); + + let project = project + .with_file_edits(2, [(child, Arc::from(edited_child.as_slice()))].into()) + .unwrap(); + let request = request.with_project(project); + let edited = session.compile(request.clone()).unwrap(); + let full = NativeCompiler::default().compile(request).unwrap(); + assert_ne!(edited.pdf, original.pdf); + assert_eq!(edited.pdf, full.pdf); + assert_eq!(edited.execution.execution, full.execution.execution); + assert_eq!(edited.source_map.pages, full.source_map.pages); + assert_eq!(edited.source_map.sources, full.source_map.sources); + assert_eq!(edited.resources.trace.reads, full.resources.trace.reads); + assert_eq!(edited.resources.trace.lookups, full.resources.trace.lookups); + let work = &edited.entry_checkpoint_work; + assert_eq!(work.reused_source, None, "{work:?}"); + let steps = edited.execution.execution.steps; + assert!(work.reused_steps > steps - steps / 8, "{work:?} of {steps} steps"); + fs::remove_dir_all(cwd).unwrap(); +} + #[test] fn replay_rejection_resumes_earlier_without_publishing_rejected_effects() { struct PublishedBeforeExecution(Vec<(PathBuf, Vec)>);