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
@@ -50,8 +50,9 @@ impl Executor {
|
|||||||
&self,
|
&self,
|
||||||
shipping: Option<&NativeBox>,
|
shipping: Option<&NativeBox>,
|
||||||
host: Option<HostResourceUsage>,
|
host: Option<HostResourceUsage>,
|
||||||
c: Collector,
|
mut c: Collector,
|
||||||
) -> ResourceUsage {
|
) -> ResourceUsage {
|
||||||
|
self.state.collect_statistics(&mut c);
|
||||||
let mut c = self.collect_live_usage(shipping, host, c);
|
let mut c = self.collect_live_usage(shipping, host, c);
|
||||||
Self::collect_event_usage(self.events.iter(), &mut c);
|
Self::collect_event_usage(self.events.iter(), &mut c);
|
||||||
c.usage
|
c.usage
|
||||||
@@ -63,7 +64,6 @@ impl Executor {
|
|||||||
host: Option<HostResourceUsage>,
|
host: Option<HostResourceUsage>,
|
||||||
mut c: Collector,
|
mut c: Collector,
|
||||||
) -> Collector {
|
) -> Collector {
|
||||||
self.state.collect_statistics(&mut c);
|
|
||||||
c.usage.steps = self.steps;
|
c.usage.steps = self.steps;
|
||||||
c.usage.source_files = self.sources.len();
|
c.usage.source_files = self.sources.len();
|
||||||
c.usage.source_payload_bytes = self.sources.iter().map(|s| s.data.len()).sum();
|
c.usage.source_payload_bytes = self.sources.iter().map(|s| s.data.len()).sum();
|
||||||
@@ -276,8 +276,11 @@ impl Executor {
|
|||||||
}
|
}
|
||||||
let (history, old_history) =
|
let (history, old_history) =
|
||||||
Self::collect_event_usage_pair(&self.events, &old.events, collector());
|
Self::collect_event_usage_pair(&self.events, &old.events, collector());
|
||||||
let mut live = self.collect_live_usage(None, None, collector().tracking_aliases());
|
let mut live = collector().tracking_aliases();
|
||||||
let old_live = old.collect_live_usage(None, None, collector().tracking_aliases());
|
let mut old_live = collector().tracking_aliases();
|
||||||
|
self.state.collect_compared_statistics(&old.state, &mut live, &mut old_live)?;
|
||||||
|
let mut live = self.collect_live_usage(None, None, live);
|
||||||
|
let old_live = old.collect_live_usage(None, None, old_live);
|
||||||
// The splice translates the cumulative counters separately.
|
// The splice translates the cumulative counters separately.
|
||||||
live.usage.steps = old_live.usage.steps;
|
live.usage.steps = old_live.usage.steps;
|
||||||
live.usage.box_nodes_created = old_live.usage.box_nodes_created;
|
live.usage.box_nodes_created = old_live.usage.box_nodes_created;
|
||||||
|
|||||||
@@ -220,6 +220,12 @@ impl SourceRelocation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A shard whose ranges end strictly before the edit can retain every
|
||||||
|
/// span, including zero-width points at those earlier positions.
|
||||||
|
pub(super) fn preserves_prefix_through(&self, end: usize) -> bool {
|
||||||
|
end < self.unchanged_prefix
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn relocate_span(&self, span: Span) -> Option<Span> {
|
pub(super) fn relocate_span(&self, span: Span) -> Option<Span> {
|
||||||
if span.source != self.source {
|
if span.source != self.source {
|
||||||
return Some(span);
|
return Some(span);
|
||||||
|
|||||||
@@ -800,7 +800,47 @@ enum PdftexRevision {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const COMMAND_SHARDS: usize = 32;
|
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)>>>;
|
type OrderedCommandNames = Arc<OnceLock<Vec<(Arc<CommandName>, u64)>>>;
|
||||||
|
|
||||||
pub(super) fn command_hasher() -> &'static RandomState {
|
pub(super) fn command_hasher() -> &'static RandomState {
|
||||||
@@ -822,7 +862,9 @@ impl Default for CommandBindings {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let hasher = command_hasher();
|
let hasher = command_hasher();
|
||||||
Self {
|
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],
|
revisions: [0; COMMAND_SHARDS],
|
||||||
ordered_names: Arc::default(),
|
ordered_names: Arc::default(),
|
||||||
}
|
}
|
||||||
@@ -849,6 +891,7 @@ impl CommandBindings {
|
|||||||
|
|
||||||
fn get_hashed(&self, name: &CommandName, hash: u64) -> Option<&Meaning> {
|
fn get_hashed(&self, name: &CommandName, hash: u64) -> Option<&Meaning> {
|
||||||
self.shards[Self::shard_index(hash)]
|
self.shards[Self::shard_index(hash)]
|
||||||
|
.entries
|
||||||
.raw_entry()
|
.raw_entry()
|
||||||
.from_key_hashed_nocheck(hash, name)
|
.from_key_hashed_nocheck(hash, name)
|
||||||
.map(|(_, meaning)| meaning)
|
.map(|(_, meaning)| meaning)
|
||||||
@@ -867,16 +910,13 @@ impl CommandBindings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn insert_if_absent(&mut self, name: CommandName, meaning: Meaning) {
|
fn insert_if_absent(&mut self, name: CommandName, meaning: Meaning) {
|
||||||
if let hashbrown::hash_map::Entry::Vacant(entry) =
|
if self.shard_mut(&name).insert_if_absent(name.into(), meaning) {
|
||||||
self.shard_mut(&name).entry(name.into())
|
|
||||||
{
|
|
||||||
entry.insert(meaning);
|
|
||||||
self.invalidate_order();
|
self.invalidate_order();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(&mut self, name: &CommandName) {
|
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();
|
self.invalidate_order();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -890,11 +930,11 @@ impl CommandBindings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn len(&self) -> usize {
|
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)> {
|
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)> {
|
fn ordered(&self) -> impl ExactSizeIterator<Item = (&Arc<CommandName>, &Meaning)> {
|
||||||
@@ -921,7 +961,11 @@ impl CommandBindings {
|
|||||||
|
|
||||||
impl Hash for CommandBindings {
|
impl Hash for CommandBindings {
|
||||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
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 {
|
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]) {
|
fn write(&mut self, bytes: &[u8]) {
|
||||||
if bytes.len() > self.buffer.len() - self.used {
|
if bytes.len() > self.buffer.len() - self.used {
|
||||||
self.hasher.write(&self.buffer[..self.used]);
|
self.hasher.write(&self.buffer[..self.used]);
|
||||||
@@ -1610,6 +1692,60 @@ impl Default for State {
|
|||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub(super) fn collect_statistics(&self, c: &mut super::statistics::Collector) {
|
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;
|
use super::statistics::Ownership::Active;
|
||||||
for b in self.boxes.values() {
|
for b in self.boxes.values() {
|
||||||
c.native_box(b, Active);
|
c.native_box(b, Active);
|
||||||
@@ -1622,26 +1758,6 @@ impl State {
|
|||||||
}
|
}
|
||||||
return;
|
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.token_registers = self.toks.len() + self.xe_inter_char_tokens.len();
|
||||||
c.usage.state.parameter_entries =
|
c.usage.state.parameter_entries =
|
||||||
self.parameters.len() + self.token_parameters.len() + self.job_parameters.len();
|
self.parameters.len() + self.token_parameters.len() + self.job_parameters.len();
|
||||||
@@ -2485,7 +2601,7 @@ impl State {
|
|||||||
Arc::unwrap_or_clone(state.commands)
|
Arc::unwrap_or_clone(state.commands)
|
||||||
.shards
|
.shards
|
||||||
.into_iter()
|
.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) {
|
.filter_map(|(name, meaning)| match Arc::unwrap_or_clone(name) {
|
||||||
CommandName::Control(name) => Some((name, meaning)),
|
CommandName::Control(name) => Some((name, meaning)),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::execution::source_relocation_events::EventRelocation;
|
use crate::execution::{
|
||||||
|
Span, source_relocation::SourceRelocation, source_relocation_events::EventRelocation,
|
||||||
|
};
|
||||||
use crate::pdf_device::forms::FormContents;
|
use crate::pdf_device::forms::FormContents;
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@@ -13,7 +15,10 @@ impl State {
|
|||||||
// Copy only shards with a relocated macro. Ordinary format macros
|
// Copy only shards with a relocated macro. Ordinary format macros
|
||||||
// and macros from the byte-identical prefix keep their allocations.
|
// and macros from the byte-identical prefix keep their allocations.
|
||||||
for (index, shard) in commands.shards.iter().enumerate() {
|
for (index, shard) in commands.shards.iter().enumerate() {
|
||||||
for (name, meaning) in shard.iter() {
|
if shard.preserves_source_spans(relocation.map()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (name, meaning) in &shard.entries {
|
||||||
if let Meaning::Macro(body) = meaning {
|
if let Meaning::Macro(body) = meaning {
|
||||||
let relocated = relocation.relocate_macro(body)?;
|
let relocated = relocation.relocate_macro(body)?;
|
||||||
if !Arc::ptr_eq(body, &relocated) {
|
if !Arc::ptr_eq(body, &relocated) {
|
||||||
@@ -97,9 +102,84 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CommandShard {
|
||||||
|
fn preserves_source_spans(&self, relocation: &SourceRelocation) -> bool {
|
||||||
|
let bounds = self.source_bounds.get_or_init(|| {
|
||||||
|
let mut bounds = BTreeMap::<usize, Option<usize>>::new();
|
||||||
|
for meaning in self.entries.values() {
|
||||||
|
include_meaning_bounds(&mut bounds, meaning);
|
||||||
|
}
|
||||||
|
bounds
|
||||||
|
});
|
||||||
|
bounds.get(&relocation.source()).is_none_or(|end| {
|
||||||
|
end.is_some_and(|end| relocation.preserves_prefix_through(end))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn include_meaning_bounds(
|
||||||
|
bounds: &mut BTreeMap<usize, Option<usize>>,
|
||||||
|
meaning: &Meaning,
|
||||||
|
) {
|
||||||
|
let Meaning::Macro(body) = meaning else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut include = |span: Span| {
|
||||||
|
let end = bounds.entry(span.source).or_insert(Some(0));
|
||||||
|
*end = end.filter(|_| span.start <= span.end).map(|end| end.max(span.end));
|
||||||
|
};
|
||||||
|
for part in &body.replacement {
|
||||||
|
if let Replacement::Token(token) = part {
|
||||||
|
include(token.span);
|
||||||
|
if let Some(invocation) = token.invocation {
|
||||||
|
include(invocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn relocate_tokens(tokens: &mut [Token], relocation: &mut EventRelocation<'_>) -> Option<()> {
|
fn relocate_tokens(tokens: &mut [Token], relocation: &mut EventRelocation<'_>) -> Option<()> {
|
||||||
for token in tokens {
|
for token in tokens {
|
||||||
relocation.relocate_token(token)?;
|
relocation.relocate_token(token)?;
|
||||||
}
|
}
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn command_bounds_track_redefinitions_and_invocation_origins() {
|
||||||
|
use crate::execution::CatCode;
|
||||||
|
|
||||||
|
let map = SourceRelocation::new(0, b"First\nx\nKeep\n", b"First\nlonger\nKeep\n").unwrap();
|
||||||
|
let name = CommandName::Control("boundtest".into());
|
||||||
|
let meaning = |invocation| {
|
||||||
|
let mut token = Token::new(
|
||||||
|
TokenKind::Character('x', CatCode::Letter),
|
||||||
|
Span { source: 0, start: 0, end: 1 },
|
||||||
|
);
|
||||||
|
token.invocation = invocation;
|
||||||
|
Meaning::Macro(Arc::new(Macro {
|
||||||
|
prefix: Vec::new(),
|
||||||
|
parameters: Vec::new(),
|
||||||
|
parameter_chars: Vec::new(),
|
||||||
|
replacement: vec![Replacement::Token(token)],
|
||||||
|
long: false,
|
||||||
|
outer: false,
|
||||||
|
protected: false,
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut state = State::default();
|
||||||
|
state.define(name.clone(), meaning(None), true);
|
||||||
|
state.relocate_source(&mut EventRelocation::new(&map)).unwrap();
|
||||||
|
let before = state.clone();
|
||||||
|
let token_span = Span { source: 0, start: 8, end: 9 };
|
||||||
|
state.define(name.clone(), meaning(Some(token_span)), true);
|
||||||
|
state.relocate_source(&mut EventRelocation::new(&map)).unwrap();
|
||||||
|
let mapped = map.relocate_span(token_span).unwrap();
|
||||||
|
assert_ne!(mapped, token_span);
|
||||||
|
assert_eq!(state.meaning(&name), meaning(Some(mapped)));
|
||||||
|
assert_eq!(before.meaning(&name), meaning(None));
|
||||||
|
|
||||||
|
state.define(name, meaning(Some(Span { source: 0, start: 2, end: 1 })), true);
|
||||||
|
assert!(state.relocate_source(&mut EventRelocation::new(&map)).is_none());
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Measured native storage and execution resources, not invented TeX arena words.
|
//! Measured native storage and execution resources, not invented TeX arena words.
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeMap, BTreeSet, HashSet},
|
collections::{BTreeSet, HashMap, HashSet, hash_map::Entry},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -456,7 +456,7 @@ pub(super) enum Ownership {
|
|||||||
Active,
|
Active,
|
||||||
Retained,
|
Retained,
|
||||||
}
|
}
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
enum Allocation {
|
enum Allocation {
|
||||||
Macro(usize),
|
Macro(usize),
|
||||||
Tokens(usize, usize),
|
Tokens(usize, usize),
|
||||||
@@ -465,22 +465,27 @@ enum Allocation {
|
|||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
struct AliasVisits {
|
struct AliasVisits {
|
||||||
ordinals: BTreeMap<Allocation, usize>,
|
ordinals: HashMap<Allocation, usize>,
|
||||||
visits: Vec<(Allocation, usize)>,
|
visits: Vec<(Allocation, usize)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AliasVisits {
|
impl AliasVisits {
|
||||||
fn record(&mut self, allocation: Allocation) {
|
fn record(&mut self, allocation: Allocation) -> bool {
|
||||||
let next = self.ordinals.len();
|
let next = self.ordinals.len();
|
||||||
let ordinal = *self.ordinals.entry(allocation).or_insert(next);
|
let (ordinal, first) = match self.ordinals.entry(allocation) {
|
||||||
|
Entry::Occupied(entry) => (*entry.get(), false),
|
||||||
|
Entry::Vacant(entry) => (*entry.insert(next), true),
|
||||||
|
};
|
||||||
self.visits.push((allocation, ordinal));
|
self.visits.push((allocation, ordinal));
|
||||||
|
first
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub(super) struct Collector {
|
pub(super) struct Collector {
|
||||||
pub usage: ResourceUsage,
|
pub usage: ResourceUsage,
|
||||||
// Alias visits keep traversal order separately from this membership index.
|
// A tracked visit uses its ordinal index for deduplication. The
|
||||||
|
// membership sets cover normal collection and any visits before tracking.
|
||||||
macros: HashSet<usize>,
|
macros: HashSet<usize>,
|
||||||
token_lists: BTreeSet<(usize, usize)>,
|
token_lists: BTreeSet<(usize, usize)>,
|
||||||
shared_lists: Option<BTreeSet<usize>>,
|
shared_lists: Option<BTreeSet<usize>>,
|
||||||
@@ -495,21 +500,14 @@ impl Collector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn tracking_aliases(mut self) -> Self {
|
pub(super) fn tracking_aliases(mut self) -> Self {
|
||||||
self.aliases = Some(AliasVisits::default());
|
self.aliases.get_or_insert_with(AliasVisits::default);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn tracks_aliases(&self) -> bool {
|
|
||||||
self.aliases.is_some()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tracks_allocation(&mut self, allocation: Allocation) {
|
|
||||||
if let Some(aliases) = &mut self.aliases {
|
|
||||||
aliases.record(allocation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn contains(&self, allocation: Allocation) -> bool {
|
fn contains(&self, allocation: Allocation) -> bool {
|
||||||
|
if self.aliases.as_ref().is_some_and(|aliases| aliases.ordinals.contains_key(&allocation)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
match allocation {
|
match allocation {
|
||||||
Allocation::Macro(address) => self.macros.contains(&address),
|
Allocation::Macro(address) => self.macros.contains(&address),
|
||||||
Allocation::Tokens(address, len) => self.token_lists.contains(&(address, len)),
|
Allocation::Tokens(address, len) => self.token_lists.contains(&(address, len)),
|
||||||
@@ -575,8 +573,13 @@ impl Collector {
|
|||||||
|
|
||||||
pub fn macro_body(&mut self, m: &Arc<Macro>) {
|
pub fn macro_body(&mut self, m: &Arc<Macro>) {
|
||||||
let address = Arc::as_ptr(m) as usize;
|
let address = Arc::as_ptr(m) as usize;
|
||||||
self.tracks_allocation(Allocation::Macro(address));
|
let first = if let Some(aliases) = &mut self.aliases {
|
||||||
if self.macros.insert(address) {
|
aliases.record(Allocation::Macro(address))
|
||||||
|
&& (self.macros.is_empty() || !self.macros.contains(&address))
|
||||||
|
} else {
|
||||||
|
self.macros.insert(address)
|
||||||
|
};
|
||||||
|
if first {
|
||||||
self.usage.unique_macro_bodies += 1;
|
self.usage.unique_macro_bodies += 1;
|
||||||
self.usage.macro_token_cells += m.prefix.len()
|
self.usage.macro_token_cells += m.prefix.len()
|
||||||
+ m.parameters.iter().map(Vec::len).sum::<usize>()
|
+ m.parameters.iter().map(Vec::len).sum::<usize>()
|
||||||
@@ -585,11 +588,15 @@ impl Collector {
|
|||||||
}
|
}
|
||||||
pub fn shared_tokens(&mut self, t: &Arc<[Token]>) {
|
pub fn shared_tokens(&mut self, t: &Arc<[Token]>) {
|
||||||
let address = t.as_ptr() as usize;
|
let address = t.as_ptr() as usize;
|
||||||
if !t.is_empty() {
|
let len = t.len();
|
||||||
self.tracks_allocation(Allocation::Tokens(address, t.len()));
|
let first = if let Some(aliases) = &mut self.aliases && len != 0 {
|
||||||
}
|
aliases.record(Allocation::Tokens(address, len))
|
||||||
if self.token_lists.insert((address, t.len())) {
|
&& !self.token_lists.contains(&(address, len))
|
||||||
self.usage.token_list_cells += t.len();
|
} else {
|
||||||
|
self.token_lists.insert((address, len))
|
||||||
|
};
|
||||||
|
if first {
|
||||||
|
self.usage.token_list_cells += len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn material(&mut self, o: Ownership) -> &mut MaterialUsage {
|
fn material(&mut self, o: Ownership) -> &mut MaterialUsage {
|
||||||
@@ -725,13 +732,15 @@ impl Collector {
|
|||||||
self.usage.active_material.ligature_work_items += b.ligature_work_items();
|
self.usage.active_material.ligature_work_items += b.ligature_work_items();
|
||||||
}
|
}
|
||||||
fn visit_shared<T: ?Sized>(&mut self, list: &Arc<T>) -> bool {
|
fn visit_shared<T: ?Sized>(&mut self, list: &Arc<T>) -> bool {
|
||||||
|
let Some(seen) = &mut self.shared_lists else {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
let address = Arc::as_ptr(list).cast::<()>() as usize;
|
let address = Arc::as_ptr(list).cast::<()>() as usize;
|
||||||
if self.shared_lists.is_some() {
|
if let Some(aliases) = &mut self.aliases {
|
||||||
self.tracks_allocation(Allocation::Material(address));
|
aliases.record(Allocation::Material(address)) && !seen.contains(&address)
|
||||||
|
} else {
|
||||||
|
seen.insert(address)
|
||||||
}
|
}
|
||||||
self.shared_lists
|
|
||||||
.as_mut()
|
|
||||||
.is_none_or(|seen| seen.insert(address))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shared_diagnostics(
|
pub fn shared_diagnostics(
|
||||||
@@ -911,6 +920,44 @@ mod tests {
|
|||||||
).is_none());
|
).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tracked_macro_counts_share_the_ordinal_index_without_losing_visits() {
|
||||||
|
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||||
|
let body = Arc::new(Macro {
|
||||||
|
prefix: Vec::new(),
|
||||||
|
parameters: Vec::new(),
|
||||||
|
parameter_chars: Vec::new(),
|
||||||
|
replacement: vec![Replacement::Token(token.clone())],
|
||||||
|
long: false,
|
||||||
|
outer: false,
|
||||||
|
protected: false,
|
||||||
|
});
|
||||||
|
let independent = Arc::new((*body).clone());
|
||||||
|
let tokens: Arc<[Token]> = Arc::from([token]);
|
||||||
|
let mut history = Collector::default();
|
||||||
|
let mut live = Collector::default().tracking_aliases();
|
||||||
|
for collector in [&mut history, &mut live] {
|
||||||
|
collector.macro_body(&body);
|
||||||
|
collector.shared_tokens(&tokens);
|
||||||
|
collector.macro_body(&body);
|
||||||
|
collector.macro_body(&independent);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(live.usage, history.usage);
|
||||||
|
assert_eq!(live.usage.unique_macro_bodies, 2);
|
||||||
|
assert!(live.macros.is_empty());
|
||||||
|
assert!(live.token_lists.is_empty());
|
||||||
|
let visits = &live.aliases.as_ref().unwrap().visits;
|
||||||
|
assert_eq!(visits.iter().map(|(_, ordinal)| *ordinal).collect::<Vec<_>>(), [0, 1, 0, 2]);
|
||||||
|
assert!(visits.iter().all(|(allocation, _)| {
|
||||||
|
history.contains(*allocation) && live.contains(*allocation)
|
||||||
|
}));
|
||||||
|
|
||||||
|
let mut previously_counted = history.clone().tracking_aliases();
|
||||||
|
previously_counted.macro_body(&body);
|
||||||
|
assert_eq!(previously_counted.usage, history.usage);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn history_equivalence_keeps_aliases_that_cross_the_boundary() {
|
fn history_equivalence_keeps_aliases_that_cross_the_boundary() {
|
||||||
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
let token = Token::new(TokenKind::Character('A', CatCode::Letter), Span::default());
|
||||||
|
|||||||
@@ -123,6 +123,39 @@ p50 分别为 33.2 和 33.3 ms,进程峰值 RSS 为 603,280 KiB。
|
|||||||
浏览器原始记录在 `target/p4-a4-retained-history/`,单次阶段
|
浏览器原始记录在 `target/p4-a4-retained-history/`,单次阶段
|
||||||
数据在 `target/p4-a4-suffix-proof/run-qcghpnrj/`。
|
数据在 `target/p4-a4-suffix-proof/run-qcghpnrj/`。
|
||||||
|
|
||||||
|
## 2026-12-02 降低后缀会合的遍历成本
|
||||||
|
|
||||||
|
沿候选分支保留不等长事件历史的复用。旧优化版 A4 的带采样诊断
|
||||||
|
显示,一次成功接缝的记账有约 88 ms 花在两遍实时状态统计,组装
|
||||||
|
中重算最终指纹约 56 ms,比较中的状态来源换算约 26 ms。原始
|
||||||
|
作用域数据在 `target/p4-a4-suffix-proof/release-profile/`。
|
||||||
|
|
||||||
|
命令表分片保留宏替换文本和调用位置的来源范围。范围严格早于
|
||||||
|
修改位置时,沿用共享分片。涉及修改区或有异常范围时,继续逐个
|
||||||
|
换算来源。写入新定义同步扩展范围。匹配两版记账时,字段和命令
|
||||||
|
键保持逐项对应,用同一遍历顺序核对别名。首次访问和分配去重
|
||||||
|
共用同一份索引。状态指纹仍按原有命令顺序和字节序列计算。
|
||||||
|
|
||||||
|
8 项内核单测和 15 项管线回归通过。冻结 A4 原生对照在两个同配置
|
||||||
|
优化构建上各跑 3 次,全部与各自独立全量一致,且两版 PDF 指纹相同。
|
||||||
|
两次成功接缝都跳过 20,618 步。保留记账分别从 64.38 降至 18.65 ms、
|
||||||
|
从 38.73 降至 18.30 ms。状态比较仍约 30–32 ms,组装仍约 58–64 ms。
|
||||||
|
阶段原始数据在 `target/p4-a4-shard-relocation/a4-main-only/`。
|
||||||
|
|
||||||
|
同机 Edge 实测主文件、子文件各 30 次。主文件 29 次各跳过
|
||||||
|
20,618 步,子文件仍执行后缀。全部 60 次的 PDF、SVG 和来源映射
|
||||||
|
指纹与主线基线逐项一致。进程峰值 RSS 为 603,116 KiB。
|
||||||
|
|
||||||
|
| A4 热编辑 | 主线可见 p50 / p95 | 上一候选 p50 / p95 | 本轮候选 p50 / p95 |
|
||||||
|
| --- | ---: | ---: | ---: |
|
||||||
|
| 主文件正文 | 230.3 / 256.3 ms | 325.5 / 512.1 ms | 285.4 / 497.4 ms |
|
||||||
|
| 子文件正文 | 184.1 / 241.5 ms | 185.4 / 266.7 ms | 188.4 / 337.5 ms |
|
||||||
|
|
||||||
|
本轮候选继续保存在 `perf/retained-history-suffix`。浏览器 100 ms
|
||||||
|
门槛仍开放。性能记录、构建指纹和逐次结果在
|
||||||
|
`target/p4-a4-shard-relocation/`。下一轮继续压缩仍占主要时间的
|
||||||
|
状态指纹,并认证子文件耗尽后的前后缀资源读证据。
|
||||||
|
|
||||||
## 2026-12-02 XeTeX 整行编辑复用
|
## 2026-12-02 XeTeX 整行编辑复用
|
||||||
|
|
||||||
XeTeX 扫描器保留已读物理行采用的分行规则。UTF-8、Bytes 及其他
|
XeTeX 扫描器保留已读物理行采用的分行规则。UTF-8、Bytes 及其他
|
||||||
|
|||||||
Reference in new issue
Block a user