Skip to content
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions crates/stage1-pass2-reuse-candidates/src/dead_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,69 @@ pub(crate) fn dead_candidate_set(

/// Per dead candidate, why, judged against those dead before it: cut off
/// from an input (`Some(input)`), or without a live consumer.
///
/// Elimination goes in rounds, each judging against the candidates dead
/// before it. The first round checks every candidate; a later one checks
/// only the candidates an edge links to one that died in the round before
/// (a candidate with no such edge is judged against the same dead set as
/// before, so it stays alive). The rounds, and so the reasons, are those
/// of rescanning every candidate each round.
fn dead_candidates(
memo: &Memo,
graph: &EdgeGraph<'_>,
) -> BTreeMap<CandidateForMemoGroupId, Option<usize>> {
let outputs = memo.root_groups();
let mut to_check: BTreeSet<CandidateForMemoGroupId> = memo
.groups()
.iter()
.enumerate()
.flat_map(|(group, memo_group)| {
(0..memo_group.candidates.len()).map(move |index_in_group| CandidateForMemoGroupId {
group: MemoGroupId(group as u32),
index_in_group,
})
})
.collect();
let mut dead: BTreeMap<CandidateForMemoGroupId, Option<usize>> = BTreeMap::new();
while !to_check.is_empty() {
let newly_dead: Vec<(CandidateForMemoGroupId, Option<usize>)> = to_check
.iter()
.copied()
.filter_map(|candidate| {
if let Some(input) = graph.cut_off_input(memo, candidate, &dead) {
Some((candidate, Some(input)))
} else if !outputs.contains(&candidate.group)
&& !graph.has_live_consumer(candidate, &dead)
{
Some((candidate, None))
} else {
None
}
})
.collect();
dead.extend(newly_dead.iter().copied());
to_check = newly_dead
.iter()
.flat_map(|&(candidate, _)| graph.neighbours(candidate))
.filter(|neighbour| !dead.contains_key(neighbour))
.collect();
}
#[cfg(test)]
assert_eq!(
dead,
dead_candidates_by_rescanning(memo, graph),
"the rounds of rescanning every candidate"
);
dead
}

/// [`dead_candidates`] rescanning every candidate each round: the
/// reference a faster elimination must agree with. The tests check the
/// two agree on every memo they eliminate over.
#[cfg(test)]
fn dead_candidates_by_rescanning(
memo: &Memo,
graph: &EdgeGraph<'_>,
) -> BTreeMap<CandidateForMemoGroupId, Option<usize>> {
let outputs = memo.root_groups();
let all: Vec<CandidateForMemoGroupId> = memo
Expand Down Expand Up @@ -88,13 +148,16 @@ fn dead_candidates(
struct EdgeGraph<'a> {
by_producer: HashMap<CandidateForMemoGroupId, Vec<&'a CandidateEdge>>,
by_consumer_input: HashMap<(CandidateForMemoGroupId, usize), Vec<&'a CandidateEdge>>,
/// Per consumer, the producers of its edges, on every input.
producers_of: HashMap<CandidateForMemoGroupId, Vec<CandidateForMemoGroupId>>,
}

impl<'a> EdgeGraph<'a> {
fn new(edges: &'a [CandidateEdge]) -> Self {
let mut graph = Self {
by_producer: HashMap::new(),
by_consumer_input: HashMap::new(),
producers_of: HashMap::new(),
};
for edge in edges {
graph
Expand All @@ -107,10 +170,36 @@ impl<'a> EdgeGraph<'a> {
.entry((edge.consumer, edge.input))
.or_default()
.push(edge);
graph
.producers_of
.entry(edge.consumer)
.or_default()
.push(edge.producer);
}
graph
}

/// The candidates an edge links to `candidate`: its consumers and its
/// producers. Only their judgement can change when `candidate` dies.
fn neighbours(
&self,
candidate: CandidateForMemoGroupId,
) -> impl Iterator<Item = CandidateForMemoGroupId> + '_ {
let consumers = self
.by_producer
.get(&candidate)
.into_iter()
.flatten()
.map(|edge| edge.consumer);
let producers = self
.producers_of
.get(&candidate)
.into_iter()
.flatten()
.copied();
consumers.chain(producers)
}

/// Whether `candidate` has a satisfied edge into a consumer not in `dead`.
fn has_live_consumer(
&self,
Expand Down
14 changes: 12 additions & 2 deletions crates/stage1-pass2-reuse-candidates/src/evaluation_grids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ pub(crate) fn evaluation_period_ms_per_query(
) -> Vec<BTreeMap<usize, Option<u64>>> {
let mut periods_ms: Vec<BTreeMap<usize, Option<u64>>> =
vec![BTreeMap::new(); memo.groups().len()];
// A group waits at most once: its periods are read when it is taken,
// so a change while it waits is passed on then. The periods are a
// least fixpoint of gcd merges, the same in any order.
let mut pending = Vec::new();
let mut waiting = vec![false; memo.groups().len()];
fn wait(pending: &mut Vec<MemoGroupId>, waiting: &mut [bool], group: MemoGroupId) {
if !std::mem::replace(&mut waiting[group.0 as usize], true) {
pending.push(group);
}
}
for (query, memo_query) in memo.queries().iter().enumerate() {
let period_ms = query_evaluation_period_ms(entries, query);
for &root in memo_query.root.operator_refs() {
if merge(&mut periods_ms[root.0 as usize], query, period_ms) {
pending.push(root);
wait(&mut pending, &mut waiting, root);
}
}
}
while let Some(group) = pending.pop() {
waiting[group.0 as usize] = false;
let periods_here_ms = periods_ms[group.0 as usize].clone();
for candidate in &memo.groups()[group.0 as usize].candidates {
let operator = &candidate.expression.operator;
Expand All @@ -50,7 +60,7 @@ pub(crate) fn evaluation_period_ms_per_query(
for (&query, &period_ms) in &periods_here_ms {
let below_ms = subquery_resolution_ms.unwrap_or(period_ms);
if merge(&mut periods_ms[child.0 as usize], query, below_ms) {
pending.push(child);
wait(&mut pending, &mut waiting, child);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/stage3-plan-selection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ good_lp = { version = "=1.15.3", default-features = false, features = ["highs"]
highs = "=2.4.0"

[dev-dependencies]
# Reads the MPS files of the block-split check (`independent_blocks`).
highs-sys = "=1.15.0"
asap-types = { path = "../types", features = ["test-support"] }
Loading