diff --git a/crates/texrra-tex-core/src/execution/engine/statistics.rs b/crates/texrra-tex-core/src/execution/engine/statistics.rs index 614dce2..52f3215 100644 --- a/crates/texrra-tex-core/src/execution/engine/statistics.rs +++ b/crates/texrra-tex-core/src/execution/engine/statistics.rs @@ -276,9 +276,9 @@ impl Executor { } let (history, old_history) = Self::collect_event_usage_pair(&self.events, &old.events, collector()); - let mut live = collector().tracking_aliases(); - let mut old_live = collector().tracking_aliases(); - self.state.collect_compared_statistics(&old.state, &mut live, &mut old_live)?; + let (live, old_live) = self.state.collect_compared_statistics( + &old.state, collector().tracking_aliases(), + )?; 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. diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index cc91960..c97ae3f 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -1192,6 +1192,50 @@ fn cached_macro_fingerprint_preserves_aliases_mutations_and_uncached_hashes() { assert_eq!(state.fingerprint(), uncached); } +#[test] +fn paired_statistics_keep_aliases_from_changed_to_shared_command_shards() { + use super::statistics::Collector; + + let body = Arc::new(Macro { + prefix: Vec::new(), + parameters: Vec::new(), + parameter_chars: Vec::new(), + replacement: Vec::new(), + long: false, + outer: false, + protected: false, + }); + let independent = Arc::new(body.as_ref().clone()); + let first = CommandName::Control("common-definition".into()); + let second = CommandName::Control("other-common-definition".into()); + let shard = |name: &CommandName| CommandBindings::shard_index(command_hasher().hash_one(name)); + let shared_shards = [shard(&first), shard(&second)]; + let edited = (0..128) + .map(|index| CommandName::Control(format!("edited-alias-{index}"))) + .find(|name| !shared_shards.contains(&shard(name))) + .unwrap(); + + let mut prior = State::default(); + prior.define(first, Meaning::Macro(body.clone()), true); + prior.define(second, Meaning::Macro(independent.clone()), true); + prior.define(edited.clone(), Meaning::Macro(body), true); + let mut current = prior.clone(); + current.define(edited, Meaning::Macro(independent), true); + assert_eq!(current, prior); + + let history = Collector::default(); + let (new_counts, old_counts) = current + .collect_compared_statistics(&prior, Collector::default().tracking_aliases()) + .unwrap(); + assert_eq!(new_counts.usage, old_counts.usage); + assert!(!new_counts.has_equivalent_history(&old_counts, &history, &history)); + + let (same, old_counts) = prior + .collect_compared_statistics(&prior, Collector::default().tracking_aliases()) + .unwrap(); + assert!(same.has_equivalent_history(&old_counts, &history, &history)); +} + #[test] fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() { let mut state = State::initex().with_etex(); @@ -1718,31 +1762,42 @@ impl State { 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) { + mut current_usage: super::statistics::Collector, + ) -> Option<(super::statistics::Collector, super::statistics::Collector)> { + let mut old_usage = if current_usage.material_only() { + current_usage.clone() + } else { + let pairs = self.commands.shards.iter().zip(&old.commands.shards); + current_usage.usage.state.command_entries = old.commands.len(); + for (current, prior) in pairs.clone() { 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); + if Arc::ptr_eq(current, prior) { + for (name, meaning) in &prior.entries { + Self::collect_command_statistics(&mut current_usage, name, meaning); + } } } - } - self.collect_state_storage(current_usage); - old.collect_state_storage(old_usage); - Some(()) + + // Both alias walks start with the exact same live allocations. + // Copy their ordinals before counting the remaining bindings so + // a later alias to a shared macro still has the original ordinal. + let mut old_usage = current_usage.clone(); + for (current, prior) in pairs { + if !Arc::ptr_eq(current, prior) { + for (name, old_meaning) in &prior.entries { + let meaning = current.entries.get(name)?; + Self::collect_command_statistics(&mut current_usage, name, meaning); + Self::collect_command_statistics(&mut old_usage, name, old_meaning); + } + } + } + old_usage + }; + self.collect_state_storage(&mut current_usage); + old.collect_state_storage(&mut old_usage); + Some((current_usage, old_usage)) } fn collect_command_statistics( diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index bfc239e..31346fb 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -262,6 +262,16 @@ Edge 的余量需继续按浏览器请求和响应调度分解。 峰值 RSS 为 618,732 KiB。100 ms 门槛仍开放。 原始浏览器、构建和直连对照在 `target/p4-a4-child-release/`。 +## 2026-12-02 共享命令分片的记账 + +事件历史与执行状态的跨接缝证明仍核对分配别名。两版共享同一 +命令分片时只走一次已共享绑定,再从该遍历的别名序号出发分别 +统计两版独立分片和活动状态。单测证明两版宏值及计数完全相等时, +跨分片别名指向另一个同值宏仍会拒绝复用。五项内核单测及五项管线 +回归通过,覆盖重复子文件读取、盒计数、不同长度的事件历史、 +连续检查点和 XeTeX UTF-16 整行编辑。验证原始记录在 +`target/p4-shared-shard-accounting/`。浏览器时延待同配置发布构建实测。 + ## 2026-12-02 XeTeX 整行编辑复用 XeTeX 扫描器保留已读物理行采用的分行规则。UTF-8、Bytes 及其他