Verify retained TeX accounting before reusing executed suffixes
This commit is contained in:
1 parent
0117449023
commit
57ce7116ad
8 files changed
+236
-15
No files matched your search
@@ -1,6 +1,9 @@
|
||||
//! Measured native storage and execution resources, not invented TeX arena words.
|
||||
use super::*;
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MaterialUsage {
|
||||
@@ -147,12 +150,34 @@ pub(super) enum Ownership {
|
||||
Active,
|
||||
Retained,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
enum Allocation {
|
||||
Macro(usize),
|
||||
Tokens(usize, usize),
|
||||
Material(usize),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct AliasVisits {
|
||||
ordinals: BTreeMap<Allocation, usize>,
|
||||
visits: Vec<(Allocation, usize)>,
|
||||
}
|
||||
|
||||
impl AliasVisits {
|
||||
fn record(&mut self, allocation: Allocation) {
|
||||
let next = self.ordinals.len();
|
||||
let ordinal = *self.ordinals.entry(allocation).or_insert(next);
|
||||
self.visits.push((allocation, ordinal));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Collector {
|
||||
pub usage: ResourceUsage,
|
||||
macros: BTreeSet<usize>,
|
||||
token_lists: BTreeSet<(usize, usize)>,
|
||||
shared_lists: Option<BTreeSet<usize>>,
|
||||
aliases: Option<AliasVisits>,
|
||||
}
|
||||
impl Collector {
|
||||
pub fn for_capacity() -> Self {
|
||||
@@ -162,12 +187,61 @@ impl Collector {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn tracking_aliases(mut self) -> Self {
|
||||
self.aliases = Some(AliasVisits::default());
|
||||
self
|
||||
}
|
||||
|
||||
pub(super) fn tracks_aliases(&self) -> bool {
|
||||
self.aliases.is_some()
|
||||
}
|
||||
|
||||
fn tracks_allocation(&mut self, allocation: Allocation) {
|
||||
if let Some(aliases) = &mut self.aliases {
|
||||
aliases.record(allocation);
|
||||
}
|
||||
}
|
||||
|
||||
fn contains(&self, allocation: Allocation) -> bool {
|
||||
match allocation {
|
||||
Allocation::Macro(address) => self.macros.contains(&address),
|
||||
Allocation::Tokens(address, len) => self.token_lists.contains(&(address, len)),
|
||||
Allocation::Material(address) => self
|
||||
.shared_lists
|
||||
.as_ref()
|
||||
.is_some_and(|lists| lists.contains(&address)),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
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
|
||||
&& std::mem::discriminant(&new) == std::mem::discriminant(&old)
|
||||
&& history.contains(new) == old_history.contains(old)
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn material_only(&self) -> bool {
|
||||
self.shared_lists.is_some()
|
||||
}
|
||||
|
||||
pub fn macro_body(&mut self, m: &Arc<Macro>) {
|
||||
if self.macros.insert(Arc::as_ptr(m) as usize) {
|
||||
let address = Arc::as_ptr(m) as usize;
|
||||
self.tracks_allocation(Allocation::Macro(address));
|
||||
if self.macros.insert(address) {
|
||||
self.usage.unique_macro_bodies += 1;
|
||||
self.usage.macro_token_cells += m.prefix.len()
|
||||
+ m.parameters.iter().map(Vec::len).sum::<usize>()
|
||||
@@ -175,7 +249,11 @@ impl Collector {
|
||||
}
|
||||
}
|
||||
pub fn shared_tokens(&mut self, t: &Arc<[Token]>) {
|
||||
if self.token_lists.insert((t.as_ptr() as usize, t.len())) {
|
||||
let address = t.as_ptr() as usize;
|
||||
if !t.is_empty() {
|
||||
self.tracks_allocation(Allocation::Tokens(address, t.len()));
|
||||
}
|
||||
if self.token_lists.insert((address, t.len())) {
|
||||
self.usage.token_list_cells += t.len();
|
||||
}
|
||||
}
|
||||
@@ -312,9 +390,13 @@ impl Collector {
|
||||
self.usage.active_material.ligature_work_items += b.ligature_work_items();
|
||||
}
|
||||
fn visit_shared<T: ?Sized>(&mut self, list: &Arc<T>) -> bool {
|
||||
self.shared_lists.as_mut().is_none_or(|seen| {
|
||||
seen.insert(Arc::as_ptr(list).cast::<()>() as usize)
|
||||
})
|
||||
let address = Arc::as_ptr(list).cast::<()>() as usize;
|
||||
if self.shared_lists.is_some() {
|
||||
self.tracks_allocation(Allocation::Material(address));
|
||||
}
|
||||
self.shared_lists
|
||||
.as_mut()
|
||||
.is_none_or(|seen| seen.insert(address))
|
||||
}
|
||||
|
||||
pub fn shared_diagnostics(
|
||||
@@ -350,3 +432,32 @@ impl Collector {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn history_equivalence_keeps_aliases_that_cross_the_boundary() {
|
||||
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||
let first: Arc<[Token]> = Arc::from([token.clone()]);
|
||||
let second: Arc<[Token]> = Arc::from([token.clone()]);
|
||||
let unrelated: Arc<[Token]> = Arc::from([token]);
|
||||
let collect = |tokens: &Arc<[Token]>, tracker: Collector| {
|
||||
let mut collector = tracker;
|
||||
collector.shared_tokens(tokens);
|
||||
collector
|
||||
};
|
||||
|
||||
let before = collect(&first, Collector::default());
|
||||
let after = collect(&second, Collector::default());
|
||||
assert_eq!(before.usage, after.usage);
|
||||
let live = collect(&first, Collector::default().tracking_aliases());
|
||||
let independent = collect(&second, Collector::default().tracking_aliases());
|
||||
assert!(live.has_equivalent_history(&independent, &before, &after));
|
||||
|
||||
let detached = collect(&unrelated, Collector::default().tracking_aliases());
|
||||
assert!(detached.has_equivalent_history(&detached, &before, &after));
|
||||
assert!(!live.has_equivalent_history(&detached, &before, &after));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user