diff --git a/teavm-core/src/main/java/org/teavm/common/GraphIndexer.java b/teavm-core/src/main/java/org/teavm/common/GraphIndexer.java index e65a27dfc..9a968ffee 100644 --- a/teavm-core/src/main/java/org/teavm/common/GraphIndexer.java +++ b/teavm-core/src/main/java/org/teavm/common/GraphIndexer.java @@ -51,11 +51,10 @@ public class GraphIndexer { byte[] state = new byte[sz]; int lastIndex = 0; int lastVisitIndex = 0; - int[] stack = new int[sz * 2]; - int stackSize = 0; - stack[stackSize++] = loopGraph.loopAt(0) != null ? loopGraph.loopAt(0).getHead() : 0; - while (stackSize > 0) { - int node = stack[--stackSize]; + IntegerStack stack = new IntegerStack(sz * 2); + stack.push(loopGraph.loopAt(0) != null ? loopGraph.loopAt(0).getHead() : 0); + while (!stack.isEmpty()) { + int node = stack.pop(); switch (state[node]) { case VISITING: { state[node] = VISITED; @@ -65,7 +64,7 @@ public class GraphIndexer { case NONE: { visitIndex[node] = lastVisitIndex++; state[node] = VISITING; - stack[stackSize++] = node; + stack.push(node); int[] successors = graph.outgoingEdges(node); LoopEntrance[] edges = new LoopEntrance[successors.length]; for (int i = 0; i < edges.length; ++i) { @@ -87,7 +86,7 @@ public class GraphIndexer { int next = edge.follower; switch (state[next]) { case NONE: - stack[stackSize++] = next; + stack.push(next); break; default: break; diff --git a/teavm-core/src/main/java/org/teavm/common/LoopGraph.java b/teavm-core/src/main/java/org/teavm/common/LoopGraph.java index 3087f8973..6ba7c2f9e 100644 --- a/teavm-core/src/main/java/org/teavm/common/LoopGraph.java +++ b/teavm-core/src/main/java/org/teavm/common/LoopGraph.java @@ -15,10 +15,7 @@ */ package org.teavm.common; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; /** * @@ -81,19 +78,18 @@ public class LoopGraph implements Graph { walkIndexes = new int[sz]; LoopImpl[] createdLoops = new LoopImpl[sz]; LoopFrame[] frames = new LoopFrame[sz]; - LoopFrame[] stack = new LoopFrame[sz * 4]; - int stackSize = 0; + Deque stack = new ArrayDeque<>(sz * 4); LoopFrame rootFrame = new LoopFrame(); - stack[stackSize++] = rootFrame; + stack.push(rootFrame); int walkIndex = 0; int lastSortIndex = sz - 1; int loopSetSize = 0; - while (stackSize > 0) { - LoopFrame frame = stack[--stackSize]; + while (!stack.isEmpty()) { + LoopFrame frame = stack.pop(); if (frames[frame.index] == null) { frames[frame.index] = frame; frame.walkIndex = walkIndex++; - stack[stackSize++] = frame; + stack.push(frame); int[] targetEdges = graph.outgoingEdges(frame.index); for (int i = 0; i < targetEdges.length; ++i) { int next = targetEdges[i]; @@ -101,7 +97,7 @@ public class LoopGraph implements Graph { if (nextFrame == null) { nextFrame = new LoopFrame(); nextFrame.index = next; - stack[stackSize++] = nextFrame; + stack.push(nextFrame); } } } else {