Reuse native command meanings within a binding revision
This commit is contained in:
1 parent
9f4cc64c8f
commit
d8af3518a7
4 files changed
+135
-32
No files matched your search
@@ -1,4 +1,4 @@
|
||||
use std::collections::{BTreeSet, HashSet, VecDeque};
|
||||
use std::collections::{BTreeSet, HashMap, VecDeque};
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::job::InputStream;
|
||||
@@ -233,7 +233,9 @@ pub struct Executor {
|
||||
stream_sources: BTreeSet<usize>,
|
||||
generated_inputs: std::collections::BTreeMap<usize, GeneratedInput>,
|
||||
reads: DependencyReads,
|
||||
meaning_reads: HashSet<CommandName>,
|
||||
// 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)>,
|
||||
reads_tracing_macros: bool,
|
||||
conditionals: Vec<Conditional>,
|
||||
in_csname: bool,
|
||||
@@ -488,7 +490,7 @@ impl Executor {
|
||||
stream_sources: BTreeSet::new(),
|
||||
generated_inputs: Default::default(),
|
||||
reads: DependencyReads::default(),
|
||||
meaning_reads: HashSet::new(),
|
||||
meaning_reads: HashMap::new(),
|
||||
reads_tracing_macros: false,
|
||||
conditionals: Vec::new(),
|
||||
in_csname: false,
|
||||
@@ -724,26 +726,30 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn record_meaning_read(&mut self, name: &CommandName) {
|
||||
if !self.meaning_reads.contains(name) {
|
||||
self.meaning_reads.insert(name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn take_dependency_reads(&mut self) -> BTreeSet<Dependency> {
|
||||
let mut reads = std::mem::take(&mut self.reads).into_ordered();
|
||||
reads.extend(self.meaning_reads.drain().map(Dependency::Meaning));
|
||||
reads.extend(self.meaning_reads.drain().map(|(name, _)| Dependency::Meaning(name)));
|
||||
reads
|
||||
}
|
||||
|
||||
fn meaning(&mut self, token: &Token) -> Meaning {
|
||||
match &token.kind {
|
||||
TokenKind::Command(name) => {
|
||||
self.record_meaning_read(name);
|
||||
self.state.meaning(name)
|
||||
let TokenKind::Command(name) = &token.kind else {
|
||||
return Meaning::Character(token.kind.clone());
|
||||
};
|
||||
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;
|
||||
}
|
||||
kind => Meaning::Character(kind.clone()),
|
||||
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) {
|
||||
@@ -1357,8 +1363,7 @@ impl Executor {
|
||||
Etexrevision => self.print_tokens(".6", token)?,
|
||||
Csname => {
|
||||
let name = self.csname(files)?;
|
||||
self.record_meaning_read(&name);
|
||||
if self.state.meaning(&name) == Meaning::Undefined {
|
||||
if self.lookup_meaning(&name) == Meaning::Undefined {
|
||||
self.define_traced(
|
||||
name.clone(),
|
||||
Meaning::Primitive(Relax),
|
||||
@@ -1756,8 +1761,7 @@ impl Executor {
|
||||
}
|
||||
Ifcsname => {
|
||||
let name = self.csname(files)?;
|
||||
self.record_meaning_read(&name);
|
||||
self.state.meaning(&name) != Meaning::Undefined
|
||||
self.lookup_meaning(&name) != Meaning::Undefined
|
||||
}
|
||||
Ifincsname => self.in_csname,
|
||||
Ifeof => {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -50,3 +50,53 @@ fn meaning_reads_survive_deduplication_and_include_negative_csname_queries() {
|
||||
Meaning::Primitive(Primitive::Relax)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resumed_command_reads_follow_local_restores_and_global_redefinitions() {
|
||||
let prefix = br"\catcode123=1 \catcode125=2
|
||||
\ifcsname value\endcsname\message{unexpected}\else\message{absent}\fi
|
||||
\def\value{outer}\message{\value}
|
||||
";
|
||||
let source_with = |label: &str| SourceFile {
|
||||
name: "probe.tex".into(),
|
||||
data: [
|
||||
prefix.as_slice(),
|
||||
format!(r"\begingroup\def\value{{{label}}}\message{{\value}}\endgroup").as_bytes(),
|
||||
br"
|
||||
\message{\value}
|
||||
\begingroup\global\let\value\missing\endgroup
|
||||
\ifcsname value\endcsname\message{unexpected}\else\message{absent}\fi
|
||||
\gdef\value{global}\message{\value}
|
||||
\end",
|
||||
]
|
||||
.concat(),
|
||||
};
|
||||
let engine = || Executor::new(State::initex().with_etex(), Limits::default());
|
||||
let (original, captured) =
|
||||
engine().run_capturing_entry(source_with("old"), &mut NoFiles, prefix.len(), |_| ());
|
||||
original.unwrap();
|
||||
let (checkpoint, ()) = captured.unwrap();
|
||||
assert_eq!(checkpoint.frontier(), prefix.len());
|
||||
|
||||
let edited = source_with("local");
|
||||
let full = engine().run(edited.clone(), &mut NoFiles).unwrap();
|
||||
let resumed = checkpoint
|
||||
.resume_after_host_replay(edited, &mut NoFiles, Cancellation::default())
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(resumed, full);
|
||||
let messages: Vec<_> = resumed
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Message { text, .. } => Some(text.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(messages, ["absent", "outer", "local", "outer", "absent", "global"]);
|
||||
assert!(
|
||||
resumed
|
||||
.reads
|
||||
.contains(&Dependency::Meaning(CommandName::Control("value".into())))
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user