Retain shared material across edited native page checkpoints
This commit is contained in:
1 parent
c42a1a72aa
commit
9a6c162b46
5 files changed
+251
-17
No files matched your search
@@ -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<Self> + '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<impl Fn(&Self) -> Option<Self> + '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(
|
||||
|
||||
@@ -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<usize, Arc<Vec<TextNode>>>,
|
||||
vertical: BTreeMap<usize, Arc<Vec<NativeVerticalNode>>>,
|
||||
diagnostic: BTreeMap<usize, Arc<[DiagnosticNode]>>,
|
||||
marks: BTreeMap<usize, Arc<[Token]>>,
|
||||
}
|
||||
|
||||
fn reuse_list<T: Eq + ?Sized>(
|
||||
old: &Arc<T>,
|
||||
new: &mut Arc<T>,
|
||||
reused: &mut BTreeMap<usize, Arc<T>>,
|
||||
) -> Option<usize> {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user