Reduce allocation and traversal in settled suffix checks
This commit is contained in:
1 parent
1ff049737f
commit
c4003c8dad
6 files changed
+351
-66
No files matched your search
@@ -800,7 +800,47 @@ enum PdftexRevision {
|
||||
}
|
||||
|
||||
const COMMAND_SHARDS: usize = 32;
|
||||
type CommandShard = HashMap<Arc<CommandName>, Meaning, RandomState>;
|
||||
type CommandEntries = HashMap<Arc<CommandName>, Meaning, RandomState>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct CommandShard {
|
||||
entries: CommandEntries,
|
||||
// Bounds may retain ranges of replaced or removed entries. Their only
|
||||
// use is to prove that every surviving token is before an edit.
|
||||
source_bounds: OnceLock<BTreeMap<usize, Option<usize>>>,
|
||||
}
|
||||
|
||||
impl CommandShard {
|
||||
fn new(entries: CommandEntries) -> Self {
|
||||
Self { entries, source_bounds: OnceLock::new() }
|
||||
}
|
||||
|
||||
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) -> Option<Meaning> {
|
||||
if let Some(bounds) = self.source_bounds.get_mut() {
|
||||
source_relocation::include_meaning_bounds(bounds, &meaning);
|
||||
}
|
||||
self.entries.insert(name, meaning)
|
||||
}
|
||||
|
||||
fn insert_if_absent(&mut self, name: Arc<CommandName>, meaning: Meaning) -> bool {
|
||||
let hashbrown::hash_map::Entry::Vacant(entry) = self.entries.entry(name) else {
|
||||
return false;
|
||||
};
|
||||
if let Some(bounds) = self.source_bounds.get_mut() {
|
||||
source_relocation::include_meaning_bounds(bounds, &meaning);
|
||||
}
|
||||
entry.insert(meaning);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for CommandShard {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.entries == other.entries
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for CommandShard {}
|
||||
type OrderedCommandNames = Arc<OnceLock<Vec<(Arc<CommandName>, u64)>>>;
|
||||
|
||||
pub(super) fn command_hasher() -> &'static RandomState {
|
||||
@@ -822,7 +862,9 @@ impl Default for CommandBindings {
|
||||
fn default() -> Self {
|
||||
let hasher = command_hasher();
|
||||
Self {
|
||||
shards: std::array::from_fn(|_| Arc::new(CommandShard::with_hasher(hasher.clone()))),
|
||||
shards: std::array::from_fn(|_| {
|
||||
Arc::new(CommandShard::new(CommandEntries::with_hasher(hasher.clone())))
|
||||
}),
|
||||
revisions: [0; COMMAND_SHARDS],
|
||||
ordered_names: Arc::default(),
|
||||
}
|
||||
@@ -849,6 +891,7 @@ impl CommandBindings {
|
||||
|
||||
fn get_hashed(&self, name: &CommandName, hash: u64) -> Option<&Meaning> {
|
||||
self.shards[Self::shard_index(hash)]
|
||||
.entries
|
||||
.raw_entry()
|
||||
.from_key_hashed_nocheck(hash, name)
|
||||
.map(|(_, meaning)| meaning)
|
||||
@@ -867,16 +910,13 @@ impl CommandBindings {
|
||||
}
|
||||
|
||||
fn insert_if_absent(&mut self, name: CommandName, meaning: Meaning) {
|
||||
if let hashbrown::hash_map::Entry::Vacant(entry) =
|
||||
self.shard_mut(&name).entry(name.into())
|
||||
{
|
||||
entry.insert(meaning);
|
||||
if self.shard_mut(&name).insert_if_absent(name.into(), meaning) {
|
||||
self.invalidate_order();
|
||||
}
|
||||
}
|
||||
|
||||
fn remove(&mut self, name: &CommandName) {
|
||||
if self.shard_mut(name).remove(name).is_some() {
|
||||
if self.shard_mut(name).entries.remove(name).is_some() {
|
||||
self.invalidate_order();
|
||||
}
|
||||
}
|
||||
@@ -890,11 +930,11 @@ impl CommandBindings {
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.shards.iter().map(|shard| shard.len()).sum()
|
||||
self.shards.iter().map(|shard| shard.entries.len()).sum()
|
||||
}
|
||||
|
||||
fn iter(&self) -> impl Iterator<Item = (&Arc<CommandName>, &Meaning)> {
|
||||
self.shards.iter().flat_map(|shard| shard.iter())
|
||||
self.shards.iter().flat_map(|shard| shard.entries.iter())
|
||||
}
|
||||
|
||||
fn ordered(&self) -> impl ExactSizeIterator<Item = (&Arc<CommandName>, &Meaning)> {
|
||||
@@ -921,7 +961,11 @@ impl CommandBindings {
|
||||
|
||||
impl Hash for CommandBindings {
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
self.ordered().collect::<Vec<_>>().hash(hasher);
|
||||
let entries = self.ordered();
|
||||
entries.len().hash(hasher);
|
||||
for entry in entries {
|
||||
entry.hash(hasher);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1049,7 +1093,45 @@ impl Default for FingerprintHasher {
|
||||
}
|
||||
}
|
||||
|
||||
impl FingerprintHasher {
|
||||
#[inline]
|
||||
fn write_fixed<const N: usize>(&mut self, bytes: [u8; N]) {
|
||||
if N > self.buffer.len() - self.used {
|
||||
self.hasher.write(&self.buffer[..self.used]);
|
||||
self.used = 0;
|
||||
}
|
||||
let end = self.used + N;
|
||||
self.buffer[self.used..end].copy_from_slice(&bytes);
|
||||
self.used = end;
|
||||
}
|
||||
}
|
||||
|
||||
impl Hasher for FingerprintHasher {
|
||||
#[inline]
|
||||
fn write_u8(&mut self, value: u8) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u16(&mut self, value: u16) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u32(&mut self, value: u32) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_u64(&mut self, value: u64) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_usize(&mut self, value: usize) {
|
||||
self.write_fixed(value.to_ne_bytes());
|
||||
}
|
||||
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
if bytes.len() > self.buffer.len() - self.used {
|
||||
self.hasher.write(&self.buffer[..self.used]);
|
||||
@@ -1610,6 +1692,60 @@ impl Default for State {
|
||||
|
||||
impl State {
|
||||
pub(super) fn collect_statistics(&self, c: &mut super::statistics::Collector) {
|
||||
if !c.material_only() {
|
||||
c.usage.state.command_entries = self.commands.len();
|
||||
for (name, meaning) in self.commands.iter() {
|
||||
Self::collect_command_statistics(c, name, meaning);
|
||||
}
|
||||
}
|
||||
self.collect_state_storage(c);
|
||||
}
|
||||
|
||||
pub(super) fn collect_compared_statistics(
|
||||
&self,
|
||||
old: &Self,
|
||||
current_usage: &mut super::statistics::Collector,
|
||||
old_usage: &mut super::statistics::Collector,
|
||||
) -> Option<()> {
|
||||
if !current_usage.material_only() {
|
||||
let entries = old.commands.len();
|
||||
current_usage.usage.state.command_entries = entries;
|
||||
old_usage.usage.state.command_entries = entries;
|
||||
// The alias proof needs corresponding visits, so both sides
|
||||
// walk the donor's binding order. Hashing and serialization
|
||||
// still use the independent, sorted command order.
|
||||
for (current, prior) in self.commands.shards.iter().zip(&old.commands.shards) {
|
||||
if current.entries.len() != prior.entries.len() {
|
||||
return None;
|
||||
}
|
||||
let shared = Arc::ptr_eq(current, prior);
|
||||
for (name, old_meaning) in &prior.entries {
|
||||
let meaning = if shared { old_meaning } else { current.entries.get(name)? };
|
||||
Self::collect_command_statistics(current_usage, name, meaning);
|
||||
Self::collect_command_statistics(old_usage, name, old_meaning);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.collect_state_storage(current_usage);
|
||||
old.collect_state_storage(old_usage);
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn collect_command_statistics(
|
||||
c: &mut super::statistics::Collector,
|
||||
name: &CommandName,
|
||||
meaning: &Meaning,
|
||||
) {
|
||||
if let CommandName::Control(name) = name {
|
||||
c.usage.state.control_name_bytes += name.len();
|
||||
}
|
||||
if let Meaning::Macro(macro_definition) = meaning {
|
||||
c.usage.state.macro_bindings += 1;
|
||||
c.macro_body(macro_definition);
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_state_storage(&self, c: &mut super::statistics::Collector) {
|
||||
use super::statistics::Ownership::Active;
|
||||
for b in self.boxes.values() {
|
||||
c.native_box(b, Active);
|
||||
@@ -1622,26 +1758,6 @@ impl State {
|
||||
}
|
||||
return;
|
||||
}
|
||||
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 {
|
||||
c.usage.state.control_name_bytes += name.len();
|
||||
}
|
||||
if let Meaning::Macro(macro_definition) = meaning {
|
||||
c.usage.state.macro_bindings += 1;
|
||||
c.macro_body(macro_definition);
|
||||
}
|
||||
};
|
||||
if track_aliases {
|
||||
for (name, meaning) in self.commands.ordered() {
|
||||
visit_command(name, meaning);
|
||||
}
|
||||
} else {
|
||||
for (name, meaning) in self.commands.iter() {
|
||||
visit_command(name, meaning);
|
||||
}
|
||||
}
|
||||
c.usage.state.token_registers = self.toks.len() + self.xe_inter_char_tokens.len();
|
||||
c.usage.state.parameter_entries =
|
||||
self.parameters.len() + self.token_parameters.len() + self.job_parameters.len();
|
||||
@@ -2485,7 +2601,7 @@ impl State {
|
||||
Arc::unwrap_or_clone(state.commands)
|
||||
.shards
|
||||
.into_iter()
|
||||
.flat_map(Arc::unwrap_or_clone)
|
||||
.flat_map(|shard| Arc::unwrap_or_clone(shard).entries)
|
||||
.filter_map(|(name, meaning)| match Arc::unwrap_or_clone(name) {
|
||||
CommandName::Control(name) => Some((name, meaning)),
|
||||
_ => None,
|
||||
|
||||
Reference in new issue
Block a user