Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/solverforge-scoring/src/stream/relational/handle_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ impl<T> HandleMap<T> {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
44 changes: 22 additions & 22 deletions crates/solverforge-scoring/src/stream/relational/operator/links.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
buckets: HandleMap<Vec<T>>,
retired: Vec<Vec<T>>,
}

impl<T> Links<T> {
pub(super) fn new() -> Self {
Self {
buckets: HandleMap::new(),
retired: Vec::new(),
}
}

Expand All @@ -30,31 +30,34 @@ impl<T> Links<T> {
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<T> {
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<Vec<T>> {
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<T>) {
pub(super) fn restore(&mut self, input: RowHandle, mut bucket: Vec<T>) {
bucket.clear();
self.retired.push(bucket);
if let Some(slot) = self.buckets.get_mut(input) {
*slot = bucket;
}
}
}

impl Links<RowHandle> {
/// 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,
Expand All @@ -63,14 +66,11 @@ impl Links<RowHandle> {
position: usize,
) -> Option<RowHandle> {
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<O, F, T: 'static> ProjectNode<O, F, T> {
self.rows.retract(h);
removed.push(h);
}
self.outputs.retire(outputs);
self.outputs.restore(input, outputs);
}
}
for input in input_inserted.iter().copied() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading