Reduce allocation and traversal in settled suffix checks
This commit is contained in:
1 parent
1ff049737f
commit
c4003c8dad
6 files changed
+351
-66
No files matched your search
@@ -1,7 +1,7 @@
|
||||
//! Measured native storage and execution resources, not invented TeX arena words.
|
||||
use super::*;
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet, HashSet},
|
||||
collections::{BTreeSet, HashMap, HashSet, hash_map::Entry},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
@@ -456,7 +456,7 @@ pub(super) enum Ownership {
|
||||
Active,
|
||||
Retained,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum Allocation {
|
||||
Macro(usize),
|
||||
Tokens(usize, usize),
|
||||
@@ -465,22 +465,27 @@ enum Allocation {
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct AliasVisits {
|
||||
ordinals: BTreeMap<Allocation, usize>,
|
||||
ordinals: HashMap<Allocation, usize>,
|
||||
visits: Vec<(Allocation, usize)>,
|
||||
}
|
||||
|
||||
impl AliasVisits {
|
||||
fn record(&mut self, allocation: Allocation) {
|
||||
fn record(&mut self, allocation: Allocation) -> bool {
|
||||
let next = self.ordinals.len();
|
||||
let ordinal = *self.ordinals.entry(allocation).or_insert(next);
|
||||
let (ordinal, first) = match self.ordinals.entry(allocation) {
|
||||
Entry::Occupied(entry) => (*entry.get(), false),
|
||||
Entry::Vacant(entry) => (*entry.insert(next), true),
|
||||
};
|
||||
self.visits.push((allocation, ordinal));
|
||||
first
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(super) struct Collector {
|
||||
pub usage: ResourceUsage,
|
||||
// Alias visits keep traversal order separately from this membership index.
|
||||
// A tracked visit uses its ordinal index for deduplication. The
|
||||
// membership sets cover normal collection and any visits before tracking.
|
||||
macros: HashSet<usize>,
|
||||
token_lists: BTreeSet<(usize, usize)>,
|
||||
shared_lists: Option<BTreeSet<usize>>,
|
||||
@@ -495,21 +500,14 @@ impl Collector {
|
||||
}
|
||||
|
||||
pub(super) fn tracking_aliases(mut self) -> Self {
|
||||
self.aliases = Some(AliasVisits::default());
|
||||
self.aliases.get_or_insert_with(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 {
|
||||
if self.aliases.as_ref().is_some_and(|aliases| aliases.ordinals.contains_key(&allocation)) {
|
||||
return true;
|
||||
}
|
||||
match allocation {
|
||||
Allocation::Macro(address) => self.macros.contains(&address),
|
||||
Allocation::Tokens(address, len) => self.token_lists.contains(&(address, len)),
|
||||
@@ -575,8 +573,13 @@ impl Collector {
|
||||
|
||||
pub fn macro_body(&mut self, m: &Arc<Macro>) {
|
||||
let address = Arc::as_ptr(m) as usize;
|
||||
self.tracks_allocation(Allocation::Macro(address));
|
||||
if self.macros.insert(address) {
|
||||
let first = if let Some(aliases) = &mut self.aliases {
|
||||
aliases.record(Allocation::Macro(address))
|
||||
&& (self.macros.is_empty() || !self.macros.contains(&address))
|
||||
} else {
|
||||
self.macros.insert(address)
|
||||
};
|
||||
if first {
|
||||
self.usage.unique_macro_bodies += 1;
|
||||
self.usage.macro_token_cells += m.prefix.len()
|
||||
+ m.parameters.iter().map(Vec::len).sum::<usize>()
|
||||
@@ -585,11 +588,15 @@ impl Collector {
|
||||
}
|
||||
pub fn shared_tokens(&mut self, t: &Arc<[Token]>) {
|
||||
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();
|
||||
let len = t.len();
|
||||
let first = if let Some(aliases) = &mut self.aliases && len != 0 {
|
||||
aliases.record(Allocation::Tokens(address, len))
|
||||
&& !self.token_lists.contains(&(address, len))
|
||||
} else {
|
||||
self.token_lists.insert((address, len))
|
||||
};
|
||||
if first {
|
||||
self.usage.token_list_cells += len;
|
||||
}
|
||||
}
|
||||
fn material(&mut self, o: Ownership) -> &mut MaterialUsage {
|
||||
@@ -725,13 +732,15 @@ impl Collector {
|
||||
self.usage.active_material.ligature_work_items += b.ligature_work_items();
|
||||
}
|
||||
fn visit_shared<T: ?Sized>(&mut self, list: &Arc<T>) -> bool {
|
||||
let Some(seen) = &mut self.shared_lists else {
|
||||
return true;
|
||||
};
|
||||
let address = Arc::as_ptr(list).cast::<()>() as usize;
|
||||
if self.shared_lists.is_some() {
|
||||
self.tracks_allocation(Allocation::Material(address));
|
||||
if let Some(aliases) = &mut self.aliases {
|
||||
aliases.record(Allocation::Material(address)) && !seen.contains(&address)
|
||||
} else {
|
||||
seen.insert(address)
|
||||
}
|
||||
self.shared_lists
|
||||
.as_mut()
|
||||
.is_none_or(|seen| seen.insert(address))
|
||||
}
|
||||
|
||||
pub fn shared_diagnostics(
|
||||
@@ -911,6 +920,44 @@ mod tests {
|
||||
).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tracked_macro_counts_share_the_ordinal_index_without_losing_visits() {
|
||||
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||
let body = Arc::new(Macro {
|
||||
prefix: Vec::new(),
|
||||
parameters: Vec::new(),
|
||||
parameter_chars: Vec::new(),
|
||||
replacement: vec![Replacement::Token(token.clone())],
|
||||
long: false,
|
||||
outer: false,
|
||||
protected: false,
|
||||
});
|
||||
let independent = Arc::new((*body).clone());
|
||||
let tokens: Arc<[Token]> = Arc::from([token]);
|
||||
let mut history = Collector::default();
|
||||
let mut live = Collector::default().tracking_aliases();
|
||||
for collector in [&mut history, &mut live] {
|
||||
collector.macro_body(&body);
|
||||
collector.shared_tokens(&tokens);
|
||||
collector.macro_body(&body);
|
||||
collector.macro_body(&independent);
|
||||
}
|
||||
|
||||
assert_eq!(live.usage, history.usage);
|
||||
assert_eq!(live.usage.unique_macro_bodies, 2);
|
||||
assert!(live.macros.is_empty());
|
||||
assert!(live.token_lists.is_empty());
|
||||
let visits = &live.aliases.as_ref().unwrap().visits;
|
||||
assert_eq!(visits.iter().map(|(_, ordinal)| *ordinal).collect::<Vec<_>>(), [0, 1, 0, 2]);
|
||||
assert!(visits.iter().all(|(allocation, _)| {
|
||||
history.contains(*allocation) && live.contains(*allocation)
|
||||
}));
|
||||
|
||||
let mut previously_counted = history.clone().tracking_aliases();
|
||||
previously_counted.macro_body(&body);
|
||||
assert_eq!(previously_counted.usage, history.usage);
|
||||
}
|
||||
|
||||
#[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