1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2021, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
//! Detect and avoid cycles in our path-finding algorithm.
//!
//! Cycles in a stack graph can indicate many things. Your language might allow mutually recursive
//! imports. If you are modeling dataflow through function calls, then any recursion in your
//! function calls will lead to cycles in your stack graph. And if you have any control-flow paths
//! that lead to infinite loops at runtime, we'll probably discover those as stack graph paths
//! during the path-finding algorithm.
//!
//! (Note that we're only considering cycles in well-formed paths. For instance, _pop symbol_
//! nodes are "guards" that don't allow you to progress into a node if the top of the symbol stack
//! doesn't match. We don't consider that a valid path, and so we don't have to worry about
//! whether it contains any cycles.)
//!
//! This module implements a cycle detector that lets us detect these situations and "cut off"
//! these paths, not trying to extend them any further. Note that any cycle detection logic we
//! implement will be a heuristic. In particular, since our path-finding algorithm will mimic any
//! runtime recursion, a "complete" cycle detection logic would be equivalent to the Halting
//! Problem.
//!
//! Right now, we implement a simple heuristic where we limit the number of distinct paths that we
//! process that have the same start and end nodes. We do not make any guarantees that we will
//! always use this particular heuristic, however! We reserve the right to change the heuristic at
//! any time.
use std::collections::HashMap;
use smallvec::SmallVec;
use crate::arena::Handle;
use crate::graph::Node;
use crate::partial::PartialPath;
use crate::paths::Path;
/// Helps detect cycles in the path-finding algorithm.
pub struct CycleDetector<P> {
paths: HashMap<PathKey, SmallVec<[P; 8]>>,
}
#[doc(hidden)]
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct PathKey {
start_node: Handle<Node>,
end_node: Handle<Node>,
}
#[doc(hidden)]
pub trait HasPathKey: Clone {
fn key(&self) -> PathKey;
fn is_shorter_than(&self, other: &Self) -> bool;
}
impl HasPathKey for Path {
fn key(&self) -> PathKey {
PathKey {
start_node: self.start_node,
end_node: self.end_node,
}
}
fn is_shorter_than(&self, other: &Self) -> bool {
self.edges.len() < other.edges.len() && self.symbol_stack.len() <= other.symbol_stack.len()
}
}
impl HasPathKey for PartialPath {
fn key(&self) -> PathKey {
PathKey {
start_node: self.start_node,
end_node: self.end_node,
}
}
fn is_shorter_than(&self, other: &Self) -> bool {
self.edges.len() < other.edges.len()
&& (self.symbol_stack_precondition.len() + self.symbol_stack_postcondition.len())
<= (other.symbol_stack_precondition.len() + other.symbol_stack_postcondition.len())
}
}
const MAX_SIMILAR_PATH_COUNT: usize = 4;
impl<P> CycleDetector<P>
where
P: HasPathKey,
{
/// Creates a new, empty cycle detector.
pub fn new() -> CycleDetector<P> {
CycleDetector {
paths: HashMap::new(),
}
}
/// Determines whether we should process this path during the path-finding algorithm. If our
/// heuristics decide that this path is a duplicate, or is "non-productive", then we return
/// `false`, and the path-finding algorithm will skip this path.
pub fn should_process_path<F>(&mut self, path: &P, cmp: F) -> bool
where
F: FnMut(&P) -> std::cmp::Ordering,
{
let key = path.key();
let paths_with_same_nodes = self.paths.entry(key).or_default();
match paths_with_same_nodes.binary_search_by(cmp) {
// We've already seen this exact path before; no need to process it again.
Ok(_) => return false,
// Otherwise add it to the list.
Err(index) => paths_with_same_nodes.insert(index, path.clone()),
}
// Count how many paths we've already processed that have the same endpoints and are
// "shorter".
let similar_path_count = paths_with_same_nodes
.iter()
.filter(|similar_path| similar_path.is_shorter_than(path))
.count();
return similar_path_count <= MAX_SIMILAR_PATH_COUNT;
}
}