changeset 17044:d5289a18cf5d

NodeClassIterator advances lazily instead of eagerly, allowing the next element to be cached in the advance operation
author Doug Simon <doug.simon@oracle.com>
date Thu, 04 Sep 2014 13:08:27 +0200
parents 46cefd15ba3f
children 7c12f8aae0c9
files graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java
diffstat 1 files changed, 47 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java	Thu Sep 04 12:54:06 2014 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeClass.java	Thu Sep 04 13:08:27 2014 +0200
@@ -496,6 +496,9 @@
         protected final Node node;
         protected int index;
         protected int subIndex;
+        NodeList<Node> list;
+        protected boolean needsForward;
+        protected Node nextElement;
 
         /**
          * Creates an iterator that will iterate over fields in the given node.
@@ -506,14 +509,16 @@
             this.node = node;
             index = NOT_ITERABLE;
             subIndex = 0;
+            needsForward = true;
         }
 
         void forward() {
+            needsForward = false;
             if (index < getDirectCount()) {
                 index++;
                 while (index < getDirectCount()) {
-                    Node element = getNode(node, getOffsets()[index]);
-                    if (element != null) {
+                    nextElement = getNode(node, getOffsets()[index]);
+                    if (nextElement != null) {
                         return;
                     }
                     index++;
@@ -522,9 +527,12 @@
                 subIndex++;
             }
             while (index < getOffsets().length) {
-                NodeList<Node> list = getNodeList(node, getOffsets()[index]);
+                if (subIndex == 0) {
+                    list = getNodeList(node, getOffsets()[index]);
+                }
                 while (subIndex < list.size()) {
-                    if (list.get(subIndex) != null) {
+                    nextElement = list.get(subIndex);
+                    if (nextElement != null) {
                         return;
                     }
                     subIndex++;
@@ -535,39 +543,39 @@
         }
 
         private Node nextElement() {
-            if (index < getDirectCount()) {
-                return getNode(node, getOffsets()[index]);
-            } else if (index < getOffsets().length) {
-                NodeList<Node> list = getNodeList(node, getOffsets()[index]);
-                return list.get(subIndex);
+            if (needsForward) {
+                forward();
+            }
+            needsForward = true;
+            if (index < getOffsets().length) {
+                return nextElement;
             }
             throw new NoSuchElementException();
         }
 
         @Override
         public boolean hasNext() {
+            if (needsForward) {
+                forward();
+            }
             return index < getOffsets().length;
         }
 
         @Override
         public Node next() {
-            try {
-                return nextElement();
-            } finally {
-                forward();
-            }
+            return nextElement();
         }
 
         public Position nextPosition() {
-            try {
-                if (index < getDirectCount()) {
-                    return new Position(getOffsets() == getNodeClass().inputOffsets, index, NOT_ITERABLE);
-                } else {
-                    return new Position(getOffsets() == getNodeClass().inputOffsets, index, subIndex);
-                }
-            } finally {
+            if (needsForward) {
                 forward();
             }
+            needsForward = true;
+            if (index < getDirectCount()) {
+                return new Position(getOffsets() == getNodeClass().inputOffsets, index, NOT_ITERABLE);
+            } else {
+                return new Position(getOffsets() == getNodeClass().inputOffsets, index, subIndex);
+            }
         }
 
         @Override
@@ -583,16 +591,10 @@
     }
 
     private class NodeClassInputsIterator extends NodeClassIterator {
+
         NodeClassInputsIterator(Node node) {
-            this(node, true);
-        }
-
-        NodeClassInputsIterator(Node node, boolean forward) {
             super(node);
             assert NodeClass.this == node.getNodeClass();
-            if (forward) {
-                forward();
-            }
         }
 
         @Override
@@ -613,22 +615,27 @@
 
     private class NodeClassAllInputsIterator extends NodeClassInputsIterator {
         NodeClassAllInputsIterator(Node node) {
-            super(node, true);
+            super(node);
         }
 
         @Override
         void forward() {
+            needsForward = false;
             if (index < getDirectCount()) {
                 index++;
                 if (index < getDirectCount()) {
+                    nextElement = getNode(node, getOffsets()[index]);
                     return;
                 }
             } else {
                 subIndex++;
             }
             while (index < getOffsets().length) {
-                NodeList<Node> list = getNodeList(node, getOffsets()[index]);
+                if (subIndex == 0) {
+                    list = getNodeList(node, getOffsets()[index]);
+                }
                 if (subIndex < list.size()) {
+                    nextElement = list.get(subIndex);
                     return;
                 }
                 subIndex = 0;
@@ -639,22 +646,27 @@
 
     private class NodeClassAllSuccessorsIterator extends NodeClassSuccessorsIterator {
         NodeClassAllSuccessorsIterator(Node node) {
-            super(node, true);
+            super(node);
         }
 
         @Override
         void forward() {
+            needsForward = false;
             if (index < getDirectCount()) {
                 index++;
                 if (index < getDirectCount()) {
+                    nextElement = getNode(node, getOffsets()[index]);
                     return;
                 }
             } else {
                 subIndex++;
             }
             while (index < getOffsets().length) {
-                NodeList<Node> list = getNodeList(node, getOffsets()[index]);
+                if (subIndex == 0) {
+                    list = getNodeList(node, getOffsets()[index]);
+                }
                 if (subIndex < list.size()) {
+                    nextElement = list.get(subIndex);
                     return;
                 }
                 subIndex = 0;
@@ -667,10 +679,9 @@
         private final int modCount;
 
         private NodeClassInputsWithModCountIterator(Node node) {
-            super(node, false);
+            super(node);
             assert MODIFICATION_COUNTS_ENABLED;
             this.modCount = node.modCount();
-            forward();
         }
 
         @Override
@@ -702,16 +713,10 @@
     }
 
     private class NodeClassSuccessorsIterator extends NodeClassIterator {
+
         NodeClassSuccessorsIterator(Node node) {
-            this(node, true);
-        }
-
-        NodeClassSuccessorsIterator(Node node, boolean forward) {
             super(node);
             assert NodeClass.this == node.getNodeClass();
-            if (forward) {
-                forward();
-            }
         }
 
         @Override
@@ -734,10 +739,9 @@
         private final int modCount;
 
         private NodeClassSuccessorsWithModCountIterator(Node node) {
-            super(node, false);
+            super(node);
             assert MODIFICATION_COUNTS_ENABLED;
             this.modCount = node.modCount();
-            forward();
         }
 
         @Override