Prove suffix reuse across unequal retained histories
This commit is contained in:
1 parent
f78aff25c5
commit
1ff049737f
9 files changed
+781
-153
No files matched your search
@@ -197,6 +197,80 @@ impl BoxNodeOffset {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, PartialEq, Eq)]
|
||||
struct RetainedHistoryUsage {
|
||||
material: MaterialUsage,
|
||||
events: usize,
|
||||
shipouts: usize,
|
||||
token_list_cells: usize,
|
||||
}
|
||||
|
||||
impl RetainedHistoryUsage {
|
||||
fn take_from(usage: &mut ResourceUsage) -> Self {
|
||||
Self {
|
||||
material: std::mem::take(&mut usage.retained_material),
|
||||
events: std::mem::take(&mut usage.retained_events),
|
||||
shipouts: std::mem::take(&mut usage.retained_shipouts),
|
||||
token_list_cells: std::mem::take(&mut usage.token_list_cells),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The contribution of the replaced prefix to the final resource report.
|
||||
#[derive(Clone, Default)]
|
||||
pub(super) struct RetainedHistoryOffset {
|
||||
previous: RetainedHistoryUsage,
|
||||
current: RetainedHistoryUsage,
|
||||
}
|
||||
|
||||
impl RetainedHistoryOffset {
|
||||
pub(super) fn is_identity(&self) -> bool {
|
||||
self.previous == self.current
|
||||
}
|
||||
|
||||
fn replace_prefix(&self, usage: &mut ResourceUsage) -> Option<()> {
|
||||
fn replace(total: usize, previous: usize, current: usize) -> Option<usize> {
|
||||
total.checked_sub(previous)?.checked_add(current)
|
||||
}
|
||||
let previous = &self.previous;
|
||||
let current = &self.current;
|
||||
let material = &usage.retained_material;
|
||||
let old = &previous.material;
|
||||
let new = ¤t.material;
|
||||
let material = MaterialUsage {
|
||||
boxes: replace(material.boxes, old.boxes, new.boxes)?,
|
||||
horizontal_nodes: replace(material.horizontal_nodes, old.horizontal_nodes, new.horizontal_nodes)?,
|
||||
vertical_nodes: replace(material.vertical_nodes, old.vertical_nodes, new.vertical_nodes)?,
|
||||
math_nodes: replace(material.math_nodes, old.math_nodes, new.math_nodes)?,
|
||||
geometry_items: replace(material.geometry_items, old.geometry_items, new.geometry_items)?,
|
||||
diagnostic_nodes: replace(material.diagnostic_nodes, old.diagnostic_nodes, new.diagnostic_nodes)?,
|
||||
ligature_work_items: replace(material.ligature_work_items, old.ligature_work_items, new.ligature_work_items)?,
|
||||
};
|
||||
// The final report formats the aggregate with MaterialUsage::records.
|
||||
[
|
||||
material.boxes,
|
||||
material.horizontal_nodes,
|
||||
material.vertical_nodes,
|
||||
material.math_nodes,
|
||||
material.geometry_items,
|
||||
material.diagnostic_nodes,
|
||||
material.ligature_work_items,
|
||||
]
|
||||
.into_iter()
|
||||
.try_fold(0usize, usize::checked_add)?;
|
||||
let events = replace(usage.retained_events, previous.events, current.events)?;
|
||||
let shipouts = replace(usage.retained_shipouts, previous.shipouts, current.shipouts)?;
|
||||
let tokens = replace(
|
||||
usage.token_list_cells, previous.token_list_cells, current.token_list_cells,
|
||||
)?;
|
||||
usage.retained_material = material;
|
||||
usage.retained_events = events;
|
||||
usage.retained_shipouts = shipouts;
|
||||
usage.token_list_cells = tokens;
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
|
||||
impl StatisticsReport {
|
||||
pub(super) fn new(phase: StatisticsPhase, destination: WriteDestination, span: Span) -> Self {
|
||||
let text = phase.text();
|
||||
@@ -227,26 +301,9 @@ impl StatisticsReport {
|
||||
/// Recompute a report for a verified suffix after the initial entry read
|
||||
/// changed size. The caller authenticates the entry scanner's lifetime.
|
||||
pub fn with_suffix_entry_resize(&self, previous: usize, current: usize) -> Option<Self> {
|
||||
let mut phase = self.phase.clone();
|
||||
match &mut phase {
|
||||
StatisticsPhase::Shipout { before, after } => {
|
||||
for usage in [before, after] {
|
||||
let active = usage.mouth_payload_bytes == previous;
|
||||
if !active && usage.mouth_payload_bytes != 0 {
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, active)?;
|
||||
}
|
||||
}
|
||||
StatisticsPhase::End { usage, .. } => {
|
||||
if usage.stacks.input_frames != 0 || usage.mouth_payload_bytes != 0 {
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, false)?;
|
||||
}
|
||||
}
|
||||
let report = Self::new(phase, self.destination, self.span);
|
||||
report.within_diagnostic_budget().then_some(report)
|
||||
self.with_suffix_adjustments(
|
||||
StepOffset::new(0, 0), BoxNodeOffset::new(0, 0), Some((previous, current)), None,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn with_step_offset(&self, offset: StepOffset) -> Option<Self> {
|
||||
@@ -257,6 +314,16 @@ impl StatisticsReport {
|
||||
&self,
|
||||
steps: StepOffset,
|
||||
boxes: BoxNodeOffset,
|
||||
) -> Option<Self> {
|
||||
self.with_suffix_adjustments(steps, boxes, None, None)
|
||||
}
|
||||
|
||||
pub(super) fn with_suffix_adjustments(
|
||||
&self,
|
||||
steps: StepOffset,
|
||||
boxes: BoxNodeOffset,
|
||||
entry_sizes: Option<(usize, usize)>,
|
||||
history: Option<&RetainedHistoryOffset>,
|
||||
) -> Option<Self> {
|
||||
let translate = |usage: &mut ResourceUsage| {
|
||||
usage.steps = steps.apply(usage.steps)?;
|
||||
@@ -266,10 +333,32 @@ impl StatisticsReport {
|
||||
let mut phase = self.phase.clone();
|
||||
match &mut phase {
|
||||
StatisticsPhase::Shipout { before, after } => {
|
||||
translate(before)?;
|
||||
translate(after)?;
|
||||
if history.is_some_and(|offset| !offset.is_identity()) {
|
||||
return None;
|
||||
}
|
||||
for usage in [before, after] {
|
||||
translate(usage)?;
|
||||
if let Some((previous, current)) = entry_sizes {
|
||||
let active = usage.mouth_payload_bytes == previous;
|
||||
if !active && usage.mouth_payload_bytes != 0 {
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, active)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
StatisticsPhase::End { usage, .. } => {
|
||||
translate(usage)?;
|
||||
if let Some((previous, current)) = entry_sizes {
|
||||
if usage.stacks.input_frames != 0 || usage.mouth_payload_bytes != 0 {
|
||||
return None;
|
||||
}
|
||||
usage.resize_entry_payload(previous, current, false)?;
|
||||
}
|
||||
if let Some(offset) = history {
|
||||
offset.replace_prefix(usage)?;
|
||||
}
|
||||
}
|
||||
StatisticsPhase::End { usage, .. } => translate(usage)?,
|
||||
}
|
||||
let report = Self::new(phase, self.destination, self.span);
|
||||
report.within_diagnostic_budget().then_some(report)
|
||||
@@ -431,19 +520,47 @@ impl Collector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check the live allocation correspondence and each allocation's
|
||||
/// membership in the retained history. Counting a later event can
|
||||
/// encounter older storage only through these surviving references.
|
||||
pub(super) fn has_equivalent_history(
|
||||
&self,
|
||||
other: &Self,
|
||||
history: &Self,
|
||||
old_history: &Self,
|
||||
) -> bool {
|
||||
history.usage == old_history.usage && self.same_live_history(other, history, old_history)
|
||||
}
|
||||
|
||||
/// Compare separately collected live state and retained events. The
|
||||
/// caller must prove that the suffix cannot recount capacity.
|
||||
pub(super) fn retained_history_offset(
|
||||
&self,
|
||||
other: &Self,
|
||||
history: &Self,
|
||||
old_history: &Self,
|
||||
) -> Option<RetainedHistoryOffset> {
|
||||
if self.material_only() || other.material_only()
|
||||
|| history.material_only() || old_history.material_only()
|
||||
|| !self.same_live_history(other, history, old_history)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let mut usage = history.usage.clone();
|
||||
let mut old_usage = old_history.usage.clone();
|
||||
let current = RetainedHistoryUsage::take_from(&mut usage);
|
||||
let previous = RetainedHistoryUsage::take_from(&mut old_usage);
|
||||
(usage == old_usage).then_some(RetainedHistoryOffset { previous, current })
|
||||
}
|
||||
|
||||
/// Later events can reach old storage only through allocations that
|
||||
/// survive in live state. Keep both their alias order and history membership.
|
||||
fn same_live_history(
|
||||
&self,
|
||||
other: &Self,
|
||||
history: &Self,
|
||||
old_history: &Self,
|
||||
) -> bool {
|
||||
let a = &self.aliases.as_ref().expect("live alias traversal").visits;
|
||||
let b = &other.aliases.as_ref().expect("live alias traversal").visits;
|
||||
self.usage == other.usage
|
||||
&& history.usage == old_history.usage
|
||||
&& a.len() == b.len()
|
||||
&& a.iter().zip(b).all(|(&(new, ni), &(old, oi))| {
|
||||
ni == oi
|
||||
@@ -692,6 +809,108 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suffix_adjustments_check_only_the_final_diagnostic_budget() {
|
||||
let report = |usage| {
|
||||
StatisticsReport::new(
|
||||
StatisticsPhase::End { success: true, usage },
|
||||
WriteDestination::Log,
|
||||
Span::default(),
|
||||
)
|
||||
};
|
||||
let mut usage = ResourceUsage {
|
||||
steps: 9,
|
||||
source_payload_bytes: 10,
|
||||
..ResourceUsage::default()
|
||||
};
|
||||
usage.token_limit = report(usage.clone()).text.len();
|
||||
let original = report(usage.clone());
|
||||
let steps = StepOffset::new(0, 1);
|
||||
assert!(original.with_step_offset(steps).is_none());
|
||||
|
||||
usage.steps = 10;
|
||||
usage.source_payload_bytes = 9;
|
||||
assert_eq!(
|
||||
original.with_suffix_adjustments(
|
||||
steps, BoxNodeOffset::new(0, 0), Some((10, 9)), None,
|
||||
),
|
||||
Some(report(usage))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retained_history_offsets_require_shared_token_membership() {
|
||||
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||
let retained: Arc<[Token]> = Arc::from([token.clone()]);
|
||||
let old_retained: Arc<[Token]> = Arc::from([token.clone()]);
|
||||
let extra: Arc<[Token]> = Arc::from([token.clone(), token]);
|
||||
let mut live = Collector::default().tracking_aliases();
|
||||
live.shared_tokens(&retained);
|
||||
let mut old_live = Collector::default().tracking_aliases();
|
||||
old_live.shared_tokens(&old_retained);
|
||||
let mut history = Collector::default();
|
||||
history.shared_tokens(&retained);
|
||||
history.shared_tokens(&extra);
|
||||
history.usage.retained_events = 3;
|
||||
history.usage.retained_material.horizontal_nodes = 5;
|
||||
let mut old_history = Collector::default();
|
||||
old_history.shared_tokens(&old_retained);
|
||||
old_history.usage.retained_events = 2;
|
||||
old_history.usage.retained_material.horizontal_nodes = 1;
|
||||
|
||||
assert!(!live.has_equivalent_history(&old_live, &history, &old_history));
|
||||
let offset = live.retained_history_offset(&old_live, &history, &old_history).unwrap();
|
||||
assert!(!offset.is_identity());
|
||||
assert!(
|
||||
old_live.retained_history_offset(&old_live, &history, &old_history).is_none()
|
||||
);
|
||||
|
||||
let mut usage = ResourceUsage {
|
||||
token_list_cells: 8,
|
||||
retained_events: 4,
|
||||
retained_material: MaterialUsage { horizontal_nodes: 3, ..MaterialUsage::default() },
|
||||
token_limit: 4096,
|
||||
..ResourceUsage::default()
|
||||
};
|
||||
let report = StatisticsReport::new(
|
||||
StatisticsPhase::End { success: true, usage: usage.clone() },
|
||||
WriteDestination::Log,
|
||||
Span::default(),
|
||||
);
|
||||
usage.token_list_cells = 10;
|
||||
usage.retained_events = 5;
|
||||
usage.retained_material.horizontal_nodes = 7;
|
||||
assert_eq!(
|
||||
report.with_suffix_adjustments(
|
||||
StepOffset::new(0, 0), BoxNodeOffset::new(0, 0), None, Some(&offset),
|
||||
).unwrap(),
|
||||
StatisticsReport::new(
|
||||
StatisticsPhase::End { success: true, usage },
|
||||
WriteDestination::Log,
|
||||
Span::default(),
|
||||
)
|
||||
);
|
||||
let overflow = StatisticsReport::new(
|
||||
StatisticsPhase::End {
|
||||
success: true,
|
||||
usage: ResourceUsage {
|
||||
retained_material: MaterialUsage {
|
||||
boxes: usize::MAX - 1,
|
||||
horizontal_nodes: 1,
|
||||
..MaterialUsage::default()
|
||||
},
|
||||
token_limit: 4096,
|
||||
..old_history.usage
|
||||
},
|
||||
},
|
||||
WriteDestination::Log,
|
||||
Span::default(),
|
||||
);
|
||||
assert!(overflow.with_suffix_adjustments(
|
||||
StepOffset::new(0, 0), BoxNodeOffset::new(0, 0), None, Some(&offset),
|
||||
).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn history_equivalence_keeps_aliases_that_cross_the_boundary() {
|
||||
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||
|
||||
Reference in new issue
Block a user