Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit d6325f5

Browse files
author
Hendrik van Antwerpen
committed
Add stub arenas for holding appened paths adn edges for cycle detection
1 parent edff309 commit d6325f5

File tree

8 files changed

+139
-22
lines changed

8 files changed

+139
-22
lines changed

stack-graphs/include/stack-graphs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ enum sg_result {
5959
SG_RESULT_CANCELLED,
6060
};
6161

62+
// Manages the state of a collection of partial paths to be used in the path-stitching algorithm.
63+
struct sg_appended_paths_arena;
64+
6265
// Manages the state of a collection of partial paths to be used in the path-stitching algorithm.
6366
struct sg_partial_path_arena;
6467

@@ -661,6 +664,12 @@ struct sg_partial_path_arena *sg_partial_path_arena_new(void);
661664
// Frees a path arena, and all of its contents.
662665
void sg_partial_path_arena_free(struct sg_partial_path_arena *partials);
663666

667+
// Creates a new, initially empty partial path arena.
668+
struct sg_appended_paths_arena *sg_appended_paths_arena_new(void);
669+
670+
// Frees a path arena, and all of its contents.
671+
void sg_appended_paths_arena_free(struct sg_appended_paths_arena *partials);
672+
664673
// Creates a new, initially empty partial path database.
665674
struct sg_partial_path_database *sg_partial_path_database_new(void);
666675

@@ -1062,6 +1071,7 @@ void sg_forward_path_stitcher_free(struct sg_forward_path_stitcher *stitcher);
10621071
struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_nodes(const struct sg_stack_graph *graph,
10631072
struct sg_partial_path_arena *partials,
10641073
struct sg_partial_path_database *db,
1074+
struct sg_appended_paths_arena *paths,
10651075
size_t count,
10661076
const sg_node_handle *starting_nodes);
10671077

@@ -1075,6 +1085,7 @@ struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_n
10751085
struct sg_forward_partial_path_stitcher *sg_forward_partial_path_stitcher_from_partial_paths(const struct sg_stack_graph *graph,
10761086
struct sg_partial_path_arena *partials,
10771087
struct sg_partial_path_database *db,
1088+
struct sg_appended_paths_arena *paths,
10781089
size_t count,
10791090
const struct sg_partial_path *initial_partial_paths);
10801091

