diff graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java @ 18795:e9cbe1618733

Truffle: refactor size calculation in location allocator to visitor pattern
author Andreas Woess <andreas.woess@jku.at>
date Wed, 07 Jan 2015 15:11:38 +0100
parents 6db7923af642
children 068256ee3b90
line wrap: on
line diff
--- a/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java	Mon Jan 05 19:01:01 2015 +0100
+++ b/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java	Wed Jan 07 15:11:38 2015 +0100
@@ -31,7 +31,7 @@
 import com.oracle.truffle.api.nodes.*;
 import com.oracle.truffle.api.object.*;
 import com.oracle.truffle.api.utilities.*;
-import com.oracle.truffle.object.LocationImpl.InternalLongLocation;
+import com.oracle.truffle.object.LocationImpl.*;
 import com.oracle.truffle.object.Locations.ConstantLocation;
 import com.oracle.truffle.object.Locations.DeclaredDualLocation;
 import com.oracle.truffle.object.Locations.DeclaredLocation;
@@ -954,7 +954,7 @@
         return null;
     }
 
-    public abstract static class BaseAllocator extends Allocator {
+    public abstract static class BaseAllocator extends Allocator implements LocationVisitor {
         protected final LayoutImpl layout;
         protected int objectArraySize;
         protected int objectFieldSize;
@@ -1056,15 +1056,13 @@
         protected <T extends Location> T advance(T location0) {
             if (location0 instanceof LocationImpl) {
                 LocationImpl location = (LocationImpl) location0;
-                objectArraySize += location.objectArrayCount();
-                primitiveArraySize += location.primitiveArrayCount();
-                primitiveFieldSize += location.primitiveFieldCount();
+                if (location != layout.getPrimitiveArrayLocation()) {
+                    location.accept(this);
+                }
                 if (layout.hasPrimitiveExtensionArray()) {
-                    hasPrimitiveArray |= location == layout.getPrimitiveArrayLocation() || location.primitiveArrayCount() != 0;
-                    objectFieldSize = location == layout.getPrimitiveArrayLocation() ? objectFieldSize : Math.max(objectFieldSize, layout.objectFieldIndex(location) + location.objectFieldCount());
+                    hasPrimitiveArray |= location == layout.getPrimitiveArrayLocation() || primitiveArraySize > 0;
                 } else {
-                    assert !hasPrimitiveArray && location.primitiveArrayCount() == 0;
-                    objectFieldSize += location.objectFieldCount();
+                    assert !hasPrimitiveArray && primitiveArraySize == 0;
                 }
             }
             depth++;
@@ -1076,6 +1074,22 @@
             advance(location);
             return this;
         }
+
+        public void visitObjectField(int index, int count) {
+            objectFieldSize = Math.max(objectFieldSize, index + count);
+        }
+
+        public void visitObjectArray(int index, int count) {
+            objectArraySize = Math.max(objectArraySize, index + count);
+        }
+
+        public void visitPrimitiveArray(int index, int count) {
+            primitiveArraySize = Math.max(primitiveArraySize, index + count);
+        }
+
+        public void visitPrimitiveField(int index, int count) {
+            primitiveFieldSize = Math.max(primitiveFieldSize, index + count);
+        }
     }
 
     private static void debugRegisterShape(ShapeImpl newShape) {