aetherlight: fold the views of a bulk commit in rows proportional to the change

The moved-targets arm called Docs::indexed once per changed placement or route,
and indexed walks every touched key while a log is open, so genesis and every
replayed commit paid touched dependencies x changed targets. Docs::indexed_unchanged
names the committed index entries of keys the log did not change, with no log walk;
a changed dependencies key is already re-judged whole, so the fold loses nothing.
folding_a_bulk_commit_reads_rows_in_proportion_to_the_change pins it by rows read.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-20 15:11:28 +01:00
commit aa9e91247d
3 changed files with 81 additions and 13 deletions

View file

@ -289,6 +289,30 @@ impl<K: Ord + Clone, V: Clone + PartialEq, P: Projection<V>> Docs<K, V, P> {
found
}
/// The committed index's (point, key) entries inside `range` for keys the
/// open log did not change: no walk of the log, so a bulk transaction pays
/// for the entries it returns and not for every key it touched. A key the
/// log changed is the caller's to judge from `changes()`.
pub(crate) fn indexed_unchanged<R: std::ops::RangeBounds<P::Point>>(
&self,
range: R,
) -> Vec<(P::Point, K)> {
let found: Vec<(P::Point, K)> = self
.index
.range((range.start_bound(), range.end_bound()))
.flat_map(|(point, keys)| keys.iter().map(move |key| (point, key)))
.filter(|(_, key)| {
self.touched
.get(*key)
.is_none_or(|old| self.map.get(*key) == old.as_ref())
})
.map(|(point, key)| (point.clone(), key.clone()))
.collect();
#[cfg(test)]
count_rows(found.len() as u64);
found
}
/// Whether the index is exactly the projection of the map (test builds).
#[cfg(test)]
pub(crate) fn index_is_derived(&self) -> bool {
@ -597,6 +621,20 @@ mod tests {
}
}
#[test]
fn indexed_unchanged_names_the_committed_entries_of_keys_the_log_did_not_change() {
let mut docs = spread(&[(1, 10), (2, 21), (3, 33), (4, 11)]);
docs.insert(1, 55); // changed
docs.remove(&2); // removed
docs.insert(3, 33); // touched, written back: unchanged
docs.insert(9, 11); // added: not in the committed index
let before = rows_read();
let found = docs.indexed_unchanged(1..=1);
assert_eq!(found, vec![(1, 4)], "only the committed entries of unchanged keys");
assert_eq!(rows_read() - before, 1, "no walk of the log, only the entries returned");
assert_eq!(docs.indexed_unchanged(..), vec![(1, 4), (3, 3)], "key 3 written back counts as unchanged");
}
#[test]
fn walking_the_open_log_is_counted_even_when_nothing_matches() {
let mut docs = spread(&[(1, 10), (2, 21), (3, 33)]);

View file

@ -1566,3 +1566,34 @@ fn releasing_a_dependency_removes_the_pressure_it_raised() {
let reopened = WorldKernel::open(&kernel.export().unwrap()).expect("the world reopens");
assert_eq!(reopened.state, kernel.state);
}
/// A bulk commit (genesis, a large patch, every replayed commit at open) moves
/// many placements and touches many dependencies at once. Folding the views
/// for it reads rows in proportion to what the commit changed, not to changed
/// targets times touched dependencies: every person moves and binds a route in
/// one transaction, and 1600 persons read under six times the rows of 400.
#[test]
fn folding_a_bulk_commit_reads_rows_in_proportion_to_the_change() {
use crate::patch::Position;
use crate::DependencyTarget;
let mut rows = Vec::new();
for persons in [400usize, 1600] {
let (kernel, people, _) = clockwork_world(persons);
let mut state = kernel.state;
let route = *state.edges.keys().next().expect("a route");
let place = state.positions[&people[0]].place;
state.begin();
for person in people.iter().filter(|person| state.positions.contains_key(person)).copied().collect::<Vec<_>>() {
state.positions.insert(person, Position { place });
state.dependencies.entry(person).or_default().insert(DependencyTarget::Route(route));
}
let mut views = std::mem::take(&mut state.views);
let before = crate::docs::rows_read();
views.fold(&state);
rows.push(crate::docs::rows_read() - before);
state.views = views;
state.rollback();
}
assert!(rows[1] > rows[0], "{rows:?}: a bigger commit read no more");
assert!(rows[1] < rows[0] * 6, "the fold read {} rows at 400 persons and {} at 1600", rows[0], rows[1]);
}

View file

@ -57,17 +57,6 @@ pub(crate) fn readable(state: &WorldState) -> bool {
&& state.positions.is_clean()
}
/// The subjects with a dependency on `target`, from the dependencies
/// partition's `DependsOn` index (true mid-transaction).
fn dependents(state: &WorldState, target: DependencyTarget) -> Vec<SubjectId> {
state
.dependencies
.indexed(target..=target)
.into_iter()
.map(|(_, subject)| subject)
.collect()
}
impl Views {
/// Subjects with a live commitment due at or before `to`.
pub(crate) fn due_by(&self, to: FictionalMinutes) -> BTreeSet<SubjectId> {
@ -152,8 +141,11 @@ impl Views {
.map(|(edge, _, _)| DependencyTarget::Route(*edge))
.chain(state.positions.changes().map(|(subject, _, _)| DependencyTarget::Subject(*subject)))
.collect();
// A changed dependencies key is already in `rejudged`, so the dependents
// left to judge are the unchanged ones, which the committed index names
// without walking the log once per target.
for target in moved {
for dependent in dependents(state, target) {
for (_, dependent) in state.dependencies.indexed_unchanged(target..=target) {
if rejudged.contains(&dependent) {
continue;
}
@ -225,7 +217,14 @@ impl Views {
.collect()
}
/// The subjects with a dependency on `target`, from the dependencies
/// partition's `DependsOn` index (true mid-transaction).
pub(crate) fn dependents_of(state: &WorldState, target: DependencyTarget) -> Vec<SubjectId> {
dependents(state, target)
state
.dependencies
.indexed(target..=target)
.into_iter()
.map(|(_, subject)| subject)
.collect()
}
}