diff --git a/crates/texrra-tex-core/src/execution/engine/continuation.rs b/crates/texrra-tex-core/src/execution/engine/continuation.rs index d4b848d..2e0e076 100644 --- a/crates/texrra-tex-core/src/execution/engine/continuation.rs +++ b/crates/texrra-tex-core/src/execution/engine/continuation.rs @@ -1,4 +1,6 @@ use super::*; +mod material; +use material::MaterialReuse; impl InputFrame { fn same_at_shipout(&self, other: &Self) -> bool { @@ -159,15 +161,28 @@ impl EntryCheckpoint { pub fn prepare_suffix_rebase<'a>( &'a self, prior: &'a Self, - ) -> impl Fn(&Self) -> Option + 'a { - // Keep the old allocation for an unchanged event. Later snapshots - // can still refer to it even when the live run rebuilt that event. + completed: &Execution, + ) -> Option Option + 'a> { + // 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 + && !self.executor.equivalent_accounting_history(&prior.executor) + { + return None; + } + // Keep the old allocations of unchanged material, including inside + // edited events, since later snapshots can still refer to them. let mut prefix = prior.executor.events.clone(); let mut current = self.executor.events.iter(); + let mut material = MaterialReuse::default(); for chunk in &mut prefix.prefix { let changes: Vec<_> = chunk.iter().zip(current.by_ref()).enumerate() .filter(|(_, (old, new))| !std::ptr::eq(*old, *new) && old != new) - .map(|(index, (_, new))| (index, new.clone())) + .map(|(index, (old, new))| { + let mut new = new.clone(); + material.event(old, &mut new); + (index, new) + }) .collect(); if !changes.is_empty() { let events = Arc::make_mut(chunk); @@ -176,7 +191,7 @@ impl EntryCheckpoint { } } } - move |later| self.rebase_verified_suffix(prior, later, &prefix) + Some(move |later: &Self| self.rebase_verified_suffix(prior, later, &prefix)) } fn rebase_verified_suffix( diff --git a/crates/texrra-tex-core/src/execution/engine/continuation/material.rs b/crates/texrra-tex-core/src/execution/engine/continuation/material.rs new file mode 100644 index 0000000..195f325 --- /dev/null +++ b/crates/texrra-tex-core/src/execution/engine/continuation/material.rs @@ -0,0 +1,191 @@ +//! Reuse equal child allocations in changed events. The accounting check +//! still decides whether these events can accompany a carried checkpoint. +use super::*; +use crate::HorizontalItem; +use crate::execution::diagnostics::DiagnosticNode; + +#[derive(Default)] +pub(super) struct MaterialReuse { + horizontal: BTreeMap>>, + vertical: BTreeMap>>, + diagnostic: BTreeMap>, + marks: BTreeMap>, +} + +fn reuse_list( + old: &Arc, + new: &mut Arc, + reused: &mut BTreeMap>, +) -> Option { + // The original events outlive this pass, so the input addresses stay + // valid even after modifying a cloned event. + let address = Arc::as_ptr(new).cast::<()>() as usize; + if let Some(material) = reused.get(&address) { + *new = material.clone(); + return None; + } + if old == new { + *new = old.clone(); + reused.insert(address, old.clone()); + return None; + } + Some(address) +} + +impl MaterialReuse { + pub(super) fn event(&mut self, old: &Event, new: &mut Event) { + match (old, new) { + (Event::Shipout(a), Event::Shipout(b)) => self.native_box(&a.box_node, &mut b.box_node), + (Event::VerticalMaterial(a), Event::VerticalMaterial(b)) => self.vertical_nodes(a, b), + (Event::BuiltDisplay(a), Event::BuiltDisplay(b)) => { + self.native_box(&a.box_, &mut b.box_) + } + (Event::BuiltParagraph(a), Event::BuiltParagraph(b)) => { + self.text(&a.nodes, &mut b.nodes); + for (old, new) in a.line_boxes.iter().zip(&mut b.line_boxes) { + self.native_box(old, new); + } + for (old, new) in a.line_adjustments.iter().zip(&mut b.line_adjustments) { + self.vertical_nodes(old, new); + } + for (old, new) in a.diagnostic_lines.iter().zip(&mut b.diagnostic_lines) { + self.diagnostic_nodes(&old.nodes, &mut new.nodes); + } + for (old, new) in a.lines.iter().zip(&mut b.lines) { + for (old, new) in old.lines.iter().zip(&mut new.lines) { + for (old, new) in old.iter().zip(new) { + if let (HorizontalItem::Box(old), HorizontalItem::Box(new)) = (old, new) + { + self.horizontal_box(old, new); + } + } + } + } + } + _ => {} + } + } + + fn native_box(&mut self, old: &NativeBox, new: &mut NativeBox) { + match (&old.content, &mut new.content) { + (BoxContent::Horizontal(a), BoxContent::Horizontal(b)) => { + if let Some(address) = reuse_list(a, b, &mut self.horizontal) { + self.text(a.as_slice(), Arc::make_mut(b).as_mut_slice()); + self.horizontal.insert(address, b.clone()); + } + } + (BoxContent::Vertical(a), BoxContent::Vertical(b)) => { + if let Some(address) = reuse_list(a, b, &mut self.vertical) { + self.vertical_nodes(a.as_slice(), Arc::make_mut(b).as_mut_slice()); + self.vertical.insert(address, b.clone()); + } + } + _ => {} + } + self.diagnostic_nodes(&old.diagnostic, &mut new.diagnostic); + } + + fn horizontal_box(&mut self, old: &crate::HorizontalBox, new: &mut crate::HorizontalBox) { + if let (Some(old), Some(new)) = (&old.native_box, &mut new.native_box) { + self.native_box(old, new); + } + } + + fn vertical_nodes(&mut self, old: &[NativeVerticalNode], new: &mut [NativeVerticalNode]) { + use NativeVerticalNode as V; + for (a, b) in old.iter().zip(new) { + match (a, b) { + (V::Box(a), V::Box(b)) | (V::Line(a), V::Line(b)) => self.native_box(a, b), + (V::Mark(a), V::Mark(b)) => self.mark(a, b), + (V::Insertion(a), V::Insertion(b)) => self.vertical_nodes(&a.nodes, &mut b.nodes), + (V::Leaders(a), V::Leaders(b)) => self.leaders(a, b), + _ => {} + } + } + } + + fn text(&mut self, old: &[TextNode], new: &mut [TextNode]) { + use TextNode as T; + for (a, b) in old.iter().zip(new) { + match (a, b) { + (T::Glyph { glyph: a, .. }, T::Glyph { glyph: b, .. }) + | (T::Box(a), T::Box(b)) + | (T::MathChar(a), T::MathChar(b)) => self.horizontal_box(a, b), + ( + T::Discretionary { + pre_break: pre_a, + post_break: post_a, + .. + }, + T::Discretionary { + pre_break: pre_b, + post_break: post_b, + .. + }, + ) => { + self.text(pre_a, pre_b); + self.text(post_a, post_b); + } + (T::Mark(a), T::Mark(b)) => self.mark(a, b), + (T::Adjustment(a), T::Adjustment(b)) => self.vertical_nodes(&a.nodes, &mut b.nodes), + (T::Insertion(a), T::Insertion(b)) => self.vertical_nodes(&a.nodes, &mut b.nodes), + (T::Leaders(a), T::Leaders(b)) => self.leaders(a, b), + _ => {} + } + } + } + + fn diagnostic_nodes(&mut self, old: &Arc<[DiagnosticNode]>, new: &mut Arc<[DiagnosticNode]>) { + if let Some(address) = reuse_list(old, new, &mut self.diagnostic) { + self.diagnostics(old, Arc::make_mut(new)); + self.diagnostic.insert(address, new.clone()); + } + } + + fn diagnostics(&mut self, old: &[DiagnosticNode], new: &mut [DiagnosticNode]) { + use DiagnosticNode as D; + for (a, b) in old.iter().zip(new) { + match (a, b) { + (D::Box(a), D::Box(b)) => self.diagnostic_nodes(&a.nodes, &mut b.nodes), + (D::Leaders { pattern: a, .. }, D::Leaders { pattern: b, .. }) => { + self.diagnostics( + std::slice::from_ref(a.as_ref()), + std::slice::from_mut(b.as_mut()), + ); + } + ( + D::Discretionary { + pre: pre_a, + post: post_a, + .. + }, + D::Discretionary { + pre: pre_b, + post: post_b, + .. + }, + ) => { + self.diagnostics(pre_a, pre_b); + self.diagnostics(post_a, post_b); + } + (D::Mark(a), D::Mark(b)) => self.mark(a, b), + (D::Adjustment { nodes: a, .. }, D::Adjustment { nodes: b, .. }) + | (D::Insertion { nodes: a, .. }, D::Insertion { nodes: b, .. }) => { + self.diagnostics(a, b) + } + _ => {} + } + } + } + + fn mark(&mut self, old: &NativeMark, new: &mut NativeMark) { + let _ = reuse_list(&old.tokens, &mut new.tokens, &mut self.marks); + } + + fn leaders(&mut self, old: &LeaderGlue, new: &mut LeaderGlue) { + if let (LeaderPattern::Box(old), LeaderPattern::Box(new)) = (&old.pattern, &mut new.pattern) + { + self.native_box(old, new); + } + } +} diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index c07fb38..1991b89 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -18,6 +18,32 @@ | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | +## 2026-12-01 正文编辑后保留排版材料的共享关系 + +原样 NeurIPS 改第 3 页标点,完整事件序列中只有四条值发生变化。 +其中竖直材料、段落和出页三条持有盒子或诊断列表。此前合并历史 +后,活跃状态的统计相符,保留历史却多计了 49 个盒子、2,032 个 +水平节点和 2,043 个诊断节点,后续候选因此被拒绝。 + +合并时保留变化事件中值相同的子列表分配。沿用当前修订不同的值, +在同一次合并中保留其共享关系。保存后续检查点前仍检查会合位置 +和恢复位置的完整材料统计及存活引用关系,并重放和核对宿主边界。 +此前未读历史统计的作业也在会合位置完成材料核验,以便后续修订 +可以开启统计。 + +原样 NeurIPS 先改第 3 页标点,再改单个第 9 页字母。第二次从 +第 620 行恢复,上一版从第 345 行恢复。实际执行从 3,699,397 +降至 1,030,250 步,减少 72.2%。两次分别只更新第 3、9 页。 +完整执行、PDF、SVG、来源、查找、资源、流、aux/out 和日志均 +逐次与同版独立全量一致。两个版本对应的 PDF 和 SVG 字节相同。 + +优化构建及三项定向回归通过。12 页用例让后续页面复用同一份 +TeX 盒子,并校验持续编辑与全量一致。证据在 target/p4-alias-graft/。 +SHA256SUMS 指纹为 +f10c4fcf3e81caa642efcfa873fb48032b8937eab1e8c1ceed3dbb31271c8839。 +下一入口是逻辑步数变化后的安全收敛,需继续核对步数预算和真实 +统计输出。正式浏览器可见时延、峰值内存和兼容矩阵继续统一验收。 + ## 2026-12-01 将已验证的后续检查点带入新修订 后缀复用完成时,会话可保留更靠后的执行检查点供下次编辑恢复。 diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index 5366d60..dd7810d 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -417,16 +417,17 @@ impl<'a> CapturedEntries<'a> { let bytes = project.file(&completed.guard.entry).expect("settled entry").data.len(); let stride = bytes.div_ceil(SOURCE_INTERVALS).max(1); let mut later = [const { None }; SOURCE_INTERVALS]; - let rebase = current.prepare_suffix_rebase(&prior.entry); - // Keep the deepest compatible settled continuation per source interval. - for (&page, anchor) in completed.settled.range((std::ops::Bound::Excluded(page), std::ops::Bound::Unbounded)).rev() { - let interval = (anchor.entry.frontier() - 1) / stride; - if later[interval].is_some() { - continue; - } - check(cancellation).map_err(|error| error.to_string())?; - if let Some(entry) = rebase(&anchor.entry) { - later[interval] = Some((page, anchor, entry)); + if let Some(rebase) = current.prepare_suffix_rebase(&prior.entry, &completed.result.execution) { + // Keep the deepest compatible settled continuation per source interval. + for (&page, anchor) in completed.settled.range((std::ops::Bound::Excluded(page), std::ops::Bound::Unbounded)).rev() { + let interval = (anchor.entry.frontier() - 1) / stride; + if later[interval].is_some() { + continue; + } + check(cancellation).map_err(|error| error.to_string())?; + if let Some(entry) = rebase(&anchor.entry) { + later[interval] = Some((page, anchor, entry)); + } } } diff --git a/tests/native_pipeline.rs b/tests/native_pipeline.rs index ee0b327..90a7e4c 100644 --- a/tests/native_pipeline.rs +++ b/tests/native_pipeline.rs @@ -1706,14 +1706,15 @@ fn consecutive_edits_reuse_later_settled_suffixes() { fn settled_suffix_carries_a_verified_prefix_into_later_edits() { let cwd = directory("long-settled-suffix"); let entry = cwd.join("main.tex"); - let middle = "\\shipout\\hbox{Middle}\\relax\n".repeat(6); + let middle = "\\shipout\\hbox{\\copy7 Middle}\\relax\n".repeat(6); let tail = "\\shipout\\hbox{Unchanged}\\relax\n".repeat(3); let body = |second, ninth| { format!(r" \tracingstats=1 \shipout\hbox{{Cover ABXY}} \setbox3=\hbox{{{second}}} -\shipout\box3 +\setbox7=\hbox{{Shared}} +\shipout\hbox{{\unhbox3\copy7}} \relax {middle}\setbox3=\hbox{{{ninth}}} \shipout\box3