diff --git a/crates/solverforge-scoring/src/stream/relational/handle_map.rs b/crates/solverforge-scoring/src/stream/relational/handle_map.rs index 221768ac..d8d30719 100644 --- a/crates/solverforge-scoring/src/stream/relational/handle_map.rs +++ b/crates/solverforge-scoring/src/stream/relational/handle_map.rs @@ -42,6 +42,27 @@ impl HandleMap { None } } + /// The entry of `h`. A stale entry an earlier handle left in the slot is + /// prepared for `h` with `reset` instead of being dropped, so its + /// allocation is kept; `make` builds the value of a slot that never held + /// one. + pub(crate) fn get_or_reuse( + &mut self, + h: RowHandle, + make: impl FnOnce() -> T, + reset: impl FnOnce(&mut T), + ) -> &mut T { + let slot = h.slot() as usize; + if slot >= self.slots.len() { + self.slots.resize_with(slot + 1, || None); + } + let entry = self.slots[slot].get_or_insert_with(|| (h.generation(), make())); + if entry.0 != h.generation() { + entry.0 = h.generation(); + reset(&mut entry.1); + } + &mut entry.1 + } pub(crate) fn get_or_insert_with(&mut self, h: RowHandle, make: impl FnOnce() -> T) -> &mut T { if self.get(h).is_none() { self.insert(h, make()); diff --git a/crates/solverforge-scoring/src/stream/relational/operator/complement.rs b/crates/solverforge-scoring/src/stream/relational/operator/complement.rs index 11ebcd7c..df1e9f14 100644 --- a/crates/solverforge-scoring/src/stream/relational/operator/complement.rs +++ b/crates/solverforge-scoring/src/stream/relational/operator/complement.rs @@ -158,7 +158,7 @@ where removed.push(h); } } - self.left_outputs.retire(outputs); + self.left_outputs.restore(handle, outputs); } } // Keys of the result rows this event removes, read back from the @@ -178,7 +178,7 @@ where removed.push(h); } } - self.right_outputs.retire(outputs); + self.right_outputs.restore(handle, outputs); } } for handle in left_inserted.iter().copied() { diff --git a/crates/solverforge-scoring/src/stream/relational/operator/join.rs b/crates/solverforge-scoring/src/stream/relational/operator/join.rs index 54e512cd..0b72b289 100644 --- a/crates/solverforge-scoring/src/stream/relational/operator/join.rs +++ b/crates/solverforge-scoring/src/stream/relational/operator/join.rs @@ -77,7 +77,7 @@ where removed.push(output); } } - self.left_outputs.retire(outputs); + self.left_outputs.restore(handle, outputs); } } for handle in right_removed.iter().copied() { @@ -89,7 +89,7 @@ where removed.push(output); } } - self.right_outputs.retire(outputs); + self.right_outputs.restore(handle, outputs); } } for handle in left_inserted.iter().copied() { diff --git a/crates/solverforge-scoring/src/stream/relational/operator/links.rs b/crates/solverforge-scoring/src/stream/relational/operator/links.rs index e98714f7..7323554b 100644 --- a/crates/solverforge-scoring/src/stream/relational/operator/links.rs +++ b/crates/solverforge-scoring/src/stream/relational/operator/links.rs @@ -1,20 +1,20 @@ use crate::stream::relational::{HandleMap, RowHandle}; -/// Output links of each input row, with a pool of emptied buckets. +/// Output links of each input row, kept in place across notifications. /// /// A row that leaves and re-enters a node on every move would otherwise drop -/// its bucket on retraction and allocate a new one on insertion; the pool -/// keeps those buckets, so steady-state notification allocates nothing. +/// its bucket on retraction and allocate a new one on insertion. Emptied +/// buckets stay in their slot instead, and a later handle of the same slot +/// takes the bucket over, so steady-state notification allocates nothing and +/// holds no memory beyond one bucket per slot. pub(super) struct Links { buckets: HandleMap>, - retired: Vec>, } impl Links { pub(super) fn new() -> Self { Self { buckets: HandleMap::new(), - retired: Vec::new(), } } @@ -30,31 +30,34 @@ impl Links { self.buckets.get_mut(input) } - /// The bucket of `input`, taken from the pool when it has none. + /// The bucket of `input`, taking over the bucket a stale handle of the + /// same slot left behind. #[inline] pub(super) fn bucket(&mut self, input: RowHandle) -> &mut Vec { - let Self { buckets, retired } = self; - buckets.get_or_insert_with(input, || retired.pop().unwrap_or_default()) + self.buckets.get_or_reuse(input, Vec::new, Vec::clear) } - /// Detaches the bucket of `input`; hand it back with [`Links::retire`]. + /// Detaches the links of `input`, leaving its slot empty until + /// [`Links::restore`] hands the bucket back. #[inline] pub(super) fn take(&mut self, input: RowHandle) -> Option> { - self.buckets.remove(input) + self.buckets.get_mut(input).map(std::mem::take) } - /// Returns an emptied bucket to the pool. + /// Puts a detached bucket back into the slot of `input`, emptied. #[inline] - pub(super) fn retire(&mut self, mut bucket: Vec) { + pub(super) fn restore(&mut self, input: RowHandle, mut bucket: Vec) { bucket.clear(); - self.retired.push(bucket); + if let Some(slot) = self.buckets.get_mut(input) { + *slot = bucket; + } } } impl Links { /// Removes `output` from the bucket of `input` at `position` by swap - /// removal and returns the output moved into that position. An emptied - /// bucket goes back to the pool. + /// removal and returns the output moved into that position. A detached + /// bucket has nothing to remove. #[inline] pub(super) fn remove_link( &mut self, @@ -63,14 +66,11 @@ impl Links { position: usize, ) -> Option { let bucket = self.buckets.get_mut(input)?; + if position >= bucket.len() { + return None; + } debug_assert_eq!(bucket[position], output); bucket.swap_remove(position); - let moved = bucket.get(position).copied(); - if bucket.is_empty() { - if let Some(bucket) = self.buckets.remove(input) { - self.retired.push(bucket); - } - } - moved + bucket.get(position).copied() } } diff --git a/crates/solverforge-scoring/src/stream/relational/operator/project.rs b/crates/solverforge-scoring/src/stream/relational/operator/project.rs index 2f04d31a..5ef10a5a 100644 --- a/crates/solverforge-scoring/src/stream/relational/operator/project.rs +++ b/crates/solverforge-scoring/src/stream/relational/operator/project.rs @@ -84,7 +84,7 @@ impl ProjectNode { self.rows.retract(h); removed.push(h); } - self.outputs.retire(outputs); + self.outputs.restore(input, outputs); } } for input in input_inserted.iter().copied() { diff --git a/crates/solverforge-scoring/src/stream/relational/operator/self_join.rs b/crates/solverforge-scoring/src/stream/relational/operator/self_join.rs index b9211bf6..5c36581d 100644 --- a/crates/solverforge-scoring/src/stream/relational/operator/self_join.rs +++ b/crates/solverforge-scoring/src/stream/relational/operator/self_join.rs @@ -275,7 +275,7 @@ where removed.push(output); } } - self.of_input.retire(tuples); + self.of_input.restore(handle, tuples); if let Some(index) = self.handle_index.remove(handle) { if let Some(key) = self .by_key