Skip to content

Commit

Permalink
Use stitcher config only for functions that wrap a stitcher, not for …
Browse files Browse the repository at this point in the history
…stitcher itself
  • Loading branch information
hendrikvanantwerpen committed Nov 13, 2023
1 parent c4c3b20 commit 064155e
Show file tree
Hide file tree
Showing 27 changed files with 109 additions and 132 deletions.
4 changes: 0 additions & 4 deletions languages/tree-sitter-stack-graphs-java/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use tree_sitter_stack_graphs::loader::FileAnalyzers;
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
use tree_sitter_stack_graphs::loader::LoadError;
use tree_sitter_stack_graphs::loader::StitcherConfig;
use tree_sitter_stack_graphs::CancellationFlag;

/// The stacks graphs tsg path for this language.
Expand Down Expand Up @@ -41,9 +40,6 @@ pub fn try_language_configuration(
)),
Some(STACK_GRAPHS_BUILTINS_CONFIG),
FileAnalyzers::new(),
StitcherConfig {
detect_similar_paths: true,
},
cancellation_flag,
)
}
2 changes: 0 additions & 2 deletions languages/tree-sitter-stack-graphs-typescript/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use tree_sitter_stack_graphs::loader::FileAnalyzers;
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
use tree_sitter_stack_graphs::loader::LoadError;
use tree_sitter_stack_graphs::loader::StitcherConfig;
use tree_sitter_stack_graphs::CancellationFlag;

use crate::npm_package::NpmPackageAnalyzer;
Expand Down Expand Up @@ -57,7 +56,6 @@ pub fn try_language_configuration(
FileAnalyzers::new()
.add("tsconfig.json".to_string(), TsConfigAnalyzer {})
.add("package.json".to_string(), NpmPackageAnalyzer {}),
StitcherConfig::default(),
cancellation_flag,
)
}
10 changes: 4 additions & 6 deletions stack-graphs/include/stack-graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ enum sg_result sg_partial_path_arena_find_partial_paths_in_file(const struct sg_
struct sg_partial_path_arena *partials,
sg_file_handle file,
struct sg_partial_path_list *partial_path_list,
struct sg_stitcher_config config,
const struct sg_stitcher_config *stitcher_config,
const size_t *cancellation_flag);

// Finds all complete paths reachable from a set of starting nodes, placing the result into the
Expand All @@ -754,7 +754,7 @@ enum sg_result sg_partial_path_arena_find_all_complete_paths(const struct sg_sta
size_t starting_node_count,
const sg_node_handle *starting_nodes,
struct sg_partial_path_list *path_list,
struct sg_stitcher_config config,
const struct sg_stitcher_config *stitcher_config,
const size_t *cancellation_flag);

// Returns a reference to the array of partial path data in this partial path database. The
Expand Down Expand Up @@ -812,16 +812,14 @@ struct sg_node_handle_set sg_partial_path_database_local_nodes(const struct sg_p
struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_nodes(const struct sg_stack_graph *graph,
struct sg_partial_path_arena *partials,
size_t count,
const sg_node_handle *starting_nodes,
struct sg_stitcher_config config);
const sg_node_handle *starting_nodes);

// Creates a new forward partial path stitcher that is "seeded" with a set of initial partial
// paths.
struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_partial_paths(const struct sg_stack_graph *graph,
struct sg_partial_path_arena *partials,
size_t count,
const struct sg_partial_path *initial_partial_paths,
struct sg_stitcher_config config);
const struct sg_partial_path *initial_partial_paths);

