From 3504a43bde80f5d16a0bedaa464c295beb5e04a7 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 2 Dec 2026 02:55:20 +0800 Subject: [PATCH] Compare command shard entries directly when their order matches --- crates/texrra-tex-core/src/execution/state.rs | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/crates/texrra-tex-core/src/execution/state.rs b/crates/texrra-tex-core/src/execution/state.rs index bdc2e4e..b18e16d 100644 --- a/crates/texrra-tex-core/src/execution/state.rs +++ b/crates/texrra-tex-core/src/execution/state.rs @@ -839,7 +839,21 @@ impl CommandShard { impl PartialEq for CommandShard { fn eq(&self, other: &Self) -> bool { - self.entries == other.entries + if self.entries.len() != other.entries.len() { + return false; + } + + // Copy-on-write shards with only value changes can keep their bucket + // order. Verify each key before comparing its meaning. + for ((name, meaning), (other_name, other_meaning)) in self.entries.iter().zip(&other.entries) { + if name != other_name { + return self.entries == other.entries; + } + if meaning != other_meaning { + return false; + } + } + true } } @@ -1234,6 +1248,50 @@ fn paired_statistics_keep_aliases_from_changed_to_shared_command_shards() { assert!(same.has_equivalent_history(&old_counts, &history, &history)); } +#[test] +fn command_shard_equality_checks_values_in_matching_bucket_order() { + let mut before = CommandShard::new(command_hasher().clone()); + let name = Arc::new(CommandName::Control("first".into())); + before.insert(name.clone(), Meaning::Count(3)); + before.insert(Arc::new(CommandName::Control("second".into())), Meaning::Count(7)); + + let mut after = before.clone(); + assert_eq!(before, after); + *after.entries.get_mut(&name).unwrap() = Meaning::Count(7); + assert!(before.entries.keys().eq(after.entries.keys())); + assert_ne!(before, after); +} + +#[test] +fn command_shard_equality_matches_keys_when_bucket_order_differs() { + let mut before = CommandShard::new(command_hasher().clone()); + for index in 0..32 { + before.insert( + Arc::new(CommandName::Control(format!("binding-{index}"))), + Meaning::Count(index), + ); + } + + // Find a genuinely different iteration order for these same bindings + // without depending on a particular hashbrown bucket layout. + let mut after = (0..16) + .find_map(|_| { + let mut candidate = CommandShard::new(RandomState::new()); + for (name, meaning) in &before.entries { + candidate.insert(name.clone(), meaning.clone()); + } + (!before.entries.keys().eq(candidate.entries.keys())).then_some(candidate) + }) + .expect("different hash seeds should give different bucket orders"); + assert_eq!(before, after); + + let (name, _) = before.entries.keys().zip(after.entries.keys()) + .find(|(name, other_name)| name != other_name) + .unwrap(); + *after.entries.get_mut(name).unwrap() = Meaning::Count(100); + assert_ne!(before, after); +} + #[test] fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() { let mut state = State::initex().with_etex();