Avoid extra copies while assembling relocated TeX suffixes
This commit is contained in:
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
|
||||
// from a live checkpoint, whose original shared allocations stay intact.
|
||||
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 {
|
||||
format_dumped: completed.format_dumped,
|
||||
format_pdf_images: completed.format_pdf_images.clone(),
|
||||
@@ -229,7 +231,7 @@ impl EntryCheckpoint {
|
||||
native_page: completed.native_page.clone(),
|
||||
split_discards: completed.split_discards.clone(),
|
||||
after_assignment: completed.after_assignment.clone(),
|
||||
events: current.events.clone().into_vec(),
|
||||
events,
|
||||
state: completed.state.clone(),
|
||||
io: completed.io.clone(),
|
||||
input_position: completed.input_position,
|
||||
|
||||
@@ -75,11 +75,11 @@ pub(super) fn same_kernel_provenance(
|
||||
let mut discards = prior.split_discards.clone();
|
||||
let mut native_page = prior.native_page.clone();
|
||||
let mut pending_shipouts = prior.pending_shipouts.clone();
|
||||
relocate_vertical(&mut vertical.nodes, relocation)?;
|
||||
relocate_vertical(&mut discards, relocation)?;
|
||||
relocation.relocate_vertical(&mut vertical.nodes)?;
|
||||
relocation.relocate_vertical(&mut discards)?;
|
||||
relocate_page(&mut native_page, relocation)?;
|
||||
for value in &mut pending_shipouts {
|
||||
*value = relocation.relocate_box(value)?;
|
||||
relocation.relocate_box(value)?;
|
||||
}
|
||||
if current.generated_inputs != generated
|
||||
|| current.conditionals != conditionals
|
||||
@@ -130,7 +130,7 @@ pub(super) fn append_suffix(
|
||||
}
|
||||
execution.state.relocate_source(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 {
|
||||
relocation.relocate_token(token)?;
|
||||
}
|
||||
@@ -156,10 +156,10 @@ fn relocate_page(
|
||||
relocation: &mut EventRelocation<'_>,
|
||||
) -> Option<()> {
|
||||
if let Some(page) = page {
|
||||
relocate_vertical(&mut page.nodes, relocation)?;
|
||||
relocate_vertical(page.contributions.make_contiguous(), relocation)?;
|
||||
relocate_vertical(&mut page.discards, relocation)?;
|
||||
relocate_vertical(&mut page.insertions.held, relocation)?;
|
||||
relocation.relocate_vertical(&mut page.nodes)?;
|
||||
relocation.relocate_vertical(page.contributions.make_contiguous())?;
|
||||
relocation.relocate_vertical(&mut page.discards)?;
|
||||
relocation.relocate_vertical(&mut page.insertions.held)?;
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
@@ -170,13 +170,3 @@ fn relocate_tokens(tokens: &mut [Token], relocation: &mut EventRelocation<'_>) -
|
||||
}
|
||||
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> {
|
||||
map: &'a SourceRelocation,
|
||||
changed: bool,
|
||||
macros: Shared<Macro>,
|
||||
text_lists: Shared<Vec<TextNode>>,
|
||||
vertical_lists: Shared<Vec<NativeVerticalNode>>,
|
||||
@@ -32,6 +33,7 @@ impl<'a> EventRelocation<'a> {
|
||||
pub(super) fn new(map: &'a SourceRelocation) -> Self {
|
||||
Self {
|
||||
map,
|
||||
changed: false,
|
||||
macros: HashMap::new(),
|
||||
text_lists: HashMap::new(),
|
||||
vertical_lists: HashMap::new(),
|
||||
@@ -49,15 +51,12 @@ impl<'a> EventRelocation<'a> {
|
||||
self.relocated(event)
|
||||
}
|
||||
|
||||
pub(super) fn relocate_box(&mut self, packed: &NativeBox) -> Option<NativeBox> {
|
||||
self.relocated(packed)
|
||||
pub(super) fn relocate_box(&mut self, packed: &mut NativeBox) -> Option<()> {
|
||||
packed.relocate(self)
|
||||
}
|
||||
|
||||
pub(super) fn relocate_vertical(
|
||||
&mut self,
|
||||
node: &NativeVerticalNode,
|
||||
) -> Option<NativeVerticalNode> {
|
||||
self.relocated(node)
|
||||
pub(super) fn relocate_vertical(&mut self, nodes: &mut [NativeVerticalNode]) -> Option<()> {
|
||||
nodes.relocate(self)
|
||||
}
|
||||
|
||||
pub(super) fn relocate_token(&mut self, token: &mut Token) -> Option<()> {
|
||||
@@ -101,6 +100,16 @@ impl<'a> EventRelocation<'a> {
|
||||
updated.relocate(self)?;
|
||||
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 {
|
||||
@@ -133,7 +142,9 @@ impl<T: Relocate> Relocate for [T] {
|
||||
|
||||
impl Relocate for Span {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
@@ -148,11 +159,11 @@ macro_rules! relocate_shared {
|
||||
fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> {
|
||||
let original = Arc::as_ptr(self);
|
||||
if let Some(updated) = context.$cache.get(&original) {
|
||||
context.changed |= !Arc::ptr_eq(self, updated);
|
||||
*self = updated.clone();
|
||||
} else {
|
||||
let mut updated = $copy(self.as_ref());
|
||||
updated.relocate(context)?;
|
||||
if updated != **self {
|
||||
if context.relocate_tracking_changes(&mut updated)? {
|
||||
*self = Arc::from(updated);
|
||||
}
|
||||
context.$cache.insert(original, self.clone());
|
||||
@@ -187,11 +198,7 @@ macro_rules! relocate_fields {
|
||||
}
|
||||
|
||||
relocate_fields!(Token { span, invocation; kind });
|
||||
impl Relocate for TokenOrigin {
|
||||
fn relocate(&mut self, context: &mut EventRelocation<'_>) -> Option<()> {
|
||||
relocate_origin(self, context.map)
|
||||
}
|
||||
}
|
||||
relocate_fields!(TokenOrigin { span, invocation; });
|
||||
|
||||
fn relocate_origin(origin: &mut TokenOrigin, map: &SourceRelocation) -> Option<()> {
|
||||
let TokenOrigin { span, invocation } = origin;
|
||||
@@ -685,4 +692,38 @@ mod tests {
|
||||
);
|
||||
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)?;
|
||||
}
|
||||
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 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() {
|
||||
if let FormContents::Pending(body) = &mut form.contents {
|
||||
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.resources, relocation)?;
|
||||
if mapped != **body {
|
||||
@@ -70,7 +70,7 @@ impl State {
|
||||
Saved::Hook(Some(tokens)) | Saved::XeInterCharTokens(Some(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::Tokens(None)
|
||||
| Saved::Hook(None)
|
||||
|
||||
Reference in new issue
Block a user