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

+23 -14
View File
@@ -1,9 +1,12 @@
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::collections::{BTreeSet, VecDeque};
use std::hash::{BuildHasher, RandomState};
use std::sync::Arc;
use hashbrown::{HashMap, hash_map::RawEntryMut};
use super::job::InputStream;
use super::mouth::Mouth;
use super::state::{GroupKind, Key, Replacement};
use super::state::{GroupKind, Key, Replacement, command_hasher};
use super::*;
mod alignment;
mod boxes;
@@ -235,8 +238,8 @@ pub struct Executor {
generated_inputs: std::collections::BTreeMap<usize, GeneratedInput>,
reads: DependencyReads,
// Cache the resolved meaning alongside the already required read evidence.
// The revision is only valid within this executor and its checkpoints.
meaning_reads: HashMap<CommandName, (u64, Meaning)>,
// A shard's revision is only valid on this executor's state history.
meaning_reads: HashMap<CommandName, (u64, Meaning), RandomState>,
reads_tracing_macros: bool,
conditionals: Vec<Conditional>,
in_csname: bool,
@@ -533,7 +536,7 @@ impl Executor {
stream_sources: BTreeSet::new(),
generated_inputs: Default::default(),
reads: DependencyReads::default(),
meaning_reads: HashMap::new(),
meaning_reads: HashMap::with_hasher(command_hasher().clone()),
reads_tracing_macros: false,
conditionals: Vec::new(),
in_csname: false,
@@ -784,17 +787,23 @@ impl Executor {
self.lookup_meaning(name)
}
fn lookup_meaning(&mut self, name: &CommandName) -> Meaning {
let revision = self.state.command_revision();
if let Some((cached_revision, meaning)) = self.meaning_reads.get_mut(name) {
if *cached_revision != revision {
*meaning = self.state.meaning(name);
*cached_revision = revision;
let hash = self.meaning_reads.hasher().hash_one(name);
let revision = self.state.command_revision(hash);
match self.meaning_reads.raw_entry_mut().from_key_hashed_nocheck(hash, name) {
RawEntryMut::Occupied(mut entry) => {
let (cached_revision, meaning) = entry.get_mut();
if *cached_revision != revision {
*meaning = self.state.meaning_hashed(name, hash);
*cached_revision = revision;
}
meaning.clone()
}
RawEntryMut::Vacant(entry) => {
let meaning = self.state.meaning_hashed(name, hash);
entry.insert_hashed_nocheck(hash, name.clone(), (revision, meaning.clone()));
meaning
}
return meaning.clone();
}
let meaning = self.state.meaning(name);
self.meaning_reads.insert(name.clone(), (revision, meaning.clone()));
meaning
}
fn primitive(&mut self, token: &Token) -> Option<Primitive> {
match self.meaning(token) {
+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