Reuse command lookup hashes and validate cached meanings by shard

This commit is contained in:
gloamxun committed 2026-11-29 10:52:20 +08:00
1 parent 330c4bde51
commit d249ec7eba
4 files changed
+105 -30

No files matched your search

+26 -16
View File
@@ -800,21 +800,25 @@ enum PdftexRevision {
const COMMAND_SHARDS: usize = 32;
type CommandShard = HashMap<Arc<CommandName>, Meaning, RandomState>;
pub(super) fn command_hasher() -> &'static RandomState {
// Identical hashes feed the binding table and the executor's read cache.
// The seed and partition are private to this process.
static HASHER: OnceLock<RandomState> = OnceLock::new();
HASHER.get_or_init(RandomState::new)
}
#[derive(Clone, Debug)]
struct CommandBindings {
shards: [Arc<CommandShard>; COMMAND_SHARDS],
revision: u64,
revisions: [u64; COMMAND_SHARDS],
}
impl Default for CommandBindings {
fn default() -> Self {
// A shared seed gives independently built tables the same partition.
// The seed and partition are private to this process.
static HASHER: OnceLock<RandomState> = OnceLock::new();
let hash = HASHER.get_or_init(RandomState::new);
let hasher = command_hasher();
Self {
shards: std::array::from_fn(|_| Arc::new(CommandShard::with_hasher(hash.clone()))),
revision: 0,
shards: std::array::from_fn(|_| Arc::new(CommandShard::with_hasher(hasher.clone()))),
revisions: [0; COMMAND_SHARDS],
}
}
}
@@ -834,7 +838,10 @@ impl CommandBindings {
}
fn get(&self, name: &CommandName) -> Option<&Meaning> {
let hash = self.shards[0].hasher().hash_one(name);
self.get_hashed(name, command_hasher().hash_one(name))
}
fn get_hashed(&self, name: &CommandName, hash: u64) -> Option<&Meaning> {
self.shards[Self::shard_index(hash)]
.raw_entry()
.from_key_hashed_nocheck(hash, name)
@@ -842,8 +849,9 @@ impl CommandBindings {
}
fn shard_mut(&mut self, name: &CommandName) -> &mut CommandShard {
let hash = self.shards[0].hasher().hash_one(name);
Arc::make_mut(&mut self.shards[Self::shard_index(hash)])
let index = Self::shard_index(command_hasher().hash_one(name));
self.revisions[index] += 1;
Arc::make_mut(&mut self.shards[index])
}
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) {
@@ -2408,23 +2416,25 @@ impl State {
}
fn command_bindings_mut(&mut self) -> &mut CommandBindings {
let bindings = Arc::make_mut(&mut self.commands);
bindings.revision += 1;
bindings
Arc::make_mut(&mut self.commands)
}
pub(super) fn command_revision(&self) -> u64 {
self.commands.revision
pub(super) fn command_revision(&self, hash: u64) -> u64 {
self.commands.revisions[CommandBindings::shard_index(hash)]
}
pub fn meaning(&self, name: &CommandName) -> Meaning {
self.meaning_hashed(name, command_hasher().hash_one(name))
}
pub(super) fn meaning_hashed(&self, name: &CommandName, hash: u64) -> 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(name).cloned().unwrap_or(Meaning::Undefined),
_ => self.commands.get_hashed(name, hash).cloned().unwrap_or(Meaning::Undefined),
}
}
/// Classic projection for byte-table consumers. A code outside the