Reuse verified command hash prefixes across rebuilt key orders
This commit is contained in:
1 parent
8f4ad600ef
commit
8ed0b70d5e
1 file changed
+46
-10
@@ -1074,13 +1074,22 @@ impl CommandBindings {
|
|||||||
|
|
||||||
impl CommandBindings {
|
impl CommandBindings {
|
||||||
fn first_changed_binding(&self, prior: &Self) -> Option<usize> {
|
fn first_changed_binding(&self, prior: &Self) -> Option<usize> {
|
||||||
// Value overwrites can retain the order object. Insertion and
|
|
||||||
// removal replace it before a different ordered key set is used.
|
|
||||||
if !Arc::ptr_eq(&self.ordered_names, &prior.ordered_names) {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let names = prior.ordered_names.get()?;
|
let names = prior.ordered_names.get()?;
|
||||||
|
let shared_order = Arc::ptr_eq(&self.ordered_names, &prior.ordered_names);
|
||||||
let mut first = None;
|
let mut first = None;
|
||||||
|
if !shared_order {
|
||||||
|
let current = self.ordered_names.get()?;
|
||||||
|
// The very first hash input is the number of bindings.
|
||||||
|
if current.len() != names.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
for ((new, _), (old, _)) in current.iter().zip(names) {
|
||||||
|
if new != old {
|
||||||
|
first = Some(new.as_ref().min(old.as_ref()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let changed_groups = self.shard_groups.iter().zip(&prior.shard_groups)
|
let changed_groups = self.shard_groups.iter().zip(&prior.shard_groups)
|
||||||
.filter(|(current, old)| !Arc::ptr_eq(current, old));
|
.filter(|(current, old)| !Arc::ptr_eq(current, old));
|
||||||
for (current, old) in changed_groups.flat_map(|(current, old)| current.shards.iter().zip(&old.shards)) {
|
for (current, old) in changed_groups.flat_map(|(current, old)| current.shards.iter().zip(&old.shards)) {
|
||||||
@@ -1088,11 +1097,14 @@ impl CommandBindings {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (name, meaning) in ¤t.entries {
|
for (name, meaning) in ¤t.entries {
|
||||||
|
let name = name.as_ref();
|
||||||
if first.is_some_and(|first| name >= first) {
|
if first.is_some_and(|first| name >= first) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let unchanged = match (meaning, old.entries.get(name)?) {
|
let unchanged = match (meaning, old.entries.get(name)?) {
|
||||||
(Meaning::Macro(current), Meaning::Macro(old)) => Arc::ptr_eq(current, old),
|
(Meaning::Macro(current), Meaning::Macro(old)) => {
|
||||||
|
Arc::ptr_eq(current, old) || (!shared_order && current == old)
|
||||||
|
}
|
||||||
(current, old) => current == old,
|
(current, old) => current == old,
|
||||||
};
|
};
|
||||||
if !unchanged {
|
if !unchanged {
|
||||||
@@ -1100,10 +1112,10 @@ impl CommandBindings {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match first {
|
Some(match first {
|
||||||
Some(name) => names.binary_search_by(|(key, _)| key.cmp(name)).ok(),
|
Some(name) => names.partition_point(|(key, _)| key.as_ref() < name),
|
||||||
None => Some(names.len()),
|
None => names.len(),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_fingerprint(&self, hasher: &mut FingerprintHasher, prior: Option<&Self>) {
|
fn hash_fingerprint(&self, hasher: &mut FingerprintHasher, prior: Option<&Self>) {
|
||||||
@@ -1356,6 +1368,30 @@ fn state_fingerprint_resumes_before_the_first_changed_binding() {
|
|||||||
revised.command_bindings_mut().remove(&name(0));
|
revised.command_bindings_mut().remove(&name(0));
|
||||||
assert!(revised.commands.first_changed_binding(&original.commands).is_none());
|
assert!(revised.commands.first_changed_binding(&original.commands).is_none());
|
||||||
assert_eq!(revised.fingerprint_with_prior(Some(&original)), direct(&revised));
|
assert_eq!(revised.fingerprint_with_prior(Some(&original)), direct(&revised));
|
||||||
|
|
||||||
|
// Replaying definitions can rebuild the sorted key set even when its
|
||||||
|
// contents match. A key replacement of equal count also preserves the
|
||||||
|
// hash before the first changed key, in either ordering direction.
|
||||||
|
let mut rebuilt = original.clone();
|
||||||
|
let restored = name(stride + 3);
|
||||||
|
rebuilt.command_bindings_mut().remove(&restored);
|
||||||
|
rebuilt.define(restored.clone(), Meaning::Count(0), true);
|
||||||
|
assert_eq!(rebuilt.commands.ordered().len(), stride * 3);
|
||||||
|
assert!(!Arc::ptr_eq(&rebuilt.commands.ordered_names, &original.commands.ordered_names));
|
||||||
|
assert_eq!(rebuilt.commands.first_changed_binding(&original.commands), Some(stride * 3));
|
||||||
|
assert_eq!(rebuilt.fingerprint_with_prior(Some(&original)), original_hash);
|
||||||
|
|
||||||
|
for (replacement, first) in [
|
||||||
|
(CommandName::Control("binding-0000a".into()), 1),
|
||||||
|
(CommandName::Control("later".into()), stride + 3),
|
||||||
|
] {
|
||||||
|
let mut changed_keys = original.clone();
|
||||||
|
changed_keys.command_bindings_mut().remove(&restored);
|
||||||
|
changed_keys.define(replacement, Meaning::Count(2), true);
|
||||||
|
assert_eq!(changed_keys.commands.ordered().len(), stride * 3);
|
||||||
|
assert_eq!(changed_keys.commands.first_changed_binding(&original.commands), Some(first));
|
||||||
|
assert_eq!(changed_keys.fingerprint_with_prior(Some(&original)), direct(&changed_keys));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in new issue
Block a user