changeset 16133:d568574e6448

reduce allocations of NodeIterables by Graph.getNewNodes()
author Doug Simon <doug.simon@oracle.com>
date Wed, 18 Jun 2014 11:28:23 +0200
parents 7143d614bb20
children 17af09bd9e75
files graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Graph.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/EmptyNodeIterable.java
diffstat 2 files changed, 81 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Graph.java	Wed Jun 18 10:39:40 2014 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Graph.java	Wed Jun 18 11:28:23 2014 +0200
@@ -22,6 +22,8 @@
  */
 package com.oracle.graal.graph;
 
+import static com.oracle.graal.graph.iterators.EmptyNodeIterable.*;
+
 import java.util.*;
 
 import com.oracle.graal.compiler.common.*;
@@ -461,6 +463,14 @@
         int getValue() {
             return value;
         }
+
+        /**
+         * Determines if this mark still represents the {@linkplain Graph#getNodeCount() live node
+         * count} of the graph.
+         */
+        public boolean isCurrent() {
+            return value == graph.nodeIdCount();
+        }
     }
 
     /**
@@ -525,6 +535,9 @@
      * mark}.
      */
     public NodeIterable<Node> getNewNodes(Mark mark) {
+        if (mark.isCurrent()) {
+            return emptyNodeIterable();
+        }
         final int index = mark.getValue();
         return new NodeIterable<Node>() {
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/iterators/EmptyNodeIterable.java	Wed Jun 18 11:28:23 2014 +0200
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.graph.iterators;
+
+import java.util.*;
+
+import com.oracle.graal.graph.*;
+
+/**
+ * Provides an immutable {@link NodeIterable} implementation that returns no elements.
+ */
+public final class EmptyNodeIterable<T extends Node> implements NodeIterable<T> {
+
+    /**
+     * Returns an empty NodeIterable (immutable).
+     */
+    @SuppressWarnings("unchecked")
+    public static final <T extends Node> NodeIterable<T> emptyNodeIterable() {
+        return EMPTY_LIST;
+    }
+
+    @SuppressWarnings("rawtypes") private static final NodeIterable EMPTY_LIST = new EmptyNodeIterable();
+
+    private final Iterator<T> iterator = new Iterator<T>() {
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+
+        public T next() {
+            throw new NoSuchElementException();
+        }
+
+        public boolean hasNext() {
+            return false;
+        }
+    };
+
+    /**
+     * Only {@link #emptyNodeIterable()} should be used.
+     */
+    private EmptyNodeIterable() {
+    }
+
+    public Iterator<T> iterator() {
+        return iterator;
+    }
+}
\ No newline at end of file