Reduce and measure native suffix convergence work
This commit is contained in:
1 parent
c008c52964
commit
b081c951af
10 files changed
+330
-97
No files matched your search
@@ -801,6 +801,7 @@ enum PdftexRevision {
|
||||
|
||||
const COMMAND_SHARDS: usize = 32;
|
||||
type CommandShard = HashMap<Arc<CommandName>, Meaning, RandomState>;
|
||||
type OrderedCommandNames = Arc<OnceLock<Vec<(Arc<CommandName>, u64)>>>;
|
||||
|
||||
pub(super) fn command_hasher() -> &'static RandomState {
|
||||
// Identical hashes feed the binding table and the executor's read cache.
|
||||
@@ -813,6 +814,8 @@ pub(super) fn command_hasher() -> &'static RandomState {
|
||||
struct CommandBindings {
|
||||
shards: [Arc<CommandShard>; COMMAND_SHARDS],
|
||||
revisions: [u64; COMMAND_SHARDS],
|
||||
// Cache names only so redefinitions can share the same order.
|
||||
ordered_names: OrderedCommandNames,
|
||||
}
|
||||
|
||||
impl Default for CommandBindings {
|
||||
@@ -821,6 +824,7 @@ impl Default for CommandBindings {
|
||||
Self {
|
||||
shards: std::array::from_fn(|_| Arc::new(CommandShard::with_hasher(hasher.clone()))),
|
||||
revisions: [0; COMMAND_SHARDS],
|
||||
ordered_names: Arc::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -857,15 +861,32 @@ impl CommandBindings {
|
||||
}
|
||||
|
||||
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) {
|
||||
self.shard_mut(&name).insert(name, meaning);
|
||||
if self.shard_mut(&name).insert(name, meaning).is_none() {
|
||||
self.invalidate_order();
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_if_absent(&mut self, name: CommandName, meaning: Meaning) {
|
||||
self.shard_mut(&name).entry(name.into()).or_insert(meaning);
|
||||
if let hashbrown::hash_map::Entry::Vacant(entry) =
|
||||
self.shard_mut(&name).entry(name.into())
|
||||
{
|
||||
entry.insert(meaning);
|
||||
self.invalidate_order();
|
||||
}
|
||||
}
|
||||
|
||||
fn remove(&mut self, name: &CommandName) {
|
||||
self.shard_mut(name).remove(name);
|
||||
if self.shard_mut(name).remove(name).is_some() {
|
||||
self.invalidate_order();
|
||||
}
|
||||
}
|
||||
|
||||
fn invalidate_order(&mut self) {
|
||||
if let Some(order) = Arc::get_mut(&mut self.ordered_names) {
|
||||
order.take();
|
||||
} else {
|
||||
self.ordered_names = Arc::default();
|
||||
}
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
@@ -876,23 +897,37 @@ impl CommandBindings {
|
||||
self.shards.iter().flat_map(|shard| shard.iter())
|
||||
}
|
||||
|
||||
fn ordered(&self) -> Vec<(&Arc<CommandName>, &Meaning)> {
|
||||
let mut entries = Vec::with_capacity(self.len());
|
||||
entries.extend(self.iter());
|
||||
entries.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
|
||||
entries
|
||||
fn ordered(&self) -> impl ExactSizeIterator<Item = (&Arc<CommandName>, &Meaning)> {
|
||||
self.ordered_names
|
||||
.get_or_init(|| {
|
||||
let hasher = command_hasher();
|
||||
let mut names = Vec::with_capacity(self.len());
|
||||
names.extend(
|
||||
self.iter()
|
||||
.map(|(name, _)| (name.clone(), hasher.hash_one(name))),
|
||||
);
|
||||
names.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
|
||||
names
|
||||
})
|
||||
.iter()
|
||||
.map(|(name, hash)| {
|
||||
let meaning = self
|
||||
.get_hashed(name, *hash)
|
||||
.expect("ordered command names match the binding table");
|
||||
(name, meaning)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for CommandBindings {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.ordered().hash(hasher);
|
||||
self.ordered().collect::<Vec<_>>().hash(hasher);
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for CommandBindings {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serde::Serialize::serialize(&self.ordered(), serializer)
|
||||
serializer.collect_seq(self.ordered())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1066,6 +1101,7 @@ 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 fingerprint = state.fingerprint();
|
||||
let snapshot = state.clone();
|
||||
state.define(name.clone(), Meaning::Count(7), true);
|
||||
assert_eq!(snapshot.meaning(&name), Meaning::Count(3));
|
||||
@@ -1073,10 +1109,45 @@ fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() {
|
||||
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);
|
||||
assert!(Arc::ptr_eq(
|
||||
&state.commands.ordered_names,
|
||||
&snapshot.commands.ordered_names,
|
||||
));
|
||||
let (_, redefined) = state
|
||||
.commands
|
||||
.ordered()
|
||||
.find(|(key, _)| key.as_ref() == &name)
|
||||
.unwrap();
|
||||
assert_eq!(*redefined, Meaning::Count(7));
|
||||
|
||||
let added = CommandName::Control("checkpointorderadded".into());
|
||||
let bindings = state.command_bindings_mut();
|
||||
let before_insertion = bindings.clone();
|
||||
bindings.insert_if_absent(name.clone(), Meaning::Count(100));
|
||||
assert!(Arc::ptr_eq(
|
||||
&bindings.ordered_names,
|
||||
&before_insertion.ordered_names,
|
||||
));
|
||||
bindings.insert_if_absent(added.clone(), Meaning::Count(9));
|
||||
assert!(!Arc::ptr_eq(
|
||||
&bindings.ordered_names,
|
||||
&before_insertion.ordered_names,
|
||||
));
|
||||
let ordered: BTreeMap<_, _> = bindings.iter().collect();
|
||||
let expected: Vec<_> = ordered.into_iter().collect();
|
||||
assert!(bindings.ordered().eq(expected.iter().copied()));
|
||||
assert_eq!(
|
||||
serde_json::to_vec(bindings).unwrap(),
|
||||
serde_json::to_vec(&expected).unwrap(),
|
||||
);
|
||||
let with_added = bindings.clone();
|
||||
bindings.remove(&added);
|
||||
assert!(bindings.ordered().eq(before_insertion.ordered()));
|
||||
assert!(with_added.ordered().any(|(key, _)| key.as_ref() == &added));
|
||||
|
||||
state.define(name, Meaning::Count(3), true);
|
||||
assert_eq!(state, snapshot);
|
||||
assert_eq!(state.fingerprint(), snapshot.fingerprint());
|
||||
assert_eq!(state.fingerprint(), fingerprint);
|
||||
assert_eq!(serde_json::to_vec(&state).unwrap(), serde_json::to_vec(&snapshot).unwrap());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user