Reduce and measure native suffix convergence work

This commit is contained in:
gloamxun committed 2026-12-01 23:15:17 +08:00
1 parent c008c52964
commit b081c951af
10 files changed
+330 -97

No files matched your search

@@ -181,6 +181,37 @@ impl Executor {
}
}
fn collect_event_usage_pair(
current: &ExecutionEvents,
prior: &ExecutionEvents,
mut history: Collector,
) -> (Collector, Collector) {
// Identical sealed chunks also own the same allocations. Copy the
// accounting state at this boundary so each tail still deduplicates
// its references against the whole prefix.
let shared = current
.prefix
.iter()
.zip(&prior.prefix)
.take_while(|(a, b)| Arc::ptr_eq(a, b))
.count();
Self::collect_event_usage(
current.prefix[..shared].iter().flat_map(|chunk| chunk.iter()),
&mut history,
);
let mut old_history = history.clone();
for (events, counts) in [(current, &mut history), (prior, &mut old_history)] {
Self::collect_event_usage(
events.prefix[shared..]
.iter()
.flat_map(|chunk| chunk.iter())
.chain(&events.tail),
counts,
);
}
(history, old_history)
}
pub(super) fn resize_entry_statistics(
&mut self,
previous: usize,
@@ -219,10 +250,8 @@ impl Executor {
}
[Collector::for_capacity, Collector::default].into_iter().all(|collector| {
let mut history = collector();
let mut old_history = collector();
Self::collect_event_usage(self.events.iter(), &mut history);
Self::collect_event_usage(old.events.iter(), &mut old_history);
let (history, old_history) =
Self::collect_event_usage_pair(&self.events, &old.events, collector());
let mut live = self.collect_live_usage(None, None, collector().tracking_aliases());
let old_live = old.collect_live_usage(None, None, collector().tracking_aliases());
// The splice translates steps separately and checks the job limit.
@@ -307,3 +336,45 @@ impl Executor {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shared_event_prefix_keeps_tail_deduplication_and_membership() {
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
let shared: Arc<[Token]> = Arc::from([token.clone()]);
let independent: Arc<[Token]> = Arc::from([token]);
let mark = |tokens| {
Event::VerticalMaterial(vec![NativeVerticalNode::Mark(NativeMark {
class: 0,
tokens,
span: Span::default(),
})])
};
let mut current = ExecutionEvents::default();
current.push(mark(shared.clone()));
current.seal_prefix();
let mut prior = current.clone();
current.push(mark(shared.clone()));
prior.push(mark(independent.clone()));
for collector in [Collector::for_capacity, Collector::default] {
let (history, old_history) =
Executor::collect_event_usage_pair(&current, &prior, collector());
assert_eq!(history.usage.token_list_cells, 1);
assert_eq!(old_history.usage.token_list_cells, 2);
let mut live = collector().tracking_aliases();
live.shared_tokens(&shared);
live.shared_tokens(&independent);
for (events, paired) in [(&current, history), (&prior, old_history)] {
let mut full = collector();
Executor::collect_event_usage(events.iter(), &mut full);
assert!(live.has_equivalent_history(&live, &paired, &full));
}
}
}
}