Reuse bound command keys and keep identical shared bindings

This commit is contained in:
gloamxun committed 2026-12-02 03:33:33 +08:00
1 parent ac93967cb2
commit 143775b427
2 files changed
+112 -9

No files matched your search

+110 -7
View File
@@ -3,7 +3,7 @@ pub(super) mod source_relocation;
use fingerprint::{FingerprintHasher, MacroFingerprintInputs};
use hashbrown::HashMap;
use hashbrown::{HashMap, hash_map::RawEntryMut};
use std::collections::BTreeMap;
use std::hash::{BuildHasher, Hash, Hasher, RandomState};
#[cfg(test)]
@@ -825,8 +825,24 @@ impl CommandShard {
}
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) -> Option<Meaning> {
let hash = self.entries.hasher().hash_one(name.as_ref());
self.insert_hashed(name, meaning, hash)
}
fn insert_hashed(
&mut self,
name: Arc<CommandName>,
meaning: Meaning,
hash: u64,
) -> Option<Meaning> {
source_relocation::include_meaning_bounds(&mut self.source_bounds, &meaning);
self.entries.insert(name, meaning)
match self.entries.raw_entry_mut().from_key_hashed_nocheck(hash, name.as_ref()) {
RawEntryMut::Occupied(mut entry) => Some(std::mem::replace(entry.get_mut(), meaning)),
RawEntryMut::Vacant(entry) => {
entry.insert_hashed_nocheck(hash, name, meaning);
None
}
}
}
fn insert_if_absent(&mut self, name: Arc<CommandName>, meaning: Meaning) -> bool {
@@ -911,11 +927,18 @@ impl CommandBindings {
}
fn get_hashed(&self, name: &CommandName, hash: u64) -> Option<&Meaning> {
self.get_hashed_entry(name, hash).map(|(_, meaning)| meaning)
}
fn get_hashed_entry(
&self,
name: &CommandName,
hash: u64,
) -> Option<(&Arc<CommandName>, &Meaning)> {
self.shards[Self::shard_index(hash)]
.entries
.raw_entry()
.from_key_hashed_nocheck(hash, name)
.map(|(_, meaning)| meaning)
}
fn shard_mut(&mut self, name: &CommandName) -> &mut CommandShard {
@@ -925,7 +948,22 @@ impl CommandBindings {
}
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) {
if self.shard_mut(&name).insert(name, meaning).is_none() {
let hash = command_hasher().hash_one(name.as_ref());
let index = Self::shard_index(hash);
let shard = &mut self.shards[index];
// The caller already saved or cleared the group entry. The shared
// shard can stay in place when the installed binding is identical.
if Arc::strong_count(shard) > 1
&& shard.entries.raw_entry().from_key_hashed_nocheck(hash, name.as_ref())
.is_some_and(|(_, previous)| match (previous, &meaning) {
(Meaning::Macro(previous), Meaning::Macro(body)) => Arc::ptr_eq(previous, body),
(previous, meaning) => previous == meaning,
})
{
return;
}
self.revisions[index] += 1;
if Arc::make_mut(shard).insert_hashed(name, meaning, hash).is_none() {
self.invalidate_order();
}
}
@@ -1371,6 +1409,48 @@ fn command_shard_equality_matches_keys_when_bucket_order_differs() {
assert_ne!(before, after);
}
#[test]
fn reinstalling_a_shared_binding_preserves_its_allocation_and_epoch() {
let name = CommandName::Control("sharedbinding".into());
let body = Arc::new(Macro {
prefix: Vec::new(),
parameters: Vec::new(),
parameter_chars: Vec::new(),
replacement: Vec::new(),
long: false,
outer: false,
protected: false,
});
let mut state = State::default();
state.define(name.clone(), Meaning::Macro(body.clone()), true);
let prior = state.clone();
let hash = command_hasher().hash_one(&name);
let index = CommandBindings::shard_index(hash);
let key = state.commands.get_hashed_entry(&name, hash).unwrap().0.clone();
assert!(Arc::ptr_eq(&key, &state.cache_meaning_hashed(&name, hash).0));
state.define(name.clone(), Meaning::Macro(body.clone()), true);
assert!(Arc::ptr_eq(&state.commands.shards[index], &prior.commands.shards[index]));
assert_eq!(state.command_revision(hash), prior.command_revision(hash));
let independent = Arc::new(body.as_ref().clone());
state.define(name.clone(), Meaning::Macro(independent.clone()), true);
assert!(!Arc::ptr_eq(&state.commands.shards[index], &prior.commands.shards[index]));
assert_eq!(state.command_revision(hash), prior.command_revision(hash) + 1);
let Meaning::Macro(installed) = state.meaning(&name) else { panic!("expected macro") };
assert!(Arc::ptr_eq(&installed, &independent));
assert_eq!(state, prior);
state.begin_group(GroupKind::Simple, None);
state.define(name.clone(), Meaning::Count(5), false);
let local = state.clone();
let saved = Key::Command(name.clone());
assert!(state.groups.last().unwrap().saved.contains_key(&saved));
state.define(name, Meaning::Count(5), true);
assert!(Arc::ptr_eq(&state.commands.shards[index], &local.commands.shards[index]));
assert!(!state.groups.last().unwrap().saved.contains_key(&saved));
}
#[test]
fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() {
let mut state = State::initex().with_etex();
@@ -2852,14 +2932,37 @@ impl State {
}
pub(super) fn meaning_hashed(&self, name: &CommandName, hash: u64) -> Meaning {
match name {
self.resolve_meaning_hashed(name, hash).1
}
pub(super) fn cache_meaning_hashed(
&self,
name: &CommandName,
hash: u64,
) -> (Arc<CommandName>, Meaning) {
let (key, meaning) = self.resolve_meaning_hashed(name, hash);
(key.cloned().unwrap_or_else(|| Arc::new(name.clone())), meaning)
}
fn resolve_meaning_hashed(
&self,
name: &CommandName,
hash: u64,
) -> (Option<&Arc<CommandName>>, Meaning) {
let meaning = match name {
CommandName::FrozenRelax => Meaning::Primitive(Primitive::Relax),
CommandName::FrozenPrimitivePrefix => Meaning::Primitive(Primitive::DeferredPrimitive),
CommandName::FrozenPrimitive(_) => self.original_primitive(name)
.map(|(_, meaning)| meaning.clone()).unwrap_or(Meaning::Undefined),
CommandName::Font(font) => Meaning::Font(*font),
_ => self.commands.get_hashed(name, hash).cloned().unwrap_or(Meaning::Undefined),
}
_ => {
if let Some((key, meaning)) = self.commands.get_hashed_entry(name, hash) {
return (Some(key), meaning.clone());
}
Meaning::Undefined
}
};
(None, meaning)
}
/// Classic projection for byte-table consumers. A code outside the
/// representable classic range has TeX's recovery value of zero.