Skip to content

Commit 9f73c70

Browse files
committed
AssignLayers may not converge for some cases so bail out after excessive iterations
1 parent 6bac7d5 commit 9f73c70

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

visualizer/IdealGraphVisualizer/HierarchicalLayout/src/main/java/org/graalvm/visualizer/hierarchicallayout/HierarchicalLayoutManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.TreeSet;
5454
import java.util.concurrent.atomic.AtomicBoolean;
5555
import java.util.function.IntUnaryOperator;
56+
import java.util.logging.Level;
5657
import java.util.logging.Logger;
5758

5859
import static org.graalvm.visualizer.settings.layout.LayoutSettings.BOTH_SORT;
@@ -2360,6 +2361,7 @@ private void assignLayerUpwards() {
23602361
private final SortedSet<LayoutLayer> lay = new TreeSet<>(LAYER_WIDTH_COMPARATOR);
23612362

23622363
private void reassignLayers() {
2364+
int iterations = 0;
23632365
layers = new LayoutLayer[layerCount];
23642366
if (layerCount == 0) {
23652367
return;
@@ -2384,6 +2386,13 @@ private void reassignLayers() {
23842386
if (layer.getMinimalWidth() < avg) {
23852387
break;
23862388
}
2389+
if (++iterations > 20000) {
2390+
// This algorithm is not guaranteed to converge so limit the number of iterations. Thie value
2391+
// was picked based on processing some very large graphs. The max number of iterations seen
2392+
// for a successful layout was around 1500 so 20000 was chosen as a high bound.
2393+
LOG.log(Level.INFO, "Too many iterations in assignLayers so giving up");
2394+
break;
2395+
}
23872396
layer.sort(NODE_WIDTH_COMPARATOR);
23882397
if (!decreaseLayerWidthDeviationUp) {
23892398
for (LayoutNode n : layer) {

0 commit comments

Comments
 (0)