Share common macro accounting and reduce command checkpoint copies

This commit is contained in:
gloamxun committed 2026-12-02 03:14:10 +08:00
1 parent f558bf43ea
commit d74933348d
6 files changed
+129 -26

No files matched your search

+117 -15
View File
@@ -804,7 +804,9 @@ enum PdftexRevision {
R24,
}
const COMMAND_SHARDS: usize = 32;
// A single definition after a checkpoint copies its shard. Smaller shards
// keep that copy proportional to the modified part of the command table.
const COMMAND_SHARDS: usize = 128;
type CommandEntries = HashMap<Arc<CommandName>, Meaning, RandomState>;
#[derive(Clone, Debug)]
@@ -1222,7 +1224,7 @@ fn paired_statistics_keep_aliases_from_changed_to_shared_command_shards() {
let second = CommandName::Control("other-common-definition".into());
let shard = |name: &CommandName| CommandBindings::shard_index(command_hasher().hash_one(name));
let shared_shards = [shard(&first), shard(&second)];
let edited = (0..128)
let edited = (0..COMMAND_SHARDS)
.map(|index| CommandName::Control(format!("edited-alias-{index}")))
.find(|name| !shared_shards.contains(&shard(name)))
.unwrap();
@@ -1248,6 +1250,83 @@ fn paired_statistics_keep_aliases_from_changed_to_shared_command_shards() {
assert!(same.has_equivalent_history(&old_counts, &history, &history));
}
#[test]
fn paired_statistics_keep_aliases_within_changed_command_shards() {
use super::statistics::Collector;
let first_body = Arc::new(Macro {
prefix: vec![TokenKind::Character('P', CatCode::Letter)],
parameters: Vec::new(),
parameter_chars: Vec::new(),
replacement: Vec::new(),
long: false,
outer: false,
protected: false,
});
let second_body = Arc::new(first_body.as_ref().clone());
let mut names_by_shard: [Vec<CommandName>; COMMAND_SHARDS] = std::array::from_fn(|_| Vec::new());
// At least one shard gets nine names, regardless of the process hash seed.
for index in 0..=8 * COMMAND_SHARDS {
let name = CommandName::Control(format!("paired-binding-{index}"));
let shard = CommandBindings::shard_index(command_hasher().hash_one(&name));
names_by_shard[shard].push(name);
}
let (index, names) = names_by_shard
.into_iter()
.enumerate()
.find(|(_, names)| names.len() >= 9)
.unwrap();
let mut prior = State::default();
let [first, second, edited, rest @ ..] = names.as_slice() else {
unreachable!();
};
prior.define(first.clone(), Meaning::Macro(first_body.clone()), true);
prior.define(second.clone(), Meaning::Macro(second_body.clone()), true);
prior.define(edited.clone(), Meaning::Macro(first_body), true);
for name in rest {
prior.define(name.clone(), Meaning::Count(0), true);
}
let original = &prior.commands.shards[index];
let mut matching = prior.clone();
let matching_shard = Arc::make_mut(&mut matching.command_bindings_mut().shards[index]);
assert!(original.entries.keys().eq(matching_shard.entries.keys()));
let mut reordered = matching.clone();
let changed = Arc::make_mut(&mut reordered.command_bindings_mut().shards[index]);
for capacity in [32, 64, 128, 256, 512, 1024] {
changed.entries.reserve(capacity);
if !original.entries.keys().eq(changed.entries.keys()) {
break;
}
}
assert!(!original.entries.keys().eq(changed.entries.keys()));
let history = Collector::default();
let counts = |state: &State| {
let (live, old_live) = state
.collect_compared_statistics(&prior, Collector::default().tracking_aliases())
.unwrap();
let mut expected = Collector::default();
state.collect_statistics(&mut expected);
assert_eq!(live.usage, expected.usage);
assert_eq!(live.usage, old_live.usage);
live.has_equivalent_history(&old_live, &history, &history)
};
for (mut current, expect_same_order) in [(matching, true), (reordered, false)] {
assert!(!Arc::ptr_eq(original, &current.commands.shards[index]));
assert_eq!(current, prior);
assert!(counts(&current));
let changed = Arc::make_mut(&mut current.command_bindings_mut().shards[index]);
*changed.entries.get_mut(edited).unwrap() = Meaning::Macro(second_body.clone());
assert_eq!(original.entries.keys().eq(changed.entries.keys()), expect_same_order);
assert_eq!(current, prior);
assert!(!counts(&current));
}
}
#[test]
fn command_shard_equality_checks_values_in_matching_bucket_order() {
let mut before = CommandShard::new(command_hasher().clone());
@@ -1823,9 +1902,9 @@ impl State {
let mut old_usage = if current_usage.material_only() {
current_usage.clone()
} else {
let pairs = self.commands.shards.iter().zip(&old.commands.shards);
current_usage.usage.state.command_entries = old.commands.len();
for (current, prior) in pairs.clone() {
let mut changed_macros = Vec::new();
for (current, prior) in self.commands.shards.iter().zip(&old.commands.shards) {
if current.entries.len() != prior.entries.len() {
return None;
}
@@ -1833,21 +1912,44 @@ impl State {
for (name, meaning) in &prior.entries {
Self::collect_command_statistics(&mut current_usage, name, meaning);
}
continue;
}
let mut ordered = Some(current.entries.iter());
for (name, old_meaning) in &prior.entries {
let meaning = match ordered.as_mut().and_then(Iterator::next) {
Some((current_name, meaning)) if name == current_name => meaning,
_ => {
ordered = None;
current.entries.get(name)?
}
};
if let CommandName::Control(name) = name.as_ref() {
current_usage.usage.state.control_name_bytes += name.len();
}
match (meaning, old_meaning) {
(Meaning::Macro(body), Meaning::Macro(old_body)) => {
current_usage.usage.state.macro_bindings += 1;
if Arc::ptr_eq(body, old_body) {
current_usage.macro_body(body);
} else {
changed_macros.push((body, old_body));
}
}
// Settled suffixes already require equal live meanings.
(Meaning::Macro(_), _) | (_, Meaning::Macro(_)) => return None,
_ => {}
}
}
}
// Both alias walks start with the exact same live allocations.
// Copy their ordinals before counting the remaining bindings so
// a later alias to a shared macro still has the original ordinal.
// The same permutation on both sides leaves the alias proof
// intact. Record common allocations first so a later reference
// to any of them still has the original ordinal on both sides.
let mut old_usage = current_usage.clone();
for (current, prior) in pairs {
if !Arc::ptr_eq(current, prior) {
for (name, old_meaning) in &prior.entries {
let meaning = current.entries.get(name)?;
Self::collect_command_statistics(&mut current_usage, name, meaning);
Self::collect_command_statistics(&mut old_usage, name, old_meaning);
}
}
for (body, old_body) in changed_macros {
current_usage.macro_body(body);
old_usage.macro_body(old_body);
}
old_usage
};