From 68eb2c320fceed951db737467116cdc7e7e8a9f2 Mon Sep 17 00:00:00 2001 From: Vittorio Distefano Date: Sat, 28 Nov 2026 09:41:45 +0100 Subject: [PATCH] perf(scoring): keep emptied link buckets in their slot instead of a pool The bucket pool from the previous change held every retired bucket in an extra vector, so the scoring path kept more memory than 0.31.3 at its peak: group_ruin +544 bytes and route_rows +1,568 bytes in the zero-regression gate. An emptied bucket now stays in its HandleMap slot, and the next handle of that slot takes it over through HandleMap::get_or_reuse. No side vector remains, and steady-state notification still allocates nothing. Against 0.31.3, group_ruin runs 8.8% fewer instructions with 8.0% fewer simulated D1 misses and a 992-byte lower peak; route_rows runs 9.4% fewer instructions with 6.5% fewer D1 misses. route_rows still peaks 32 bytes higher: one bucket stays in its slot across a move, where 0.31.3 frees it and allocates it again. --- .../src/stream/relational/handle_map.rs | 21 +++++++++ .../stream/relational/operator/complement.rs | 4 +- .../src/stream/relational/operator/join.rs | 4 +- .../src/stream/relational/operator/links.rs | 44 +++++++++---------- .../src/stream/relational/operator/project.rs | 2 +- .../stream/relational/operator/self_join.rs | 2 +- 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/crates/solverforge-scoring/src/stream/relational/handle_map.rs b/crates/solverforge-scoring/src/stream/relational/handle_map.rs index 221768acf..d8d307194 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 11ebcd7c5..df1e9f146 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 54e512cd7..0b72b289b 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 e98714f7d..7323554ba 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 2f04d31a3..5ef10a5a0 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 b9211bf6b..5c36581da 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