Preserve read evidence and box accounting in settled suffix reuse

This commit is contained in:
gloamxun committed 2026-12-02 00:47:50 +08:00
1 parent 473b3beace
commit f78aff25c5
12 files changed
+381 -46

No files matched your search

@@ -171,6 +171,32 @@ impl StepOffset {
}
}
/// Translate the cumulative, saturating count of box node visits.
#[derive(Clone, Copy)]
pub(super) struct BoxNodeOffset {
previous: usize,
current: usize,
}
impl BoxNodeOffset {
pub(super) fn new(previous: usize, current: usize) -> Self {
Self { previous, current }
}
pub(super) fn is_identity(self) -> bool {
self.previous == self.current
}
pub(super) fn apply(self, nodes: usize) -> Option<usize> {
let suffix_nodes = nodes.checked_sub(self.previous)?;
// A saturated donor does not reveal how much work would survive a decrease.
if self.current < self.previous && nodes == usize::MAX {
return None;
}
Some(self.current.saturating_add(suffix_nodes))
}
}
impl StatisticsReport {
pub(super) fn new(phase: StatisticsPhase, destination: WriteDestination, span: Span) -> Self {
let text = phase.text();
@@ -224,13 +250,26 @@ impl StatisticsReport {
}
pub fn with_step_offset(&self, offset: StepOffset) -> Option<Self> {
self.with_execution_offsets(offset, BoxNodeOffset::new(0, 0))
}
pub(super) fn with_execution_offsets(
&self,
steps: StepOffset,
boxes: BoxNodeOffset,
) -> Option<Self> {
let translate = |usage: &mut ResourceUsage| {
usage.steps = steps.apply(usage.steps)?;
usage.box_nodes_created = boxes.apply(usage.box_nodes_created)?;
Some(())
};
let mut phase = self.phase.clone();
match &mut phase {
StatisticsPhase::Shipout { before, after } => {
before.steps = offset.apply(before.steps)?;
after.steps = offset.apply(after.steps)?;
translate(before)?;
translate(after)?;
}
StatisticsPhase::End { usage, .. } => usage.steps = offset.apply(usage.steps)?,
StatisticsPhase::End { usage, .. } => translate(usage)?,
}
let report = Self::new(phase, self.destination, self.span);
report.within_diagnostic_budget().then_some(report)
@@ -616,6 +655,43 @@ impl Collector {
mod tests {
use super::*;
#[test]
fn box_node_offsets_require_an_exact_saturating_count() {
let max = usize::MAX;
assert_eq!(BoxNodeOffset::new(10, 12).apply(17), Some(19));
assert_eq!(BoxNodeOffset::new(12, 10).apply(17), Some(15));
assert_eq!(BoxNodeOffset::new(12, 10).apply(11), None);
assert_eq!(BoxNodeOffset::new(10, 12).apply(max - 1), Some(max));
assert_eq!(BoxNodeOffset::new(10, 12).apply(max), Some(max));
assert_eq!(BoxNodeOffset::new(10, 10).apply(max), Some(max));
assert_eq!(BoxNodeOffset::new(12, 10).apply(max), None);
}
#[test]
fn statistics_translate_box_counts_without_changing_the_transcript() {
let usage = |box_nodes_created| ResourceUsage {
box_nodes_created,
token_limit: 4096,
..ResourceUsage::default()
};
let translate = |old, expected| {
let report = StatisticsReport::new(old, WriteDestination::Log, Span::default());
let updated = report
.with_execution_offsets(StepOffset::new(0, 0), BoxNodeOffset::new(2, 7))
.unwrap();
assert_eq!(updated.phase, expected);
assert_eq!(updated.text, report.text);
};
translate(
StatisticsPhase::Shipout { before: usage(5), after: usage(8) },
StatisticsPhase::Shipout { before: usage(10), after: usage(13) },
);
translate(
StatisticsPhase::End { success: true, usage: usage(8) },
StatisticsPhase::End { success: true, usage: usage(13) },
);
}
#[test]
fn history_equivalence_keeps_aliases_that_cross_the_boundary() {
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());