Avoid extra copies while assembling relocated TeX suffixes

This commit is contained in:
gloamxun committed 2026-12-01 23:32:01 +08:00
1 parent 5b9eca1d60
commit 5b86a7ac7e
5 files changed
+90 -38

No files matched your search

@@ -221,6 +221,8 @@ impl EntryCheckpoint {
// The joined events are published results. Resumption always starts // The joined events are published results. Resumption always starts
// from a live checkpoint, whose original shared allocations stay intact. // from a live checkpoint, whose original shared allocations stay intact.
let execution = measure(timings.map(|timings| &mut timings.suffix_assembly), || { let execution = measure(timings.map(|timings| &mut timings.suffix_assembly), || {
let mut events = Vec::with_capacity(completed.events.len());
events.extend(current.events.iter().cloned());
let mut execution = Execution { let mut execution = Execution {
format_dumped: completed.format_dumped, format_dumped: completed.format_dumped,
format_pdf_images: completed.format_pdf_images.clone(), format_pdf_images: completed.format_pdf_images.clone(),
@@ -229,7 +231,7 @@ impl EntryCheckpoint {
native_page: completed.native_page.clone(), native_page: completed.native_page.clone(),
split_discards: completed.split_discards.clone(), split_discards: completed.split_discards.clone(),
after_assignment: completed.after_assignment.clone(), after_assignment: completed.after_assignment.clone(),
events: current.events.clone().into_vec(), events,
state: completed.state.clone(), state: completed.state.clone(),
io: completed.io.clone(), io: completed.io.clone(),
input_position: completed.input_position, input_position: completed.input_position,
@@ -75,11 +75,11 @@ pub(super) fn same_kernel_provenance(
let mut discards = prior.split_discards.clone(); let mut discards = prior.split_discards.clone();
let mut native_page = prior.native_page.clone(); let mut native_page = prior.native_page.clone();
let mut pending_shipouts = prior.pending_shipouts.clone(); let mut pending_shipouts = prior.pending_shipouts.clone();
relocate_vertical(&mut vertical.nodes, relocation)?; relocation.relocate_vertical(&mut vertical.nodes)?;
relocate_vertical(&mut discards, relocation)?; relocation.relocate_vertical(&mut discards)?;
relocate_page(&mut native_page, relocation)?; relocate_page(&mut native_page, relocation)?;
for value in &mut pending_shipouts { for value in &mut pending_shipouts {
*value = relocation.relocate_box(value)?; relocation.relocate_box(value)?;
} }
if current.generated_inputs != generated if current.generated_inputs != generated
|| current.conditionals != conditionals || current.conditionals != conditionals
@@ -130,7 +130,7 @@ pub(super) fn append_suffix(
} }
execution.state.relocate_source(relocation)?; execution.state.relocate_source(relocation)?;
relocate_page(&mut execution.native_page, relocation)?; relocate_page(&mut execution.native_page, relocation)?;
relocate_vertical(&mut execution.split_discards, relocation)?; relocation.relocate_vertical(&mut execution.split_discards)?;
if let Some(token) = &mut execution.after_assignment { if let Some(token) = &mut execution.after_assignment {
relocation.relocate_token(token)?; relocation.relocate_token(token)?;
} }
@@ -156,10 +156,10 @@ fn relocate_page(
relocation: &mut EventRelocation<'_>, relocation: &mut EventRelocation<'_>,
) -> Option<()> { ) -> Option<()> {
if let Some(page) = page { if let Some(page) = page {
relocate_vertical(&mut page.nodes, relocation)?; relocation.relocate_vertical(&mut page.nodes)?;
relocate_vertical(page.contributions.make_contiguous(), relocation)?; relocation.relocate_vertical(page.contributions.make_contiguous())?;
relocate_vertical(&mut page.discards, relocation)?; relocation.relocate_vertical(&mut page.discards)?;
relocate_vertical(&mut page.insertions.held, relocation)?; relocation.relocate_vertical(&mut page.insertions.held)?;
} }
Some(()) Some(())
} }
@@ -170,13 +170,3 @@ fn relocate_tokens(tokens: &mut [Token], relocation: &mut EventRelocation<'_>) -
} }
Some(()) Some(())
} }
fn relocate_vertical(
nodes: &mut [NativeVerticalNode],
relocation: &mut EventRelocation<'_>,
) -> Option<()> {
for node in nodes {
*node = relocation.relocate_vertical(node)?;
}
Some(())
}
@@ -20,6 +20,7 @@ type Shared<T> = HashMap<*const T, Arc<T>>;
pub(super) struct EventRelocation<'a> { pub(super) struct EventRelocation<'a> {
map: &'a SourceRelocation, map: &'a SourceRelocation,
changed: bool,
macros: Shared<Macro>, macros: Shared<Macro>,
text_lists: Shared<Vec<TextNode>>, text_lists: Shared<Vec<TextNode>>,
vertical_lists: Shared<Vec<NativeVerticalNode>>, vertical_lists: Shared<Vec<NativeVerticalNode>>,
@@ -32,6 +33,7 @@ impl<'a> EventRelocation<'a> {
pub(super) fn new(map: &'a SourceRelocation) -> Self { pub(super) fn new(map: &'a SourceRelocation) -> Self {
Self { Self {
map, map,
changed: false,
macros: HashMap::new(), macros: HashMap::new(),
text_lists: HashMap::new(), text_lists: HashMap::new(),
vertical_lists: HashMap::new(), vertical_lists: HashMap::new(),
@@ -49,15 +51,12 @@ impl<'a> EventRelocation<'a> {
self.relocated(event) self.relocated(event)
} }
pub(super) fn relocate_box(&mut self, packed: &NativeBox) -> Option<NativeBox> { pub(super) fn relocate_box(&mut self, packed: &mut NativeBox) -> Option<()> {
self.relocated(packed) packed.relocate(self)
} }
pub(super) fn relocate_vertical( pub(super) fn relocate_vertical(&mut self, nodes: &mut [NativeVerticalNode]) -> Option<()> {
&mut self, nodes.relocate(self)
node: &NativeVerticalNode,
) -> Option<NativeVerticalNode> {
self.relocated(node)
} }
pub(super) fn relocate_token(&mut self, token: &mut Token) -> Option<()> { pub(super) fn relocate_token(&mut self, token: &mut Token) -> Option<()> {
@@ -101,6 +100,16 @@ impl<'a> EventRelocation<'a> {
updated.relocate(self)?; updated.relocate(self)?;
Some(updated) Some(updated)
} }
// Track actual coordinate changes only within one shared value. A
// nested cache hit contributes its change to the containing value.
fn relocate_tracking_changes(&mut self, value: &mut (impl Relocate + ?Sized)) -> Option<bool> {
let outer_change = std::mem::take(&mut self.changed);
let result = value.relocate(self);
let changed = self.changed;
self.changed |= outer_change;
result.map(|()| changed)
}
} }
trait Relocate { trait Relocate {
@@ -133,7 +142,9 @@ impl<T: Relocate> Relocate for [T] {
impl Relocate for Span { impl Relocate for Span {
fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> { fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> {
*self = context.map.relocate_span(*self)?; let updated = context.map.relocate_span(*self)?;
context.changed |= *self != updated;
*self = updated;
Some(()) Some(())
} }
} }
@@ -148,11 +159,11 @@ macro_rules! relocate_shared {
fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> { fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> {
let original = Arc::as_ptr(self); let original = Arc::as_ptr(self);
if let Some(updated) = context.$cache.get(&original) { if let Some(updated) = context.$cache.get(&original) {
context.changed |= !Arc::ptr_eq(self, updated);
*self = updated.clone(); *self = updated.clone();
} else { } else {
let mut updated = $copy(self.as_ref()); let mut updated = $copy(self.as_ref());
updated.relocate(context)?; if context.relocate_tracking_changes(&mut updated)? {
if updated != **self {
*self = Arc::from(updated); *self = Arc::from(updated);
} }
context.$cache.insert(original, self.clone()); context.$cache.insert(original, self.clone());
@@ -187,11 +198,7 @@ macro_rules! relocate_fields {
} }
relocate_fields!(Token { span, invocation; kind }); relocate_fields!(Token { span, invocation; kind });
impl Relocate for TokenOrigin { relocate_fields!(TokenOrigin { span, invocation; });
fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> {
relocate_origin(self, context.map)
}
}
fn relocate_origin(origin: &mut TokenOrigin, map: &SourceRelocation) -> Option<()> { fn relocate_origin(origin: &mut TokenOrigin, map: &SourceRelocation) -> Option<()> {
let TokenOrigin { span, invocation } = origin; let TokenOrigin { span, invocation } = origin;
@@ -685,4 +692,38 @@ mod tests {
); );
assert_eq!(original[0].invocation, Some(invocation)); assert_eq!(original[0].invocation, Some(invocation));
} }
#[test]
fn nested_cached_relocation_tracks_changes_per_list() {
let map = SourceRelocation::new(2, b"a\nsecond\n", b"ab\nsecond\n").unwrap();
let location = Span { source: 2, start: 2, end: 8 };
let kind = TokenKind::Character('x', CatCode::Letter);
let changed: Arc<[Token]> = Arc::from([Token::new(kind.clone(), location)]);
let unchanged: Arc<[Token]> =
Arc::from([Token::new(kind, Span { source: 1, ..location })]);
let stable_span = Span { source: 1, ..location };
let mark = |tokens| TextNode::Mark(NativeMark { class: 0, tokens, span: stable_span });
let old_changed = Arc::new(vec![mark(changed.clone())]);
let old_unchanged = Arc::new(vec![mark(unchanged)]);
let mut relocation = EventRelocation::new(&map);
// Prime the child cache before mapping its parent. The parent span
// is stable and only an Arc returned by the cache has moved.
let mapped_child = relocation.relocate_tokens(&changed).unwrap();
let mapped_parent = relocation.relocated(&old_changed).unwrap();
let TextNode::Mark(mapped_mark) = &mapped_parent[0] else {
unreachable!();
};
assert!(!Arc::ptr_eq(&mapped_parent, &old_changed));
assert!(Arc::ptr_eq(&mapped_mark.tokens, &mapped_child));
assert_eq!(mapped_mark.tokens[0].span.start, location.start + 1);
assert!(Arc::ptr_eq(
&relocation.relocated(&old_changed).unwrap(),
&mapped_parent,
));
assert!(Arc::ptr_eq(
&relocation.relocated(&old_unchanged).unwrap(),
&old_unchanged,
));
}
} }
@@ -34,7 +34,7 @@ impl State {
*tokens = relocation.relocate_tokens(tokens)?; *tokens = relocation.relocate_tokens(tokens)?;
} }
for value in self.boxes.values_mut() { for value in self.boxes.values_mut() {
*value = relocation.relocate_box(value)?; relocation.relocate_box(value)?;
} }
for marks in self.page_marks.values_mut() { for marks in self.page_marks.values_mut() {
for tokens in [&mut marks.top, &mut marks.first, &mut marks.bottom] for tokens in [&mut marks.top, &mut marks.first, &mut marks.bottom]
@@ -51,7 +51,7 @@ impl State {
for form in self.pdf_forms.values_mut() { for form in self.pdf_forms.values_mut() {
if let FormContents::Pending(body) = &mut form.contents { if let FormContents::Pending(body) = &mut form.contents {
let mut mapped = body.as_ref().clone(); let mut mapped = body.as_ref().clone();
mapped.box_node = relocation.relocate_box(&mapped.box_node)?; relocation.relocate_box(&mut mapped.box_node)?;
relocate_tokens(&mut mapped.attributes, relocation)?; relocate_tokens(&mut mapped.attributes, relocation)?;
relocate_tokens(&mut mapped.resources, relocation)?; relocate_tokens(&mut mapped.resources, relocation)?;
if mapped != **body { if mapped != **body {
@@ -70,7 +70,7 @@ impl State {
Saved::Hook(Some(tokens)) | Saved::XeInterCharTokens(Some(tokens)) => { Saved::Hook(Some(tokens)) | Saved::XeInterCharTokens(Some(tokens)) => {
*tokens = relocation.relocate_tokens(tokens)?; *tokens = relocation.relocate_tokens(tokens)?;
} }
Saved::Box(Some(value)) => *value = relocation.relocate_box(value)?, Saved::Box(Some(value)) => relocation.relocate_box(value)?,
Saved::Command(_) Saved::Command(_)
| Saved::Tokens(None) | Saved::Tokens(None)
| Saved::Hook(None) | Saved::Hook(None)
+20 -1
View File
@@ -18,6 +18,24 @@
| W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 | | W6 会话 | 实施中 | 共享库/CLI 与当前原生文字/数学/规则来源进入公共结果;真实 A4 副本九类/27 revision 与独立干净作业页面/PDF/aux/映射一致,新增数学场景独立参考通过;详细失败/捕获重放及宿主/离线/CLI观察已通过;原子项目版本/已知生成初态及TeX流记录已接通;原样 12 页 NeurIPS 的中部编辑与恢复已验证前后缀复用、仅更新一页,且与独立全量一致。全部副作用/交叉编辑、旧来源与正式性能门禁仍待闭合 |
| W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 | | W7 总验收 | 未满足 | 原有和新增测试、原样项目、格式与会话门禁全部通过才能结项 |
## 2026-12-01 减少后缀拼接的复制
发布事件列表按最终长度一次分配。来源换算直接更新已有的草稿节点,
共享列表根据实际坐标变更决定是否创建新副本。嵌套缓存命中仍传递
来源变化,同一对象在保留的状态和事件中继续共享。换算失败时
丢弃本次拼接并继续执行。
原样 12 页 NeurIPS 的单字节插入和删除各执行 646,780 步,
跳过 4,634,847 步后缀。两次均只更新第 3 页,复用其余 11 页。
第 4 页的字形来源分别向前或向后移动 1 字节。执行状态、PDF、
SVG、来源映射、资源与流、aux、out 和 log 与独立全量一致。
两次 PDF 和第 3 页 SVG 与本轮对照版逐字节一致。
本轮观察到插入和删除的后缀拼接分别用时 400 和 269 ms。
并行构建影响了同轮基线的时延,现有采样仍需在固定条件下复验。
四项来源换算单测和八项管线回归通过。实跑日志、两组独立全量
对照与 PDF/SVG 指纹位于 `target/p4-suffix-draft/`。
## 2026-12-01 引导盒重复绘制的 PDF 链接 ## 2026-12-01 引导盒重复绘制的 PDF 链接
pdfTeX 把链接的声明尺寸和绘制后的矩形存入同一节点。引导盒的 pdfTeX 把链接的声明尺寸和绘制后的矩形存入同一节点。引导盒的
@@ -30,7 +48,8 @@ pdfTeX 把链接的声明尺寸和绘制后的矩形存入同一节点。引导
全部 21 个注释的矩形逐项一致。样例覆盖普通盒副本、平铺内多个链接、 全部 21 个注释的矩形逐项一致。样例覆盖普通盒副本、平铺内多个链接、
放不下的图案、不同对齐位置与水平和垂直嵌套,以及宽度自动延伸的 放不下的图案、不同对齐位置与水平和垂直嵌套,以及宽度自动延伸的
链接。旧原生在第 1 页给三个注释分别写入不同矩形,复现日志与参考 链接。旧原生在第 1 页给三个注释分别写入不同矩形,复现日志与参考
PDF 保存在 `target/repeated-leader-links/`。四项引导盒回归通过。 PDF 保存在 `target/repeated-leader-links/`。四项引导盒回归和
已有的运行链接矩阵变换回归通过。
参考 PDF 的 SHA-256 为 参考 PDF 的 SHA-256 为
`a33ca51a7c6c179e91f5edb6699b378f6adaf73c81f8594a0713af10bcf854ce`。 `a33ca51a7c6c179e91f5edb6699b378f6adaf73c81f8594a0713af10bcf854ce`。