Share command bindings by partition across TeX checkpoints
This commit is contained in:
1 parent
a8dfa14b7e
commit
ffd84cd15b
4 files changed
+130
-30
No files matched your search
@@ -7,6 +7,7 @@ description = "Experimental incremental TeX-compatible typesetting core (route D
|
||||
|
||||
[dependencies]
|
||||
flate2 = "1.1"
|
||||
hashbrown = { version = "0.17.2", default-features = false, features = ["raw-entry"] }
|
||||
harfbuzz_rs = "=3.0.1"
|
||||
image = { version = "0.23.12", default-features = false, features = ["bmp"] }
|
||||
lopdf = "0.26"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
use std::sync::Arc;
|
||||
use hashbrown::HashMap;
|
||||
use std::collections::BTreeMap;
|
||||
use std::hash::{BuildHasher, DefaultHasher, Hash, Hasher, RandomState};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
use super::{
|
||||
CatCode, FontId, Glue, JobParameter, JobTime, NativeBox, Parameter, ParameterValue, Register,
|
||||
@@ -796,23 +797,78 @@ enum PdftexRevision {
|
||||
R24,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
const COMMAND_SHARDS: usize = 32;
|
||||
type CommandShard = HashMap<Arc<CommandName>, Meaning, RandomState>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct CommandBindings {
|
||||
by_name: HashMap<Arc<CommandName>, Meaning>,
|
||||
shards: [Arc<CommandShard>; COMMAND_SHARDS],
|
||||
revision: u64,
|
||||
}
|
||||
|
||||
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);
|
||||
Self {
|
||||
shards: std::array::from_fn(|_| Arc::new(CommandShard::with_hasher(hash.clone()))),
|
||||
revision: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for CommandBindings {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.by_name == other.by_name
|
||||
self.shards == other.shards
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for CommandBindings {}
|
||||
|
||||
impl CommandBindings {
|
||||
fn shard_index(hash: u64) -> usize {
|
||||
// Use bits separate from the table's bucket index and control tag.
|
||||
((hash >> 32) as usize) % COMMAND_SHARDS
|
||||
}
|
||||
|
||||
fn get(&self, name: &CommandName) -> Option<&Meaning> {
|
||||
let hash = self.shards[0].hasher().hash_one(name);
|
||||
self.shards[Self::shard_index(hash)]
|
||||
.raw_entry()
|
||||
.from_key_hashed_nocheck(hash, name)
|
||||
.map(|(_, meaning)| meaning)
|
||||
}
|
||||
|
||||
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)])
|
||||
}
|
||||
|
||||
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) {
|
||||
self.shard_mut(&name).insert(name, meaning);
|
||||
}
|
||||
|
||||
fn insert_if_absent(&mut self, name: CommandName, meaning: Meaning) {
|
||||
self.shard_mut(&name).entry(name.into()).or_insert(meaning);
|
||||
}
|
||||
|
||||
fn remove(&mut self, name: &CommandName) {
|
||||
self.shard_mut(name).remove(name);
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.shards.iter().map(|shard| shard.len()).sum()
|
||||
}
|
||||
|
||||
fn iter(&self) -> impl Iterator<Item = (&Arc<CommandName>, &Meaning)> {
|
||||
self.shards.iter().flat_map(|shard| shard.iter())
|
||||
}
|
||||
|
||||
fn ordered(&self) -> Vec<(&Arc<CommandName>, &Meaning)> {
|
||||
let mut entries: Vec<_> = self.by_name.iter().collect();
|
||||
let mut entries = Vec::with_capacity(self.len());
|
||||
entries.extend(self.iter());
|
||||
entries.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
|
||||
entries
|
||||
}
|
||||
@@ -835,17 +891,18 @@ impl<'de> serde::Deserialize<'de> for CommandBindings {
|
||||
let entries = <Vec<(Arc<CommandName>, Meaning)> as serde::Deserialize>::deserialize(
|
||||
deserializer,
|
||||
)?;
|
||||
Ok(Self {
|
||||
by_name: entries.into_iter().collect(),
|
||||
revision: 0,
|
||||
})
|
||||
let mut bindings = Self::default();
|
||||
for (name, meaning) in entries {
|
||||
bindings.insert(name, meaning);
|
||||
}
|
||||
Ok(bindings)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct State {
|
||||
// Checkpoints share the table. A write copies the bindings, while names
|
||||
// and macro bodies stay shared. Formats and fingerprints use key order.
|
||||
// Checkpoints share the table. A write copies just its command shard.
|
||||
// Formats and fingerprints use key order.
|
||||
commands: Arc<CommandBindings>,
|
||||
catcodes: BTreeMap<char, CatCode>,
|
||||
counts: BTreeMap<u16, i32>,
|
||||
@@ -978,7 +1035,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.by_name.iter().collect();
|
||||
let ordered: BTreeMap<_, _> = state.commands.iter().collect();
|
||||
ordered.hash(&mut ordered_hash);
|
||||
state.commands.hash(&mut table_hash);
|
||||
assert_eq!(table_hash.finish(), ordered_hash.finish());
|
||||
@@ -994,6 +1051,25 @@ fn buffered_fingerprint_matches_the_ordered_state_hash() {
|
||||
check(&state);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() {
|
||||
let mut state = State::initex().with_etex();
|
||||
let name = CommandName::Control("checkpointbinding".into());
|
||||
state.define(name.clone(), Meaning::Count(3), true);
|
||||
let snapshot = state.clone();
|
||||
state.define(name.clone(), Meaning::Count(7), true);
|
||||
assert_eq!(snapshot.meaning(&name), Meaning::Count(3));
|
||||
assert_eq!(state.meaning(&name), Meaning::Count(7));
|
||||
let shared = state.commands.shards.iter().zip(&snapshot.commands.shards)
|
||||
.filter(|(current, saved)| Arc::ptr_eq(current, saved)).count();
|
||||
assert_eq!(shared, COMMAND_SHARDS - 1);
|
||||
|
||||
state.define(name, Meaning::Count(3), true);
|
||||
assert_eq!(state, snapshot);
|
||||
assert_eq!(state.fingerprint(), snapshot.fingerprint());
|
||||
assert_eq!(serde_json::to_vec(&state).unwrap(), serde_json::to_vec(&snapshot).unwrap());
|
||||
}
|
||||
|
||||
// These primitives belong to the pdfTeX command surface. Position queries,
|
||||
// page dimensions and the shared margin-kern extension are installed separately.
|
||||
const PDFTEX_ONLY_PRIMITIVES: &[(&str, Primitive)] = {
|
||||
@@ -1465,7 +1541,7 @@ impl State {
|
||||
}
|
||||
return;
|
||||
}
|
||||
c.usage.state.command_entries = self.commands.by_name.len();
|
||||
c.usage.state.command_entries = self.commands.len();
|
||||
let track_aliases = c.tracks_aliases();
|
||||
let mut visit_command = |name: &CommandName, meaning: &Meaning| {
|
||||
if let CommandName::Control(name) = name {
|
||||
@@ -1481,7 +1557,7 @@ impl State {
|
||||
visit_command(name, meaning);
|
||||
}
|
||||
} else {
|
||||
for (name, meaning) in &self.commands.by_name {
|
||||
for (name, meaning) in self.commands.iter() {
|
||||
visit_command(name, meaning);
|
||||
}
|
||||
}
|
||||
@@ -1947,8 +2023,7 @@ impl State {
|
||||
),
|
||||
] {
|
||||
self.command_bindings_mut()
|
||||
.entry(CommandName::Control(name.into()).into())
|
||||
.or_insert(meaning);
|
||||
.insert_if_absent(CommandName::Control(name.into()), meaning);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1964,8 +2039,7 @@ impl State {
|
||||
if self.profile == Profile::EtexBootstrap {
|
||||
for kind in LinePenaltyKind::ALL {
|
||||
self.command_bindings_mut()
|
||||
.entry(CommandName::Control(kind.name().into()).into())
|
||||
.or_insert(Meaning::Primitive(Primitive::LinePenalties(kind)));
|
||||
.insert_if_absent(CommandName::Control(kind.name().into()), Meaning::Primitive(Primitive::LinePenalties(kind)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1985,8 +2059,7 @@ impl State {
|
||||
pub(super) fn restore_legacy_etex_parameter(&mut self, parameter: Parameter) {
|
||||
if self.profile == Profile::EtexBootstrap {
|
||||
self.command_bindings_mut()
|
||||
.entry(CommandName::Control(parameter.name()).into())
|
||||
.or_insert(Meaning::Parameter(parameter));
|
||||
.insert_if_absent(CommandName::Control(parameter.name()), Meaning::Parameter(parameter));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2073,8 +2146,8 @@ impl State {
|
||||
fn update_profile_command(&mut self, name: &str, meaning: Meaning, enabled: bool) {
|
||||
let name = CommandName::Control(name.into());
|
||||
if enabled {
|
||||
self.command_bindings_mut().entry(name.into()).or_insert(meaning);
|
||||
} else if self.commands.by_name.get(&name) == Some(&meaning) {
|
||||
self.command_bindings_mut().insert_if_absent(name, meaning);
|
||||
} else if self.commands.get(&name) == Some(&meaning) {
|
||||
self.command_bindings_mut().remove(&name);
|
||||
}
|
||||
}
|
||||
@@ -2251,7 +2324,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.by_name.get(&name) == Some(&meaning) {
|
||||
} else if self.commands.get(&name) == Some(&meaning) {
|
||||
self.command_bindings_mut().remove(&name);
|
||||
}
|
||||
}
|
||||
@@ -2321,8 +2394,9 @@ 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)
|
||||
.by_name
|
||||
.shards
|
||||
.into_iter()
|
||||
.flat_map(Arc::unwrap_or_clone)
|
||||
.filter_map(|(name, meaning)| match Arc::unwrap_or_clone(name) {
|
||||
CommandName::Control(name) => Some((name, meaning)),
|
||||
_ => None,
|
||||
@@ -2333,10 +2407,10 @@ impl State {
|
||||
.map(|meaning| (name, meaning))
|
||||
}
|
||||
|
||||
fn command_bindings_mut(&mut self) -> &mut HashMap<Arc<CommandName>, Meaning> {
|
||||
fn command_bindings_mut(&mut self) -> &mut CommandBindings {
|
||||
let bindings = Arc::make_mut(&mut self.commands);
|
||||
bindings.revision += 1;
|
||||
&mut bindings.by_name
|
||||
bindings
|
||||
}
|
||||
|
||||
pub(super) fn command_revision(&self) -> u64 {
|
||||
@@ -2350,7 +2424,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.by_name.get(name).cloned().unwrap_or(Meaning::Undefined),
|
||||
_ => self.commands.get(name).cloned().unwrap_or(Meaning::Undefined),
|
||||
}
|
||||
}
|
||||
/// Classic projection for byte-table consumers. A code outside the
|
||||
@@ -2749,7 +2823,7 @@ impl State {
|
||||
return;
|
||||
}
|
||||
let value = match &key {
|
||||
Key::Command(name) => Saved::Command(self.commands.by_name.get(name).cloned()),
|
||||
Key::Command(name) => Saved::Command(self.commands.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),
|
||||
|
||||
Reference in new issue
Block a user