Compare command shard entries directly when their order matches

This commit is contained in:
gloamxun committed 2026-12-02 02:55:20 +08:00
1 parent f1b95bd749
commit 3504a43bde
1 file changed
+59 -1
+59 -1
View File
@@ -839,7 +839,21 @@ impl CommandShard {
impl PartialEq for CommandShard { impl PartialEq for CommandShard {
fn eq(&self, other: &Self) -> bool { 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)); 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] #[test]
fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() { fn command_snapshots_share_unmodified_shards_and_keep_ordered_identity() {
let mut state = State::initex().with_etex(); let mut state = State::initex().with_etex();