Share packed box diagnostics across native execution

This commit is contained in:
gloamxun committed 2026-11-28 07:43:44 +08:00
1 parent 526ec9b3b0
commit ca3730338e
13 files changed
+126 -22

No files matched your search

@@ -1,6 +1,7 @@
//! Owned native boxes. Packing follows tex.web hpack/vpackage, not page costs.
use super::*;
use crate::{HorizontalBox, HorizontalItem, Paragraph, ParagraphFont};
use std::sync::Arc;
mod interword;
mod tail;
pub(super) use tail::TailNodeRef;
@@ -56,8 +57,8 @@ pub struct NativeBox {
pub height: i64,
pub depth: i64,
pub content: BoxContent,
/// Original horizontal semantics, retained before paint-time glue setting.
pub diagnostic: Vec<super::diagnostics::DiagnosticNode>,
/// Original horizontal semantics, shared by box copies and diagnostic views.
pub diagnostic: Arc<[super::diagnostics::DiagnosticNode]>,
pub setting: GlueSetting,
// Prepared only on the disposable shipout box after its glue is set.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@@ -662,7 +663,7 @@ pub(super) fn vpack(
depth,
shift: 0,
content: BoxContent::Vertical(nodes),
diagnostic: Vec::new(),
diagnostic: Arc::default(),
setting: set,
interword_runs: Vec::new(),
badness,
@@ -766,7 +767,7 @@ pub(super) fn hpack_with_direction_report(
depth: None,
}));
}
let diagnostic = super::diagnostics::text_nodes(&nodes);
let diagnostic = super::diagnostics::text_nodes(&nodes).into();
Ok((NativeBox {
kind: BoxKind::Horizontal,
display: false,
@@ -66,7 +66,7 @@ fn refresh(b: &mut NativeBox, path: &[usize]) {
refresh(child, rest);
}
if let BoxContent::Horizontal(v) = &b.content {
b.diagnostic = crate::execution::diagnostics::text_nodes(v);
b.diagnostic = crate::execution::diagnostics::text_nodes(v).into();
}
}
fn horizontal(n: NativeVerticalNode) -> TextNode {
@@ -1,6 +1,7 @@
//! Lossless native list diagnostics; never infer TeX nodes from painted positions.
use super::registers::print_scaled;
use super::*;
use std::sync::Arc;
mod math;
pub(super) use math::show_math;
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -72,12 +73,12 @@ pub struct DiagnosticBox {
pub depth: i64,
pub shift: i64,
pub setting: GlueSetting,
pub nodes: Vec<DiagnosticNode>,
pub nodes: Arc<[DiagnosticNode]>,
}
pub(super) fn box_view(b: &NativeBox) -> DiagnosticBox {
let nodes = match &b.content {
BoxContent::Horizontal(_) => b.diagnostic.clone(),
BoxContent::Vertical(v) => vertical_nodes(v),
BoxContent::Vertical(v) => vertical_nodes(v).into(),
};
DiagnosticBox {
kind: b.kind,
@@ -795,7 +795,7 @@ fn empty_alignment_box(width: i64, horizontal: bool, span: Span) -> NativeBox {
} else {
BoxContent::Vertical(Vec::new())
},
diagnostic: Vec::new(),
diagnostic: Default::default(),
setting: GlueSetting::default(),
interword_runs: Vec::new(),
badness: 0,
@@ -1013,7 +1013,7 @@ fn set_alignment_row(
row.excess = 0;
if horizontal {
row.width = prototype.width;
row.diagnostic = super::super::diagnostics::text_nodes(&h);
row.diagnostic = super::super::diagnostics::text_nodes(&h).into();
row.content = BoxContent::Horizontal(h);
} else {
row.height = prototype.height;
@@ -521,7 +521,7 @@ impl Executor {
depth: 0,
shift: 0,
content: BoxContent::Horizontal(Vec::new()),
diagnostic: Vec::new(),
diagnostic: Default::default(),
setting: GlueSetting::default(),
interword_runs: Vec::new(),
badness: 0,
@@ -24,8 +24,8 @@ impl Executor {
return Ok(());
}
// Packing can move existing lists or replace an old register. Recount
// actual owners at an execution boundary before rejecting the job.
let usage = self.resource_usage(shipping, None);
// actual storage here, including a shared diagnostic allocation once.
let usage = self.collect_usage(shipping, None, Collector::for_capacity());
self.material_charge = usage
.active_material
.records()
@@ -40,7 +40,15 @@ impl Executor {
shipping: Option<&NativeBox>,
host: Option<HostResourceUsage>,
) -> ResourceUsage {
let mut c = Collector::default();
self.collect_usage(shipping, host, Collector::default())
}
fn collect_usage(
&self,
shipping: Option<&NativeBox>,
host: Option<HostResourceUsage>,
mut c: Collector,
) -> ResourceUsage {
self.state.collect_statistics(&mut c);
c.usage.steps = self.steps;
c.usage.source_files = self.sources.len();
@@ -132,7 +140,7 @@ impl Executor {
c.vertical(v, Retained);
}
for b in &p.diagnostic_lines {
c.diagnostics(&b.nodes, Retained);
c.shared_diagnostics(&b.nodes, Retained);
}
for paragraph in &p.lines {
for line in &paragraph.lines {
@@ -17,7 +17,7 @@ pub(in crate::execution) fn rebind(
BoxContent::Horizontal(nodes) => horizontal(nodes, images)?,
BoxContent::Vertical(nodes) => vertical(nodes, images)?,
}
diagnostics(&mut b.diagnostic, images)
diagnostics(Arc::make_mut(&mut b.diagnostic), images)
}
fn replace(image: &mut Arc<PdfImageUse>, images: &[Arc<PdfImageUse>]) -> Result<(), String> {
@@ -90,7 +90,7 @@ fn leader(pattern: &mut LeaderGlue, images: &[Arc<PdfImageUse>]) -> Result<(), S
fn diagnostics(nodes: &mut [DiagnosticNode], images: &[Arc<PdfImageUse>]) -> Result<(), String> {
for node in nodes {
match node {
DiagnosticNode::Box(b) => diagnostics(&mut b.nodes, images)?,
DiagnosticNode::Box(b) => diagnostics(Arc::make_mut(&mut b.nodes), images)?,
DiagnosticNode::Leaders { pattern, .. } => {
diagnostics(std::slice::from_mut(pattern.as_mut()), images)?;
}
@@ -327,7 +327,7 @@ fn delimiter_native_box(content: BoxContent, width: i64, height: i64, depth: i64
depth,
shift: 0,
content,
diagnostic,
diagnostic: diagnostic.into(),
setting: GlueSetting::default(),
interword_runs: Vec::new(),
badness: 0,
@@ -123,7 +123,7 @@ impl Converter<'_> {
depth,
shift: 0,
content: BoxContent::Vertical(nodes),
diagnostic: Vec::new(),
diagnostic: Default::default(),
setting: GlueSetting::default(),
interword_runs: Vec::new(),
badness: 0,
@@ -32,7 +32,7 @@ impl Converter<'_> {
&& matches!(nodes[1], TextNode::Kern { .. })
{
nodes.pop();
b.diagnostic = super::diagnostics::text_nodes(nodes);
b.diagnostic = super::diagnostics::text_nodes(nodes).into();
}
}
Ok(b)
@@ -152,8 +152,16 @@ pub(super) struct Collector {
pub usage: ResourceUsage,
macros: BTreeSet<usize>,
token_lists: BTreeSet<(usize, usize)>,
shared_diagnostic_lists: Option<BTreeSet<usize>>,
}
impl Collector {
pub fn for_capacity() -> Self {
Self {
shared_diagnostic_lists: Some(BTreeSet::new()),
..Self::default()
}
}
pub fn macro_body(&mut self, m: &Arc<Macro>) {
if self.macros.insert(Arc::as_ptr(m) as usize) {
self.usage.unique_macro_bodies += 1;
@@ -187,7 +195,7 @@ impl Collector {
BoxContent::Horizontal(v) => self.text(v, o),
BoxContent::Vertical(v) => self.vertical(v, o),
}
self.diagnostics(&b.diagnostic, o);
self.shared_diagnostics(&b.diagnostic, o);
}
pub fn horizontal_box(&mut self, b: &crate::HorizontalBox, o: Ownership) {
if let Some(b) = &b.native_box {
@@ -298,12 +306,24 @@ impl Collector {
self.text(&b.nodes, Ownership::Active);
self.usage.active_material.ligature_work_items += b.ligature_work_items();
}
pub fn diagnostics(&mut self, nodes: &[super::diagnostics::DiagnosticNode], o: Ownership) {
pub fn shared_diagnostics(
&mut self,
nodes: &Arc<[super::diagnostics::DiagnosticNode]>,
o: Ownership,
) {
if !nodes.is_empty() && self.shared_diagnostic_lists.as_mut()
.is_none_or(|seen| seen.insert(nodes.as_ptr() as usize))
{
self.diagnostics(nodes, o);
}
}
fn diagnostics(&mut self, nodes: &[super::diagnostics::DiagnosticNode], o: Ownership) {
use super::diagnostics::DiagnosticNode as D;
self.material(o).diagnostic_nodes += nodes.len();
for n in nodes {
match n {
D::Box(b) => self.diagnostics(&b.nodes, o),
D::Box(b) => self.shared_diagnostics(&b.nodes, o),
D::Leaders { pattern, .. } => {
self.diagnostics(std::slice::from_ref(pattern.as_ref()), o)
}
@@ -306,6 +306,44 @@ fn local_switches_failures_sink_errors_and_unknown_host_metrics_are_not_silenced
fs::remove_dir_all(p).unwrap();
}
#[test]
fn nested_box_views_share_diagnostic_storage_at_the_node_limit() {
let p = directory();
let material = r"\kern1sp ".repeat(96);
let wrapping = r"\setbox0=\hbox{\box0}".repeat(16);
let source = format!(r"{HEADER}\setbox0=\hbox{{{material}}}{wrapping}\shipout\box0\end");
let mut files = Files::new(&p);
let execution = Executor::new(
State::initex().with_etex(),
Limits {
tokens: 1024,
..Limits::default()
},
)
.with_native_pages()
.run(
SourceFile {
name: "probe.tex".into(),
data: source.into_bytes(),
},
&mut files,
)
.unwrap();
let boxes = execution
.events
.iter()
.filter_map(|event| match event {
Event::Shipout(page) => Some(&page.box_node),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(boxes.len(), 1);
assert_eq!(boxes[0].width, 96);
assert_eq!(boxes[0].node_count(), 129);
fs::remove_dir_all(p).unwrap();
}
#[test]
fn statistics_observe_real_paragraph_math_output_and_language_data_without_rewriting_lists() {
let p = directory();
+36
View File
@@ -819,6 +819,42 @@ letterspace-fresh.log 和 device-pdftex-spaced-moved.log。
[原版字距虚拟字体构造](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L16400-L16492)
[原版格式所写字体字段](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftex.web#L30627-L30696)
### 盒子副本共享诊断列表
原样 xeCJK 下划线样稿的默认容量失败已从执行事件归因。
在第 56 行停止时,累计保留 1,768,696 条逻辑材料记录,其中
1,296,309 条为诊断节点。段落事件保留 990,828 条,垂直材料事件
440,005 条,两次出页保留 335,901 条。诊断列表此前随层层
套盒和盒的副本递归分配。
盒与语义诊断视图共享只读诊断列表,容量复核按实际共享分配计数。
尾节点变化和格式图像重绑定各自建立需要更新的列表。原始材料和
诊断报告继续按逻辑列表保留,原统计的活动和保留项仍能对照。
16,000 个 kern 的 68 层套盒此前在默认容量停止。新构建以同一
容量输出一页,144 DPI 光栅与实际 XeTeX 参考逐像素相同。小型盒
格式转储及重载的两页 PDF 与直接执行逐字节一致。统计与 special
的八项定向回归通过。
四页会话在检查点前建立并复制嵌套盒,三次成功修订只发第 2 页,
后两次各复用 2,143 步。PDF、测量页面、SVG、来源映射和 aux
与独立全量编译一致,失败保留已发布版本和 aux。
原样样稿以同一格式、资源和默认节点容量复编。原来第 56 行
的删除线段落已经越过,完整作业保留四次出页。下一次触发容量
限制发生在源第 87 行进入 LaTeX 的页面标记提取时。共执行
65,451,429 步,保留 3,232,797 条逻辑材料记录。参考为六页,
该次原生作业尚无 PDF。下一步归因实际水平和垂直子列表在
复制、重装箱与执行事件中的分配,优先复用已封装的列表。
独立拆盒探针记录了另一个实际语义缺口。原版对拆盒过程中读到
的两份无限 shrink 逐项发出诊断,并保留转换后的两个有限 shrink,
继续写出 XDV。当前原生遇到第一份即停止。LaTeX 的真实页面标记
提取也调用该原语。该探针保留在 next-split-shrink,便于后续
定位。样稿的最近实际失败仍是节点容量。
[参考拆盒中的 shrink 转换](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/xetexdir/xetex.web#L23162-L23190)
证据在 target/p3-xecjk-material,receipt.json 为
902fff05e6dbde96473ce0b551a0bb544ef2599f5b6756be7c3b23bdf54c0bab。
### XeTeX 局部 PDF 内容和标记文本
pdf:content 在实际出页位置保存图形状态、平移并写出原始字节,