changeset 11531:331f7590b741

modified Node.removeUsage to do less copying (GRAAL-452)
author Doug Simon <doug.simon@oracle.com>
date Thu, 05 Sep 2013 14:50:46 +0200
parents be9e54fbb699
children 6a1b7d28f2d4
files graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Node.java
diffstat 1 files changed, 43 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Node.java	Thu Sep 05 10:59:01 2013 +0200
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/Node.java	Thu Sep 05 14:50:46 2013 +0200
@@ -279,20 +279,25 @@
         }
     }
 
-    private Node removeFirstExtraUsage() {
-        Node res = null;
-        if (extraUsages.length > 0) {
-            res = extraUsages[0];
-            for (int i = 1; i < extraUsages.length; ++i) {
-                Node n = extraUsages[i];
-                extraUsages[i - 1] = n;
-                if (n == null) {
-                    break;
-                }
+    private Node removeLastUsage() {
+        for (int i = extraUsages.length - 1; i >= 0; --i) {
+            Node n = extraUsages[i];
+            if (n != null) {
+                extraUsages[i] = null;
+                return n;
             }
-            extraUsages[extraUsages.length - 1] = null;
         }
-        return res;
+        if (usage1 != null) {
+            Node n = usage1;
+            usage1 = null;
+            return n;
+        }
+        if (usage0 != null) {
+            Node n = usage0;
+            usage0 = null;
+            return n;
+        }
+        return null;
     }
 
     /**
@@ -309,32 +314,41 @@
             return false;
         }
         if (usage0 == node) {
-            usage0 = usage1;
             if (usage1 != null) {
-                usage1 = removeFirstExtraUsage();
+                // usage0 is not the last element
+                usage0 = removeLastUsage();
+            } else {
+                // usage0 is the last element
+                usage0 = null;
             }
             return true;
         }
-        if (usage1 == null) {
-            return false;
-        }
         if (usage1 == node) {
-            usage1 = removeFirstExtraUsage();
+            if (extraUsages.length != 0 && extraUsages[0] != null) {
+                // usage1 is not the last element
+                usage1 = removeLastUsage();
+            } else {
+                // usage1 is the last element
+                usage1 = null;
+            }
             return true;
         }
-        int length = extraUsages.length;
-        for (int i = 0; i < length; i++) {
-            if (extraUsages[i] == node) {
-                for (int j = i + 1; j < length; j++) {
-                    Node toMove = extraUsages[j];
-                    extraUsages[j - 1] = toMove;
-                    if (toMove == null) {
-                        break;
+        for (int i = extraUsages.length - 1; i >= 0; --i) {
+            Node n = extraUsages[i];
+            if (n != null) {
+                if (n == node) {
+                    extraUsages[i] = null;
+                    return true;
+                } else {
+                    for (int j = 0; j < i; j++) {
+                        if (extraUsages[j] == node) {
+                            // Move the last element to the position of 'node'
+                            extraUsages[j] = n;
+                            extraUsages[i] = null;
+                            return true;
+                        }
                     }
                 }
-                extraUsages[length - 1] = null;
-
-                return true;
             }
         }
         return false;