// Sets whether similar path detection should be enabled during path stitching. Paths are similar
// if start and end node, and pre- and postconditions are the same. The presence of similar paths
Expand Down
4 changes: 2 additions & 2 deletions stack-graphs/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Assertion {
graph: &StackGraph,
partials: &mut PartialPaths,
db: &mut Database,
stitcher_config: &StitcherConfig,
stitcher_config: StitcherConfig,
cancellation_flag: &dyn CancellationFlag,
) -> Result<(), AssertionError> {
match self {
Expand All @@ -175,7 +175,7 @@ impl Assertion {
db: &mut Database,
source: &AssertionSource,
expected_targets: &Vec<AssertionTarget>,
stitcher_config: &StitcherConfig,
stitcher_config: StitcherConfig,
cancellation_flag: &dyn CancellationFlag,
) -> Result<(), AssertionError> {
let references = source.iter_references(graph).collect::<Vec<_>>();
Expand Down
21 changes: 8 additions & 13 deletions stack-graphs/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,20 +1174,21 @@ pub extern "C" fn sg_partial_path_arena_find_partial_paths_in_file(
partials: *mut sg_partial_path_arena,
file: sg_file_handle,
partial_path_list: *mut sg_partial_path_list,
config: sg_stitcher_config,
stitcher_config: *const sg_stitcher_config,
cancellation_flag: *const usize,
) -> sg_result {
let graph = unsafe { &(*graph).inner };
let partials = unsafe { &mut (*partials).inner };
let file = file.into();
let partial_path_list = unsafe { &mut *partial_path_list };
let stitcher_config = unsafe { *stitcher_config };
let cancellation_flag: Option<&AtomicUsize> =
unsafe { std::mem::transmute(cancellation_flag.as_ref()) };
ForwardPartialPathStitcher::find_minimal_partial_path_set_in_file(
graph,
partials,
file,
&config.into(),
stitcher_config.into(),
&AtomicUsizeCancellationFlag(cancellation_flag),
|_graph, partials, path| {
let mut path = path.clone();
Expand All @@ -1213,19 +1214,20 @@ pub extern "C" fn sg_partial_path_arena_find_all_complete_paths(
starting_node_count: usize,
starting_nodes: *const sg_node_handle,
path_list: *mut sg_partial_path_list,
config: sg_stitcher_config,
stitcher_config: *const sg_stitcher_config,
cancellation_flag: *const usize,
) -> sg_result {
let graph = unsafe { &(*graph).inner };
let partials = unsafe { &mut (*partials).inner };
let starting_nodes = unsafe { std::slice::from_raw_parts(starting_nodes, starting_node_count) };
let stitcher_config = unsafe { *stitcher_config };
let path_list = unsafe { &mut *path_list };
let cancellation_flag: Option<&AtomicUsize> =
unsafe { std::mem::transmute(cancellation_flag.as_ref()) };
ForwardPartialPathStitcher::find_all_complete_partial_paths(
&mut GraphEdgeCandidates::new(graph, partials, None),
starting_nodes.iter().copied().map(sg_node_handle::into),
&config.into(),
stitcher_config.into(),
&AtomicUsizeCancellationFlag(cancellation_flag),
|graph, _partials, path| {
if path.is_complete(graph) {
Expand Down Expand Up @@ -1398,6 +1400,7 @@ pub struct sg_forward_partial_path_stitcher {

// Configuration for partial path stitchers.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct sg_stitcher_config {
/// Enables similar path detection during stiching.
pub detect_similar_paths: bool,
Expand Down Expand Up @@ -1461,7 +1464,6 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_nodes(
partials: *mut sg_partial_path_arena,
count: usize,
starting_nodes: *const sg_node_handle,
config: sg_stitcher_config,
) -> *mut sg_forward_partial_path_stitcher {
let graph = unsafe { &(*graph).inner };
let partials = unsafe { &mut (*partials).inner };
Expand All @@ -1476,12 +1478,7 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_nodes(
p
})
.collect::<Vec<_>>();
let stitcher = ForwardPartialPathStitcher::from_partial_paths(
graph,
partials,
initial_paths,
&config.into(),
);
let stitcher = ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
Box::into_raw(Box::new(InternalForwardPartialPathStitcher::new(
stitcher, partials,
))) as *mut _
Expand All @@ -1495,7 +1492,6 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_partial_paths(
partials: *mut sg_partial_path_arena,
count: usize,
initial_partial_paths: *const sg_partial_path,
config: sg_stitcher_config,
) -> *mut sg_forward_partial_path_stitcher {
let graph = unsafe { &(*graph).inner };
let partials = unsafe { &mut (*partials).inner };
Expand All @@ -1505,7 +1501,6 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_partial_paths(
graph,
partials,
initial_partial_paths.to_vec(),
&config.into(),
);
Box::into_raw(Box::new(InternalForwardPartialPathStitcher::new(
stitcher, partials,
Expand Down
111 changes: 62 additions & 49 deletions stack-graphs/src/stitching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,6 @@ impl<'a> Display for DisplaySymbolStackKey<'a> {
//-------------------------------------------------------------------------------------------------
// Stitching partial paths together

/// Configuration for partial path stitchers.
#[derive(Clone, Debug, Default)]
pub struct StitcherConfig {
/// Enables similar path detection during path stitching.
pub detect_similar_paths: bool,
}

/// Implements a phased forward partial path stitching algorithm.
///
/// Our overall goal is to start with a set of _seed_ partial paths, and to repeatedly extend each
Expand Down Expand Up @@ -796,7 +789,6 @@ impl<H> ForwardPartialPathStitcher<H> {
_graph: &StackGraph,
_partials: &mut PartialPaths,
initial_partial_paths: I,
config: &StitcherConfig,
) -> Self
where
I: IntoIterator<Item = PartialPath>,
Expand All @@ -809,16 +801,14 @@ impl<H> ForwardPartialPathStitcher<H> {
(p, c, false)
})
.multiunzip();
let similar_path_detector =
Some(SimilarPathDetector::new()).filter(|_| config.detect_similar_paths);
ForwardPartialPathStitcher {
candidates: Vec::new(),
extensions: Vec::new(),
queue: VecDeque::new(),
initial_paths: next_iteration.0.len(),
next_iteration,
appended_paths,
similar_path_detector,
similar_path_detector: Some(SimilarPathDetector::new()),
// By default, all nodes are checked for cycles and (if enabled) similarity
check_only_join_nodes: false,
// By default, there's no artificial bound on the amount of work done per phase
Expand All @@ -827,28 +817,6 @@ impl<H> ForwardPartialPathStitcher<H> {
phase_number: 1,
}
}
}

impl<H: Clone> ForwardPartialPathStitcher<H> {
/// Returns an iterator of all of the (possibly incomplete) partial paths that were encountered
/// during the most recent phase of the algorithm.
pub fn previous_phase_partial_paths(&self) -> impl Iterator<Item = &PartialPath> + '_ {
self.next_iteration.0.iter()
}

/// Returns a slice of all of the (possibly incomplete) partial paths that were encountered
/// during the most recent phase of the algorithm.
pub fn previous_phase_partial_paths_slice(&mut self) -> &[PartialPath] {
self.next_iteration.0.make_contiguous();
self.next_iteration.0.as_slices().0
}

/// Returns a mutable slice of all of the (possibly incomplete) partial paths that were
/// encountered during the most recent phase of the algorithm.
pub fn previous_phase_partial_paths_slice_mut(&mut self) -> &mut [PartialPath] {
self.next_iteration.0.make_contiguous();
self.next_iteration.0.as_mut_slices().0
}

/// Sets whether similar path detection should be enabled during path stitching. Paths are similar
/// if start and end node, and pre- and postconditions are the same. The presence of similar paths
Expand Down Expand Up @@ -878,6 +846,28 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
pub fn set_max_work_per_phase(&mut self, max_work_per_phase: usize) {
self.max_work_per_phase = max_work_per_phase;
}
}

impl<H: Clone> ForwardPartialPathStitcher<H> {
/// Returns an iterator of all of the (possibly incomplete) partial paths that were encountered
/// during the most recent phase of the algorithm.
pub fn previous_phase_partial_paths(&self) -> impl Iterator<Item = &PartialPath> + '_ {
self.next_iteration.0.iter()
}

/// Returns a slice of all of the (possibly incomplete) partial paths that were encountered
/// during the most recent phase of the algorithm.
pub fn previous_phase_partial_paths_slice(&mut self) -> &[PartialPath] {
self.next_iteration.0.make_contiguous();
self.next_iteration.0.as_slices().0
}

/// Returns a mutable slice of all of the (possibly incomplete) partial paths that were
/// encountered during the most recent phase of the algorithm.
pub fn previous_phase_partial_paths_slice_mut(&mut self) -> &mut [PartialPath] {
self.next_iteration.0.make_contiguous();
self.next_iteration.0.as_mut_slices().0
}

/// Attempts to extend one partial path as part of the algorithm. When calling this function,
/// you are responsible for ensuring that `db` already contains all of the possible appendables
Expand Down Expand Up @@ -1092,7 +1082,7 @@ impl ForwardPartialPathStitcher<Edge> {
graph: &StackGraph,
partials: &mut PartialPaths,
file: Handle<File>,
config: &StitcherConfig,
config: StitcherConfig,
cancellation_flag: &dyn CancellationFlag,
mut visit: F,
) -> Result<(), CancellationError>
Expand All @@ -1111,7 +1101,8 @@ impl ForwardPartialPathStitcher<Edge> {
.map(|node| PartialPath::from_node(graph, partials, node))
.collect::<Vec<_>>();
let mut stitcher =
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths, config);
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
config.apply(&mut stitcher);
stitcher.set_check_only_join_nodes(true);
while !stitcher.is_complete() {
cancellation_flag.check("finding complete partial paths")?;
Expand Down Expand Up @@ -1144,7 +1135,7 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
pub fn find_all_complete_partial_paths<I, F, A, Db, C, Err>(
candidates: &mut C,
starting_nodes: I,
config: &StitcherConfig,
config: StitcherConfig,
cancellation_flag: &dyn CancellationFlag,
mut visit: F,
) -> Result<(), Err>
Expand All @@ -1156,19 +1147,19 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
F: FnMut(&StackGraph, &mut PartialPaths, &PartialPath),
Err: std::convert::From<CancellationError>,
{
let mut stitcher = {
let (graph, partials, _) = candidates.get_graph_partials_and_db();
let initial_paths = starting_nodes
.into_iter()
.filter(|n| graph[*n].is_reference())
.map(|n| {
let mut p = PartialPath::from_node(graph, partials, n);
p.eliminate_precondition_stack_variables(partials);
p
})
.collect::<Vec<_>>();
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths, config)
};
let (graph, partials, _) = candidates.get_graph_partials_and_db();
let initial_paths = starting_nodes
.into_iter()
.filter(|n| graph[*n].is_reference())
.map(|n| {
let mut p = PartialPath::from_node(graph, partials, n);
p.eliminate_precondition_stack_variables(partials);
p
})
.collect::<Vec<_>>();
let mut stitcher =
ForwardPartialPathStitcher::from_partial_paths(graph, partials, initial_paths);
config.apply(&mut stitcher);
stitcher.set_check_only_join_nodes(true);
while !stitcher.is_complete() {
cancellation_flag.check("finding complete partial paths")?;
Expand All @@ -1186,3 +1177,25 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
Ok(())
}
}

/// Configuration for partial path stitchers.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct StitcherConfig {
/// Enables similar path detection during path stitching.
pub detect_similar_paths: bool,
}

impl StitcherConfig {
fn apply<H>(&self, stitcher: &mut ForwardPartialPathStitcher<H>) {
stitcher.set_similar_path_detection(self.detect_similar_paths);
}
}

impl Default for StitcherConfig {
fn default() -> Self {
Self {
detect_similar_paths: true,
}
}
}
4 changes: 2 additions & 2 deletions stack-graphs/tests/it/c/can_find_local_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ fn check_local_nodes(graph: &TestGraph, file: &str, expected_local_nodes: &[&str

let partials = sg_partial_path_arena_new();
let path_list = sg_partial_path_list_new();
let config = sg_stitcher_config {
let stitcher_config = sg_stitcher_config {
detect_similar_paths: false,
};
sg_partial_path_arena_find_partial_paths_in_file(
graph.graph,
partials,
file.as_u32(),
path_list,
config,
&stitcher_config,
std::ptr::null(),
);

Expand Down
Loading

0 comments on commit 064155e

Please sign in to comment.