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();