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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::links::Links;
use super::{ComplementView, GroupedOperator, Operator, Pair, PairDelta, ProjectView};
use crate::stream::joiner::plan::{CompileCondition, ExecutablePlan, IndexedPlan};
use crate::stream::relational::hash::FastMap;
Expand Down Expand Up @@ -48,8 +49,8 @@ pub struct ComplementNode<
rows: DenseRowStore<Output<T>>,
real_outputs: FastMap<JoinedIdentity, RowHandle>,
default_outputs: HandleMap<RowHandle>,
left_outputs: HandleMap<Vec<RowHandle>>,
right_outputs: HandleMap<Vec<RowHandle>>,
left_outputs: Links<RowHandle>,
right_outputs: Links<RowHandle>,
/// Child notification scratch, reused across events (targets/results).
child_l_removed: Vec<RowHandle>,
child_l_inserted: Vec<RowHandle>,
Expand Down Expand Up @@ -82,8 +83,8 @@ where
rows: DenseRowStore::new(),
real_outputs: FastMap::default(),
default_outputs: HandleMap::new(),
left_outputs: HandleMap::new(),
right_outputs: HandleMap::new(),
left_outputs: Links::new(),
right_outputs: Links::new(),
child_l_removed: Vec::new(),
child_l_inserted: Vec::new(),
child_r_removed: Vec::new(),
Expand Down Expand Up @@ -150,11 +151,14 @@ where
} = delta;
for handle in left.iter().copied() {
self.plan.remove_left(&mut self.indexes, handle);
for h in self.left_outputs.remove(handle).unwrap_or_default() {
if self.rows.get(h).is_some() {
self.remove_output(h);
removed.push(h);
if let Some(mut outputs) = self.left_outputs.take(handle) {
for h in outputs.drain(..) {
if self.rows.get(h).is_some() {
self.remove_output(h);
removed.push(h);
}
}
self.left_outputs.retire(outputs);
}
}
// Keys of the result rows this event removes, read back from the
Expand All @@ -167,11 +171,14 @@ where
.collect();
for handle in right.iter().copied() {
self.plan.remove_right(&mut self.indexes, handle);
for h in self.right_outputs.remove(handle).unwrap_or_default() {
if self.rows.get(h).is_some() {
self.remove_output(h);
removed.push(h);
if let Some(mut outputs) = self.right_outputs.take(handle) {
for h in outputs.drain(..) {
if self.rows.get(h).is_some() {
self.remove_output(h);
removed.push(h);
}
}
self.right_outputs.retire(outputs);
}
}
for handle in left_inserted.iter().copied() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::stream::relational::operator::links::remove_link;
use crate::stream::relational::operator::links::Links;

impl<S: 'static, L: Operator<S>, R: Operator<S>, P, F, T> ComplementNode<S, L, R, P, F, T>
where
Expand All @@ -13,15 +13,17 @@ where
OutputValue::Real(id) => {
self.real_outputs.remove(&id);
if let Some(moved) =
remove_link(&mut self.left_outputs, id.left(), h, row.left_position)
self.left_outputs
.remove_link(id.left(), h, row.left_position)
{
self.rows
.get_mut(moved)
.expect("moved complement target")
.left_position = row.left_position;
}
if let Some(moved) =
remove_link(&mut self.right_outputs, id.right(), h, row.right_position)
self.right_outputs
.remove_link(id.right(), h, row.right_position)
{
self.rows
.get_mut(moved)
Expand All @@ -31,8 +33,7 @@ where
}
OutputValue::Default(input, _) => {
self.default_outputs.remove(input);
if let Some(moved) =
remove_link(&mut self.left_outputs, input, h, row.left_position)
if let Some(moved) = self.left_outputs.remove_link(input, h, row.left_position)
{
self.rows
.get_mut(moved)
Expand All @@ -47,8 +48,8 @@ where
pub(super) fn emit_real(
rows: &mut DenseRowStore<Output<T>>,
outputs: &mut FastMap<JoinedIdentity, RowHandle>,
left_outputs: &mut HandleMap<Vec<RowHandle>>,
right_outputs: &mut HandleMap<Vec<RowHandle>>,
left_outputs: &mut Links<RowHandle>,
right_outputs: &mut Links<RowHandle>,
left: RowHandle,
right: RowHandle,
inserted: &mut Vec<RowHandle>,
Expand All @@ -57,8 +58,8 @@ where
if outputs.contains_key(&id) {
return None;
}
let left_bucket = left_outputs.get_or_insert_with(left, Vec::new);
let right_bucket = right_outputs.get_or_insert_with(right, Vec::new);
let left_bucket = left_outputs.bucket(left);
let right_bucket = right_outputs.bucket(right);
let h = rows.insert(Output {
value: OutputValue::Real(id),
left_position: left_bucket.len(),
Expand Down Expand Up @@ -86,7 +87,7 @@ where
.resolve(solution, input)
.expect("live complement target");
let value = (self.default)(solution, &row);
let bucket = self.left_outputs.get_or_insert_with(input, Vec::new);
let bucket = self.left_outputs.bucket(input);
let h = self.rows.insert(Output {
value: OutputValue::Default(input, value),
left_position: bucket.len(),
Expand Down
62 changes: 32 additions & 30 deletions crates/solverforge-scoring/src/stream/relational/operator/join.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::{DenseRowStore, HandleMap, JoinedIdentity, RowHandle};
use super::{links::remove_link, GroupedOperator, Operator, Pair, PairDelta};
use super::super::{DenseRowStore, JoinedIdentity, RowHandle};
use super::{links::Links, GroupedOperator, Operator, Pair, PairDelta};
use super::{ExplainOperator, RowOrigin};
use crate::api::analysis::EntityRef;
use crate::stream::joiner::plan::{CompileCondition, ExecutablePlan, IndexedPlan};
Expand All @@ -21,8 +21,8 @@ pub struct JoinNode<S, L, R, P: IndexedPlan> {
indexes: P::Indexes,
rows: DenseRowStore<JoinedRow>,
outputs: FastMap<JoinedIdentity, RowHandle>,
left_outputs: HandleMap<Vec<RowHandle>>,
right_outputs: HandleMap<Vec<RowHandle>>,
left_outputs: Links<RowHandle>,
right_outputs: Links<RowHandle>,
/// Child notification scratch, reused across events (L/R, removed/inserted).
child_l_removed: Vec<RowHandle>,
child_l_inserted: Vec<RowHandle>,
Expand All @@ -46,8 +46,8 @@ where
indexes,
rows: DenseRowStore::new(),
outputs: FastMap::default(),
left_outputs: HandleMap::new(),
right_outputs: HandleMap::new(),
left_outputs: Links::new(),
right_outputs: Links::new(),
child_l_removed: Vec::new(),
child_l_inserted: Vec::new(),
child_r_removed: Vec::new(),
Expand All @@ -70,20 +70,26 @@ where
} = delta;
for handle in left_removed.iter().copied() {
self.plan.remove_left(&mut self.indexes, handle);
for output in self.left_outputs.remove(handle).unwrap_or_default() {
if self.rows.get(output).is_some() {
self.remove_output(output);
removed.push(output);
if let Some(mut outputs) = self.left_outputs.take(handle) {
for output in outputs.drain(..) {
if self.rows.get(output).is_some() {
self.remove_output(output);
removed.push(output);
}
}
self.left_outputs.retire(outputs);
}
}
for handle in right_removed.iter().copied() {
self.plan.remove_right(&mut self.indexes, handle);
for output in self.right_outputs.remove(handle).unwrap_or_default() {
if self.rows.get(output).is_some() {
self.remove_output(output);
removed.push(output);
if let Some(mut outputs) = self.right_outputs.take(handle) {
for output in outputs.drain(..) {
if self.rows.get(output).is_some() {
self.remove_output(output);
removed.push(output);
}
}
self.right_outputs.retire(outputs);
}
}
for handle in left_inserted.iter().copied() {
Expand All @@ -110,23 +116,19 @@ where
fn remove_output(&mut self, handle: RowHandle) {
if let Some(row) = self.rows.retract(handle) {
self.outputs.remove(&row.identity);
if let Some(moved) = remove_link(
&mut self.left_outputs,
row.identity.left(),
handle,
row.left_position,
) {
if let Some(moved) =
self.left_outputs
.remove_link(row.identity.left(), handle, row.left_position)
{
self.rows
.get_mut(moved)
.expect("moved left output")
.left_position = row.left_position;
}
if let Some(moved) = remove_link(
&mut self.right_outputs,
row.identity.right(),
handle,
row.right_position,
) {
if let Some(moved) =
self.right_outputs
.remove_link(row.identity.right(), handle, row.right_position)
{
self.rows
.get_mut(moved)
.expect("moved right output")
Expand All @@ -143,17 +145,17 @@ where
fn emit_output(
rows: &mut DenseRowStore<JoinedRow>,
outputs: &mut FastMap<JoinedIdentity, RowHandle>,
left_outputs: &mut HandleMap<Vec<RowHandle>>,
right_outputs: &mut HandleMap<Vec<RowHandle>>,
left_outputs: &mut Links<RowHandle>,
right_outputs: &mut Links<RowHandle>,
left: RowHandle,
right: RowHandle,
) -> Option<RowHandle> {
let identity = JoinedIdentity::new(left, right, 0);
if outputs.contains_key(&identity) {
return None;
}
let left_bucket = left_outputs.get_or_insert_with(left, Vec::new);
let right_bucket = right_outputs.get_or_insert_with(right, Vec::new);
let left_bucket = left_outputs.bucket(left);
let right_bucket = right_outputs.bucket(right);
let handle = rows.insert(JoinedRow {
identity,
left_position: left_bucket.len(),
Expand Down
88 changes: 73 additions & 15 deletions crates/solverforge-scoring/src/stream/relational/operator/links.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
use crate::stream::relational::{HandleMap, RowHandle};

#[inline]
pub(super) fn remove_link(
links: &mut HandleMap<Vec<RowHandle>>,
input: RowHandle,
output: RowHandle,
position: usize,
) -> Option<RowHandle> {
let bucket = links.get_mut(input)?;
debug_assert_eq!(bucket[position], output);
bucket.swap_remove(position);
let moved = bucket.get(position).copied();
if bucket.is_empty() {
links.remove(input);
}
moved
/// Output links of each input row, with a pool of emptied buckets.
///
/// 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.
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(),
}
}

pub(super) fn clear(&mut self) {
self.buckets.clear();
}

pub(super) fn get(&self, input: RowHandle) -> Option<&Vec<T>> {
self.buckets.get(input)
}

pub(super) fn get_mut(&mut self, input: RowHandle) -> Option<&mut Vec<T>> {
self.buckets.get_mut(input)
}

/// The bucket of `input`, taken from the pool when it has none.
#[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())
}

/// Detaches the bucket of `input`; hand it back with [`Links::retire`].
#[inline]
pub(super) fn take(&mut self, input: RowHandle) -> Option<Vec<T>> {
self.buckets.remove(input)
}

/// Returns an emptied bucket to the pool.
#[inline]
pub(super) fn retire(&mut self, mut bucket: Vec<T>) {
bucket.clear();
self.retired.push(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.
#[inline]
pub(super) fn remove_link(
&mut self,
input: RowHandle,
output: RowHandle,
position: usize,
) -> Option<RowHandle> {
let bucket = self.buckets.get_mut(input)?;
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
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::links::Links;
use super::Operator;
use super::{ExplainOperator, RowOrigin};
use crate::api::analysis::EntityRef;
use crate::stream::relational::{DenseRowStore, HandleMap, RowHandle};
use crate::stream::relational::{DenseRowStore, RowHandle};

/// Borrowed owned output with its entire upstream row and emission position.
pub struct ProjectView<'a, V, T> {
Expand Down Expand Up @@ -29,7 +30,7 @@ pub struct ProjectNode<O, F, T> {
input: O,
mapper: F,
rows: DenseRowStore<Output<T>>,
outputs: HandleMap<Vec<RowHandle>>,
outputs: Links<RowHandle>,
/// Child notification scratch, reused across events.
child_removed: Vec<RowHandle>,
child_inserted: Vec<RowHandle>,
Expand All @@ -47,13 +48,13 @@ impl<O, F, T> ProjectNode<O, F, T> {
input,
mapper,
rows: DenseRowStore::new(),
outputs: HandleMap::new(),
outputs: Links::new(),
child_removed: Vec::new(),
child_inserted: Vec::new(),
}
}
fn add(&mut self, input: RowHandle, values: Vec<T>, inserted: &mut Vec<RowHandle>) {
let outputs = self.outputs.get_or_insert_with(input, Vec::new);
let outputs = self.outputs.bucket(input);
for (emission, value) in values.into_iter().enumerate() {
let h = self.rows.insert(Output {
input,
Expand All @@ -78,9 +79,12 @@ impl<O, F, T: 'static> ProjectNode<O, F, T> {
F: for<'a> Fn(&S, &O::View<'a>) -> Vec<T> + 'static,
{
for input in input_removed.iter().copied() {
for h in self.outputs.remove(input).unwrap_or_default() {
self.rows.retract(h);
removed.push(h);
if let Some(mut outputs) = self.outputs.take(input) {
for h in outputs.drain(..) {
self.rows.retract(h);
removed.push(h);
}
self.outputs.retire(outputs);
}
}
for input in input_inserted.iter().copied() {
Expand Down
Loading
Loading