@@ -1097,6 +1108,7 @@ void sg_forward_partial_path_stitcher_set_max_work_per_phase(struct sg_forward_p
10971108
void sg_forward_partial_path_stitcher_process_next_phase(const struct sg_stack_graph *graph,
10981109
struct sg_partial_path_arena *partials,
10991110
struct sg_partial_path_database *db,
1111+
struct sg_appended_paths_arena *paths,
11001112
struct sg_forward_partial_path_stitcher *stitcher);
11011113

11021114
// Frees a forward path stitcher.

stack-graphs/src/c.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::sync::atomic::AtomicUsize;
1515
use libc::c_char;
1616

1717
use crate::arena::Handle;
18+
use crate::cycles::AppendedPartialPaths;
1819
use crate::graph::File;
1920
use crate::graph::InternedString;
2021
use crate::graph::Node;
@@ -98,6 +99,25 @@ pub extern "C" fn sg_partial_path_arena_free(partials: *mut sg_partial_path_aren
9899
drop(unsafe { Box::from_raw(partials) })
99100
}
100101

102+
/// Manages the state of a collection of partial paths to be used in the path-stitching algorithm.
103+
pub struct sg_appended_paths_arena {
104+
pub inner: AppendedPartialPaths,
105+
}
106+
107+
/// Creates a new, initially empty partial path arena.
108+
#[no_mangle]
109+
pub extern "C" fn sg_appended_paths_arena_new() -> *mut sg_appended_paths_arena {
110+
Box::into_raw(Box::new(sg_appended_paths_arena {
111+
inner: AppendedPartialPaths::new(),
112+
}))
113+
}
114+
115+
/// Frees a path arena, and all of its contents.
116+
#[no_mangle]
117+
pub extern "C" fn sg_appended_paths_arena_free(partials: *mut sg_appended_paths_arena) {
118+
drop(unsafe { Box::from_raw(partials) })
119+
}
120+
101121
/// Contains a "database" of partial paths.
102122
///
103123
/// This type is meant to be a lazily loaded "view" into a proper storage layer. During the
@@ -1962,17 +1982,20 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_nodes(
19621982
graph: *const sg_stack_graph,
19631983
partials: *mut sg_partial_path_arena,
19641984
db: *mut sg_partial_path_database,
1985+
paths: *mut sg_appended_paths_arena,
19651986
count: usize,
19661987
starting_nodes: *const sg_node_handle,
19671988
) -> *mut sg_forward_partial_path_stitcher {
19681989
let graph = unsafe { &(*graph).inner };
19691990
let partials = unsafe { &mut (*partials).inner };
19701991
let db = unsafe { &mut (*db).inner };
1992+
let paths = unsafe { &mut (*paths).inner };
19711993
let starting_nodes = unsafe { std::slice::from_raw_parts(starting_nodes, count) };
19721994
let stitcher = ForwardPartialPathStitcher::from_nodes(
19731995
graph,
19741996
partials,
19751997
db,
1998+
paths,
19761999
starting_nodes.iter().copied().map(sg_node_handle::into),
19772000
);
19782001
Box::into_raw(Box::new(InternalForwardPartialPathStitcher::new(
@@ -1992,18 +2015,21 @@ pub extern "C" fn sg_forward_partial_path_stitcher_from_partial_paths(
19922015
graph: *const sg_stack_graph,
19932016
partials: *mut sg_partial_path_arena,
19942017
db: *mut sg_partial_path_database,
2018+
paths: *mut sg_appended_paths_arena,
19952019
count: usize,
19962020
initial_partial_paths: *const sg_partial_path,
19972021
) -> *mut sg_forward_partial_path_stitcher {
19982022
let graph = unsafe { &(*graph).inner };
19992023
let partials = unsafe { &mut (*partials).inner };
20002024
let db = unsafe { &mut (*db).inner };
2025+
let paths = unsafe { &mut (*paths).inner };
20012026
let initial_partial_paths =
20022027
unsafe { std::slice::from_raw_parts(initial_partial_paths as *const PartialPath, count) };
20032028
let stitcher = ForwardPartialPathStitcher::from_partial_paths(
20042029
graph,
20052030
partials,
20062031
db,
2032+
paths,
20072033
initial_partial_paths.to_vec(),
20082034
);
20092035
Box::into_raw(Box::new(InternalForwardPartialPathStitcher::new(
@@ -2038,13 +2064,17 @@ pub extern "C" fn sg_forward_partial_path_stitcher_process_next_phase(
20382064
graph: *const sg_stack_graph,
20392065
partials: *mut sg_partial_path_arena,
20402066
db: *mut sg_partial_path_database,
2067+
paths: *mut sg_appended_paths_arena,
20412068
stitcher: *mut sg_forward_partial_path_stitcher,
20422069
) {
20432070
let graph = unsafe { &(*graph).inner };
20442071
let partials = unsafe { &mut (*partials).inner };
20452072
let db = unsafe { &mut (*db).inner };
2073+
let paths = unsafe { &mut (*paths).inner };
20462074
let stitcher = unsafe { &mut *(stitcher as *mut InternalForwardPartialPathStitcher) };
2047-
stitcher.stitcher.process_next_phase(graph, partials, db);
2075+
stitcher
2076+
.stitcher
2077+
.process_next_phase(graph, partials, db, paths);
20482078
stitcher.update_previous_phase_partial_paths(partials);
20492079
}
20502080

stack-graphs/src/cycles.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,22 @@ where
135135
}
136136
}
137137

138+
// ----------------------------------------------------------------------------
139+
// Cycle detector when appending edges
140+
138141
#[derive(Clone)]
139142
pub struct EdgeAppendingCycleDetector {
140143
edges: VecDeque<Edge>,
141144
}
142145

146+
pub struct AppendedEdges {}
147+
148+
impl AppendedEdges {
149+
pub fn new() -> Self {
150+
Self {}
151+
}
152+
}
153+
143154
impl EdgeAppendingCycleDetector {
144155
pub fn new() -> Self {
145156
Self {
@@ -151,6 +162,7 @@ impl EdgeAppendingCycleDetector {
151162
&mut self,
152163
graph: &StackGraph,
153164
partials: &mut PartialPaths,
165+
_edges: &mut AppendedEdges,
154166
edge: Edge,
155167
) -> Result<(), ()> {
156168
let end_node = edge.sink;
@@ -188,16 +200,28 @@ impl EdgeAppendingCycleDetector {
188200
}
189201
}
190202

203+
// ----------------------------------------------------------------------------
204+
// Cycle detector when appending partial paths
205+
191206
#[derive(Clone)]
192207
pub struct PartialPathAppendingCycleDetector {
193208
paths: VecDeque<OwnedOrDatabasePath>,
194209
}
195210

211+
pub struct AppendedPartialPaths {}
212+
213+
impl AppendedPartialPaths {
214+
pub fn new() -> Self {
215+
Self {}
216+
}
217+
}
218+
196219
impl PartialPathAppendingCycleDetector {
197220
pub fn from_partial_path(
198221
_graph: &StackGraph,
199222
_partials: &mut PartialPaths,
200223
_db: &mut Database,
224+
_paths: &mut AppendedPartialPaths,
201225
path: OwnedOrDatabasePath,
202226
) -> Self {
203227
Self {
@@ -210,6 +234,7 @@ impl PartialPathAppendingCycleDetector {
210234
graph: &StackGraph,
211235
partials: &mut PartialPaths,
212236
db: &Database,
237+
_paths: &mut AppendedPartialPaths,
213238
path: OwnedOrDatabasePath,
214239
) -> Result<(), ()> {
215240
let end_node = path.get(db).end_node;

stack-graphs/src/partial.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use smallvec::SmallVec;
4545
use crate::arena::Deque;
4646
use crate::arena::DequeArena;
4747
use crate::arena::Handle;
48+
use crate::cycles::AppendedEdges;
4849
use crate::cycles::EdgeAppendingCycleDetector;
4950
use crate::cycles::SimilarPathDetector;
5051
use crate::graph::Edge;
@@ -2382,6 +2383,7 @@ impl PartialPath {
23822383
graph: &StackGraph,
23832384
partials: &mut PartialPaths,
23842385
file: Handle<File>,
2386+
edges: &mut AppendedEdges,
23852387
path_cycle_detector: EdgeAppendingCycleDetector,
23862388
result: &mut R,
23872389
) {
@@ -2406,7 +2408,7 @@ impl PartialPath {
24062408
continue;
24072409
}
24082410
if new_cycle_detector
2409-
.append_edge(graph, partials, extension)
2411+
.append_edge(graph, partials, edges, extension)
24102412
.is_err()
24112413
{
24122414
copious_debugging!(" * cycle");
@@ -2698,6 +2700,7 @@ impl PartialPaths {
26982700
copious_debugging!("Find all partial paths in {}", graph[file]);
26992701
let mut similar_path_detector = SimilarPathDetector::new();
27002702
let mut queue = VecDeque::new();
2703+
let mut edges = AppendedEdges::new();
27012704
queue.extend(
27022705
graph
27032706
.nodes_for_file(file)
@@ -2725,7 +2728,14 @@ impl PartialPaths {
27252728
}
27262729
} else {
27272730
copious_debugging!(" * extend");
2728-
path.extend_from_file(graph, self, file, path_cycle_detector, &mut queue);
2731+
path.extend_from_file(
2732+
graph,
2733+
self,
2734+
file,
2735+
&mut edges,
2736+
path_cycle_detector,
2737+
&mut queue,
2738+
);
27292739
}
27302740
}
27312741
Ok(())

stack-graphs/src/stitching.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use crate::arena::List;
4848
use crate::arena::ListArena;
4949
use crate::arena::ListCell;
5050
use crate::arena::SupplementalArena;
51+
use crate::cycles::AppendedPartialPaths;
5152
use crate::cycles::PartialPathAppendingCycleDetector;
5253
use crate::cycles::SimilarPathDetector;
5354
use crate::graph::Node;
@@ -749,6 +750,7 @@ impl ForwardPartialPathStitcher {
749750
graph: &StackGraph,
750751
partials: &mut PartialPaths,
751752
db: &mut Database,
753+
paths: &mut AppendedPartialPaths,
752754
starting_nodes: I,
753755
) -> ForwardPartialPathStitcher
754756
where
@@ -784,6 +786,7 @@ impl ForwardPartialPathStitcher {
784786
graph,
785787
partials,
786788
db,
789+
paths,
787790
handle.into(),
788791
),
789792
)
@@ -816,6 +819,7 @@ impl ForwardPartialPathStitcher {
816819
graph: &StackGraph,
817820
partials: &mut PartialPaths,
818821
db: &mut Database,
822+
paths: &mut AppendedPartialPaths,
819823
initial_partial_paths: Vec<PartialPath>,
820824
) -> ForwardPartialPathStitcher {
821825
let next_iteration = initial_partial_paths
@@ -825,6 +829,7 @@ impl ForwardPartialPathStitcher {
825829
graph,
826830
partials,
827831
db,
832+
paths,
828833
p.clone().into(),
829834
);
830835
(p, c)
@@ -888,6 +893,7 @@ impl ForwardPartialPathStitcher {
888893
graph: &StackGraph,
889894
partials: &mut PartialPaths,
890895
db: &mut Database,
896+
paths: &mut AppendedPartialPaths,
891897
partial_path: &PartialPath,
892898
cycle_detector: PartialPathAppendingCycleDetector,
893899
) -> usize {
@@ -934,7 +940,7 @@ impl ForwardPartialPathStitcher {
934940
continue;
935941
}
936942
if new_cycle_detector
937-
.append_partial_path(graph, partials, db, extension.into())
943+
.append_partial_path(graph, partials, db, paths, extension.into())
938944
.is_err()
939945
{
940946
copious_debugging!(" is invalid: cyclic");
@@ -968,6 +974,7 @@ impl ForwardPartialPathStitcher {
968974
graph: &StackGraph,
969975
partials: &mut PartialPaths,
970976
db: &mut Database,
977+
paths: &mut AppendedPartialPaths,
971978
) {
972979
copious_debugging!("==> Start phase {}", self.phase_number);
973980
self.queue.extend(
@@ -996,7 +1003,7 @@ impl ForwardPartialPathStitcher {
9961003
continue;
9971004
}
9981005
work_performed +=
999-
self.stitch_partial_path(graph, partials, db, &partial_path, cycle_detector);
1006+
self.stitch_partial_path(graph, partials, db, paths, &partial_path, cycle_detector);
10001007
if work_performed >= self.max_work_per_phase {
10011008
break;
10021009
}
@@ -1031,8 +1038,9 @@ impl ForwardPartialPathStitcher {
10311038
I: IntoIterator<Item = Handle<Node>>,
10321039
F: FnMut(&StackGraph, &mut PartialPaths, &PartialPath),
10331040
{
1041+
let mut paths = AppendedPartialPaths::new();
10341042
let mut stitcher =
1035-
ForwardPartialPathStitcher::from_nodes(graph, partials, db, starting_nodes);
1043+
ForwardPartialPathStitcher::from_nodes(graph, partials, db, &mut paths, starting_nodes);
10361044
stitcher.set_should_extend(|g, _, p| p.starts_at_reference(g));
10371045
while !stitcher.is_complete() {
10381046
cancellation_flag.check("finding complete partial paths")?;
@@ -1042,7 +1050,7 @@ impl ForwardPartialPathStitcher {
10421050
for path in complete_partial_paths {
10431051
visit(graph, partials, path);
10441052
}
1045-
stitcher.process_next_phase(graph, partials, db);
1053+
stitcher.process_next_phase(graph, partials, db, &mut paths);
10461054
}
10471055
Ok(())
10481056
}

0 commit comments

Comments
 (0)