Revert "Replay verified unchanged command blocks when hashing state"
This reverts commit 89e773b9f8.
This commit is contained in:
1 parent
d1e9f61559
commit
e659dcc86d
2 files changed
+48
-220
No files matched your search
@@ -1,7 +1,7 @@
|
||||
mod fingerprint;
|
||||
pub(super) mod source_relocation;
|
||||
|
||||
use fingerprint::{FingerprintHasher, HashInput, MacroFingerprintInputs};
|
||||
use fingerprint::{FingerprintHasher, MacroFingerprintInputs};
|
||||
|
||||
use hashbrown::{HashMap, hash_map::RawEntryMut};
|
||||
use std::collections::BTreeMap;
|
||||
@@ -896,31 +896,9 @@ impl PartialEq for CommandShardGroup {
|
||||
|
||||
impl Eq for CommandShardGroup {}
|
||||
|
||||
type OrderedCommandNames = Arc<OnceLock<Vec<(Arc<CommandName>, u64)>>>;
|
||||
|
||||
const COMMAND_HASH_CHECKPOINT_STRIDE: usize = 128;
|
||||
const MIN_COMMANDS_TO_RECORD_BLOCKS: usize = 4096;
|
||||
const MAX_COMMAND_BLOCK_INPUT_BYTES: usize = 12 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct OrderedCommandNames {
|
||||
names: OnceLock<Vec<(Arc<CommandName>, u64)>>,
|
||||
inputs: OnceLock<CommandBlockInputs>,
|
||||
}
|
||||
|
||||
// One bounded donor per shared key order. Shared shards certify unchanged
|
||||
// values, while ordered bytes can be fed after any changed prefix.
|
||||
struct CommandBlockInputs {
|
||||
groups: [Arc<CommandShardGroup>; COMMAND_SHARD_GROUPS],
|
||||
blocks: Vec<Option<Box<[u8]>>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for CommandBlockInputs {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("CommandBlockInputs")
|
||||
.field("block_count", &self.blocks.len())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CommandHashCheckpoints {
|
||||
@@ -938,8 +916,8 @@ pub(super) fn command_hasher() -> &'static RandomState {
|
||||
#[derive(Clone, Debug)]
|
||||
struct CommandBindings {
|
||||
shard_groups: [Arc<CommandShardGroup>; COMMAND_SHARD_GROUPS],
|
||||
// Redefinitions can share key order and proven unchanged hash inputs.
|
||||
ordered_names: Arc<OrderedCommandNames>,
|
||||
// Cache names only so redefinitions can share the same order.
|
||||
ordered_names: OrderedCommandNames,
|
||||
fingerprint_inputs: Arc<Mutex<MacroFingerprintInputs>>,
|
||||
hash_checkpoints: OnceLock<Arc<CommandHashCheckpoints>>,
|
||||
}
|
||||
@@ -1058,8 +1036,7 @@ impl CommandBindings {
|
||||
|
||||
fn invalidate_order(&mut self) {
|
||||
if let Some(order) = Arc::get_mut(&mut self.ordered_names) {
|
||||
order.names.take();
|
||||
order.inputs.take();
|
||||
order.take();
|
||||
} else {
|
||||
self.ordered_names = Arc::default();
|
||||
}
|
||||
@@ -1073,33 +1050,25 @@ impl CommandBindings {
|
||||
self.shards().flat_map(|shard| shard.entries.iter())
|
||||
}
|
||||
|
||||
fn ordered_names(&self) -> &[(Arc<CommandName>, u64)] {
|
||||
self.ordered_names.names.get_or_init(|| {
|
||||
let hasher = command_hasher();
|
||||
let mut names = Vec::with_capacity(self.len());
|
||||
names.extend(
|
||||
self.iter()
|
||||
.map(|(name, _)| (name.clone(), hasher.hash_one(name))),
|
||||
);
|
||||
names.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
|
||||
names
|
||||
})
|
||||
}
|
||||
|
||||
fn ordered_entries<'a>(
|
||||
&'a self,
|
||||
names: &'a [(Arc<CommandName>, u64)],
|
||||
) -> impl ExactSizeIterator<Item = (&'a Arc<CommandName>, &'a Meaning)> {
|
||||
names.iter().map(|(name, hash)| {
|
||||
let meaning = self
|
||||
.get_hashed(name, *hash)
|
||||
.expect("ordered command names match the binding table");
|
||||
(name, meaning)
|
||||
})
|
||||
}
|
||||
|
||||
fn ordered(&self) -> impl ExactSizeIterator<Item = (&Arc<CommandName>, &Meaning)> {
|
||||
self.ordered_entries(self.ordered_names())
|
||||
self.ordered_names
|
||||
.get_or_init(|| {
|
||||
let hasher = command_hasher();
|
||||
let mut names = Vec::with_capacity(self.len());
|
||||
names.extend(
|
||||
self.iter()
|
||||
.map(|(name, _)| (name.clone(), hasher.hash_one(name))),
|
||||
);
|
||||
names.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
|
||||
names
|
||||
})
|
||||
.iter()
|
||||
.map(|(name, hash)| {
|
||||
let meaning = self
|
||||
.get_hashed(name, *hash)
|
||||
.expect("ordered command names match the binding table");
|
||||
(name, meaning)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1110,7 +1079,7 @@ impl CommandBindings {
|
||||
if !Arc::ptr_eq(&self.ordered_names, &prior.ordered_names) {
|
||||
return None;
|
||||
}
|
||||
let names = prior.ordered_names.names.get()?;
|
||||
let names = prior.ordered_names.get()?;
|
||||
let mut first = None;
|
||||
let changed_groups = self.shard_groups.iter().zip(&prior.shard_groups)
|
||||
.filter(|(current, old)| !Arc::ptr_eq(current, old));
|
||||
@@ -1137,69 +1106,13 @@ impl CommandBindings {
|
||||
}
|
||||
}
|
||||
|
||||
fn record_block_inputs(&self, names: &[(Arc<CommandName>, u64)]) -> CommandBlockInputs {
|
||||
let mut remaining = MAX_COMMAND_BLOCK_INPUT_BYTES;
|
||||
let blocks = names
|
||||
.chunks(COMMAND_HASH_CHECKPOINT_STRIDE)
|
||||
.map(|names| {
|
||||
if remaining == 0 {
|
||||
return None;
|
||||
}
|
||||
let mut record = HashInput::new(remaining);
|
||||
for entry in self.ordered_entries(names) {
|
||||
entry.hash(&mut record);
|
||||
}
|
||||
let bytes = record.into_bytes()?;
|
||||
remaining -= bytes.len();
|
||||
Some(bytes)
|
||||
})
|
||||
.collect();
|
||||
CommandBlockInputs {
|
||||
groups: self.shard_groups.clone(),
|
||||
blocks,
|
||||
}
|
||||
}
|
||||
|
||||
fn changed_input_blocks(
|
||||
&self,
|
||||
names: &[(Arc<CommandName>, u64)],
|
||||
donor: &CommandBlockInputs,
|
||||
) -> Vec<bool> {
|
||||
let mut changed = vec![false; donor.blocks.len()];
|
||||
for (current, old) in self.shard_groups.iter().zip(&donor.groups) {
|
||||
if Arc::ptr_eq(current, old) {
|
||||
continue;
|
||||
}
|
||||
for (current, old) in current.shards.iter().zip(&old.shards) {
|
||||
if Arc::ptr_eq(current, old) {
|
||||
continue;
|
||||
}
|
||||
for (name, meaning) in ¤t.entries {
|
||||
// Both versions share the key order and partition. Only
|
||||
// values can differ, including relocated macro spans.
|
||||
let unchanged = match (meaning, &old.entries[name]) {
|
||||
(Meaning::Macro(current), Meaning::Macro(old)) => Arc::ptr_eq(current, old),
|
||||
(current, old) => current == old,
|
||||
};
|
||||
if !unchanged {
|
||||
let index = names
|
||||
.binary_search_by(|(key, _)| key.cmp(name))
|
||||
.expect("shared command order contains every name");
|
||||
changed[index / COMMAND_HASH_CHECKPOINT_STRIDE] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
fn hash_fingerprint(&self, hasher: &mut FingerprintHasher, prior: Option<&Self>) {
|
||||
if let Some(checkpoints) = self.hash_checkpoints.get() {
|
||||
hasher.restore(&checkpoints.end);
|
||||
return;
|
||||
}
|
||||
let names = self.ordered_names();
|
||||
let count = names.len();
|
||||
let entries = self.ordered();
|
||||
let count = entries.len();
|
||||
let donor = prior.and_then(|prior| {
|
||||
let checkpoints = prior.hash_checkpoints.get()?;
|
||||
Some((self.first_changed_binding(prior)?, checkpoints))
|
||||
@@ -1223,43 +1136,18 @@ impl CommandBindings {
|
||||
blocks.push(hasher.checkpoint());
|
||||
0
|
||||
};
|
||||
let first_block = offset / COMMAND_HASH_CHECKPOINT_STRIDE;
|
||||
let block_inputs = (count >= MIN_COMMANDS_TO_RECORD_BLOCKS).then(|| {
|
||||
let donor = self
|
||||
.ordered_names
|
||||
.inputs
|
||||
.get_or_init(|| self.record_block_inputs(names));
|
||||
let changed = self.changed_input_blocks(names, donor);
|
||||
(donor, changed)
|
||||
});
|
||||
let mut macro_inputs = self.fingerprint_inputs.try_lock().ok();
|
||||
if let Some(inputs) = &mut macro_inputs {
|
||||
let mut inputs = self.fingerprint_inputs.try_lock().ok();
|
||||
if let Some(inputs) = &mut inputs {
|
||||
inputs.prune_if_full();
|
||||
}
|
||||
for (block, names) in names
|
||||
.chunks(COMMAND_HASH_CHECKPOINT_STRIDE)
|
||||
.enumerate()
|
||||
.skip(first_block)
|
||||
{
|
||||
let recorded = block_inputs.as_ref().and_then(|(donor, changed)| {
|
||||
if changed[block] {
|
||||
return None;
|
||||
}
|
||||
donor.blocks[block].as_deref()
|
||||
});
|
||||
if let Some(bytes) = recorded {
|
||||
hasher.write(bytes);
|
||||
for (index, (name, meaning)) in entries.enumerate().skip(offset) {
|
||||
name.hash(hasher);
|
||||
if let Some(inputs) = &mut inputs {
|
||||
inputs.hash_meaning(meaning, hasher);
|
||||
} else {
|
||||
for (name, meaning) in self.ordered_entries(names) {
|
||||
name.hash(hasher);
|
||||
if let Some(inputs) = &mut macro_inputs {
|
||||
inputs.hash_meaning(meaning, hasher);
|
||||
} else {
|
||||
meaning.hash(hasher);
|
||||
}
|
||||
}
|
||||
meaning.hash(hasher);
|
||||
}
|
||||
if names.len() == COMMAND_HASH_CHECKPOINT_STRIDE {
|
||||
if (index + 1) % COMMAND_HASH_CHECKPOINT_STRIDE == 0 {
|
||||
blocks.push(hasher.checkpoint());
|
||||
}
|
||||
}
|
||||
@@ -1470,50 +1358,6 @@ fn state_fingerprint_resumes_before_the_first_changed_binding() {
|
||||
assert_eq!(revised.fingerprint_with_prior(Some(&original)), direct(&revised));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_fingerprint_replays_unchanged_blocks_after_earlier_changes() {
|
||||
let mut original = State {
|
||||
commands: Arc::default(),
|
||||
..State::default()
|
||||
};
|
||||
let stride = COMMAND_HASH_CHECKPOINT_STRIDE;
|
||||
let size = MIN_COMMANDS_TO_RECORD_BLOCKS + 3;
|
||||
let name = |index| CommandName::Control(format!("binding-{index:04}"));
|
||||
for index in 0..size {
|
||||
original.define(name(index), Meaning::Count(0), true);
|
||||
}
|
||||
let original_hash = original.fingerprint();
|
||||
let mut revised = original.clone();
|
||||
for index in [stride + 1, size - 1] {
|
||||
revised.define(name(index), Meaning::Count(1), true);
|
||||
}
|
||||
let names = revised.commands.ordered_names();
|
||||
let donor = revised.commands.ordered_names.inputs.get().unwrap();
|
||||
assert!(donor.blocks.iter().all(Option::is_some));
|
||||
let changed = revised.commands.changed_input_blocks(names, donor);
|
||||
let changed: Vec<_> = changed
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, changed)| changed.then_some(index))
|
||||
.collect();
|
||||
assert_eq!(changed, [1, (size - 1) / stride]);
|
||||
let check = |state: &State| {
|
||||
let mut direct = DefaultHasher::new();
|
||||
state.hash(&mut direct);
|
||||
assert_eq!(
|
||||
state.fingerprint_with_prior(Some(&original)),
|
||||
direct.finish()
|
||||
);
|
||||
};
|
||||
check(&revised);
|
||||
assert_ne!(revised.fingerprint(), original_hash);
|
||||
|
||||
revised.command_bindings_mut().remove(&name(stride + 2));
|
||||
assert!(revised.commands.ordered_names.inputs.get().is_none());
|
||||
check(&revised);
|
||||
assert_eq!(original.fingerprint(), original_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_macro_fingerprint_preserves_aliases_mutations_and_uncached_hashes() {
|
||||
use super::Span;
|
||||
|
||||
@@ -169,50 +169,34 @@ impl MacroFingerprintInputs {
|
||||
// Record the complete Meaning hash, including its enum tag. Replay
|
||||
// only to FingerprintHasher, whose integer, length, and string writes
|
||||
// consume the same ordered bytes as the recorder's default methods.
|
||||
let mut record = HashInput::new(MAX_RECORDED_BYTES);
|
||||
let mut record = HashInput::default();
|
||||
meaning.hash(&mut record);
|
||||
let mut bytes = None;
|
||||
if let Some(record) = record.into_bytes() {
|
||||
hasher.write(&record);
|
||||
if record.len() > MAX_CACHED_BYTES - self.bytes {
|
||||
if record.overflow {
|
||||
meaning.hash(hasher);
|
||||
} else {
|
||||
hasher.write(&record.bytes);
|
||||
if record.bytes.len() <= MAX_CACHED_BYTES - self.bytes {
|
||||
self.bytes += record.bytes.len();
|
||||
bytes = Some(record.bytes.into_boxed_slice());
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
self.bytes += record.len();
|
||||
bytes = Some(record);
|
||||
} else {
|
||||
meaning.hash(hasher);
|
||||
}
|
||||
self.inputs.insert(key, MacroInput { body: Arc::downgrade(body), bytes });
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct HashInput {
|
||||
#[derive(Default)]
|
||||
struct HashInput {
|
||||
bytes: Vec<u8>,
|
||||
limit: usize,
|
||||
overflow: bool,
|
||||
}
|
||||
|
||||
impl HashInput {
|
||||
pub(super) fn new(limit: usize) -> Self {
|
||||
Self {
|
||||
bytes: Vec::new(),
|
||||
limit,
|
||||
overflow: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn into_bytes(self) -> Option<Box<[u8]>> {
|
||||
if self.overflow {
|
||||
return None;
|
||||
}
|
||||
Some(self.bytes.into_boxed_slice())
|
||||
}
|
||||
}
|
||||
|
||||
impl Hasher for HashInput {
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
if !self.overflow {
|
||||
if bytes.len() > self.limit - self.bytes.len() {
|
||||
if bytes.len() > MAX_RECORDED_BYTES - self.bytes.len() {
|
||||
self.overflow = true;
|
||||
} else {
|
||||
self.bytes.extend_from_slice(bytes);
|
||||
@@ -221,6 +205,6 @@ impl Hasher for HashInput {
|
||||
}
|
||||
|
||||
fn finish(&self) -> u64 {
|
||||
unreachable!("recording hash input never finishes a digest")
|
||||
unreachable!("recording a macro writes hash input without finishing a digest")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user