Reuse native command meanings within a binding revision

This commit is contained in:
gloamxun committed 2026-11-29 07:03:27 +08:00
1 parent 9f4cc64c8f
commit d8af3518a7
4 files changed
+135 -32

No files matched your search

+33 -13
View File
@@ -796,12 +796,23 @@ enum PdftexRevision {
R24,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct CommandBindings(HashMap<Arc<CommandName>, Meaning>);
#[derive(Clone, Debug, Default)]
struct CommandBindings {
by_name: HashMap<Arc<CommandName>, Meaning>,
revision: u64,
}
impl PartialEq for CommandBindings {
fn eq(&self, other: &Self) -> bool {
self.by_name == other.by_name
}
}
impl Eq for CommandBindings {}
impl CommandBindings {
fn ordered(&self) -> Vec<(&Arc<CommandName>, &Meaning)> {
let mut entries: Vec<_> = self.0.iter().collect();
let mut entries: Vec<_> = self.by_name.iter().collect();
entries.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
entries
}
@@ -824,7 +835,10 @@ impl<'de> serde::Deserialize<'de> for CommandBindings {
let entries = <Vec<(Arc<CommandName>, Meaning)> as serde::Deserialize>::deserialize(
deserializer,
)?;
Ok(Self(entries.into_iter().collect()))
Ok(Self {
by_name: entries.into_iter().collect(),
revision: 0,
})
}
}
@@ -964,7 +978,7 @@ fn buffered_fingerprint_matches_the_ordered_state_hash() {
assert_eq!(state.fingerprint(), direct.finish());
let mut ordered_hash = DefaultHasher::new();
let mut table_hash = DefaultHasher::new();
let ordered: BTreeMap<_, _> = state.commands.0.iter().collect();
let ordered: BTreeMap<_, _> = state.commands.by_name.iter().collect();
ordered.hash(&mut ordered_hash);
state.commands.hash(&mut table_hash);
assert_eq!(table_hash.finish(), ordered_hash.finish());
@@ -1451,8 +1465,8 @@ impl State {
}
return;
}
c.usage.state.command_entries = self.commands.0.len();
for (name, meaning) in &self.commands.0 {
c.usage.state.command_entries = self.commands.by_name.len();
for (name, meaning) in &self.commands.by_name {
if let CommandName::Control(name) = name.as_ref() {
c.usage.state.control_name_bytes += name.len();
}
@@ -2050,7 +2064,7 @@ impl State {
let name = CommandName::Control(name.into());
if enabled {
self.command_bindings_mut().entry(name.into()).or_insert(meaning);
} else if self.commands.0.get(&name) == Some(&meaning) {
} else if self.commands.by_name.get(&name) == Some(&meaning) {
self.command_bindings_mut().remove(&name);
}
}
@@ -2227,7 +2241,7 @@ impl State {
let meaning = Meaning::Primitive(primitive);
if self.input_profile != super::InputProfile::TeX8Bit {
self.command_bindings_mut().insert(name.into(), meaning);
} else if self.commands.0.get(&name) == Some(&meaning) {
} else if self.commands.by_name.get(&name) == Some(&meaning) {
self.command_bindings_mut().remove(&name);
}
}
@@ -2297,7 +2311,7 @@ impl State {
let state = Self::initex().with_input_profile(self.input_profile);
let state = if extended { state.with_etex() } else { state };
Arc::unwrap_or_clone(state.commands)
.0
.by_name
.into_iter()
.filter_map(|(name, meaning)| match Arc::unwrap_or_clone(name) {
CommandName::Control(name) => Some((name, meaning)),
@@ -2310,7 +2324,13 @@ impl State {
}
fn command_bindings_mut(&mut self) -> &mut HashMap<Arc<CommandName>, Meaning> {
&mut Arc::make_mut(&mut self.commands).0
let bindings = Arc::make_mut(&mut self.commands);
bindings.revision += 1;
&mut bindings.by_name
}
pub(super) fn command_revision(&self) -> u64 {
self.commands.revision
}
pub fn meaning(&self, name: &CommandName) -> Meaning {
@@ -2320,7 +2340,7 @@ impl State {
CommandName::FrozenPrimitive(_) => self.original_primitive(name)
.map(|(_, meaning)| meaning.clone()).unwrap_or(Meaning::Undefined),
CommandName::Font(font) => Meaning::Font(*font),
_ => self.commands.0.get(name).cloned().unwrap_or(Meaning::Undefined),
_ => self.commands.by_name.get(name).cloned().unwrap_or(Meaning::Undefined),
}
}
/// Classic projection for byte-table consumers. A code outside the
@@ -2719,7 +2739,7 @@ impl State {
return;
}
let value = match &key {
Key::Command(name) => Saved::Command(self.commands.0.get(name).cloned()),
Key::Command(name) => Saved::Command(self.commands.by_name.get(name).cloned()),
Key::Catcode(c) => Saved::Catcode(self.catcodes.get(c).copied()),
Key::Count(index) => Saved::Integer(self.count(*index)),
Key::Endlinechar => Saved::Integer(self.endlinechar),