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

Commit e4bdc08

Browse files
author
Hendrik van Antwerpen
committed
Do not initialize cyclic path unless there is a cycle
1 parent 2438a01 commit e4bdc08

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

stack-graphs/src/cycles.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl EdgeAppendingCycleDetector {
162162
let end_node = edge.sink;
163163
self.edges.push_front(appended_edges, edge);
164164

165-
let mut cyclic_path = PartialPath::from_node(graph, partials, end_node);
165+
let mut maybe_cyclic_path = None;
166166
let mut index = self.edges;
167167
let mut edges = self.edges;
168168
loop {
@@ -196,18 +196,19 @@ impl EdgeAppendingCycleDetector {
196196
}
197197

198198
// build cyclic path
199+
let cyclic_path = maybe_cyclic_path
200+
.unwrap_or_else(|| PartialPath::from_node(graph, partials, end_node));
199201
prefix_path
200202
.resolve_to(graph, partials, cyclic_path.start_node)
201203
.unwrap();
202204
prefix_path.ensure_no_overlapping_variables(partials, &cyclic_path);
203205
prefix_path
204206
.concatenate(graph, partials, &cyclic_path)
205207
.unwrap();
206-
cyclic_path = prefix_path;
207-
208-
if !cyclic_path.is_productive(graph, partials) {
208+
if !prefix_path.is_productive(graph, partials) {
209209
return Err(());
210210
}
211+
maybe_cyclic_path = Some(prefix_path);
211212
}
212213
}
213214
}
@@ -246,7 +247,7 @@ impl PartialPathAppendingCycleDetector {
246247
let end_node = path.get(db).end_node;
247248
self.paths.push_front(appended_paths, path);
248249

249-
let mut cyclic_path = PartialPath::from_node(graph, partials, end_node);
250+
let mut maybe_cyclic_path = None;
250251
let mut index = self.paths;
251252
let mut paths = self.paths;
252253
loop {
@@ -285,18 +286,19 @@ impl PartialPathAppendingCycleDetector {
285286
}
286287

287288
// build cyclic path
289+
let cyclic_path = maybe_cyclic_path
290+
.unwrap_or_else(|| PartialPath::from_node(graph, partials, end_node));
288291
prefix_path
289292
.resolve_to(graph, partials, cyclic_path.start_node)
290293
.unwrap();
291294
prefix_path.ensure_no_overlapping_variables(partials, &cyclic_path);
292295
prefix_path
293296
.concatenate(graph, partials, &cyclic_path)
294297
.unwrap();
295-
cyclic_path = prefix_path;
296-
297-
if !cyclic_path.is_productive(graph, partials) {
298+
if !prefix_path.is_productive(graph, partials) {
298299
return Err(());
299300
}
301+
maybe_cyclic_path = Some(prefix_path);
300302
}
301303
}
302304
}

0 commit comments

Comments
 (0)