diff graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java @ 18627:196cf131ed32

OM: extract queryTransition method
author Andreas Woess <andreas.woess@jku.at>
date Thu, 04 Dec 2014 19:36:50 +0100
parents ce46f909c176
children 6db7923af642
line wrap: on
line diff
--- a/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java	Thu Dec 04 19:31:45 2014 +0100
+++ b/graal/com.oracle.truffle.object/src/com/oracle/truffle/object/ShapeImpl.java	Thu Dec 04 19:36:50 2014 +0100
@@ -318,6 +318,17 @@
         return propertyMap;
     }
 
+    private ShapeImpl queryTransition(Transition transition) {
+        ShapeImpl cachedShape = this.getTransitionMapForRead().get(transition);
+        if (cachedShape != null) { // Shape already exists?
+            shapeCacheHitCount.inc();
+            return (ShapeImpl) layout.getStrategy().returnCached(cachedShape);
+        }
+        shapeCacheMissCount.inc();
+
+        return null;
+    }
+
     /**
      * Add a new property in the map, yielding a new or cached Shape object.
      *
@@ -347,19 +358,16 @@
         assert !getPropertyListInternal(false).contains(prop);
         // invalidatePropertyAssumption(prop.getName());
 
-        AddPropertyTransition key = new AddPropertyTransition(prop);
-        Map<Transition, ShapeImpl> transitionMapForRead = this.getTransitionMapForRead();
-        ShapeImpl cachedShape = transitionMapForRead.get(key);
-        if (cachedShape != null) { // Shape already exists?
-            shapeCacheHitCount.inc();
-            return (ShapeImpl) layout.getStrategy().returnCached(cachedShape);
+        AddPropertyTransition addTransition = new AddPropertyTransition(prop);
+        ShapeImpl cachedShape = queryTransition(addTransition);
+        if (cachedShape != null) {
+            return cachedShape;
         }
-        shapeCacheMissCount.inc();
 
         ShapeImpl oldShape = (ShapeImpl) layout.getStrategy().ensureSpace(this, prop.getLocation());
 
-        ShapeImpl newShape = makeShapeWithAddedProperty(oldShape, key);
-        oldShape.addDirectTransition(key, newShape);
+        ShapeImpl newShape = makeShapeWithAddedProperty(oldShape, addTransition);
+        oldShape.addDirectTransition(addTransition, newShape);
         return newShape;
     }
 
@@ -417,17 +425,15 @@
 
     private ShapeImpl addPrimitiveExtensionArray() {
         assert layout.hasPrimitiveExtensionArray() && !hasPrimitiveArray();
-        Transition key = new ReservePrimitiveArrayTransition();
-        ShapeImpl cachedShape = this.getTransitionMapForRead().get(key);
+        Transition transition = new ReservePrimitiveArrayTransition();
+        ShapeImpl cachedShape = queryTransition(transition);
         if (cachedShape != null) {
-            shapeCacheHitCount.inc();
-            return (ShapeImpl) layout.getStrategy().returnCached(cachedShape);
+            return cachedShape;
         }
-        shapeCacheMissCount.inc();
 
         ShapeImpl oldShape = (ShapeImpl) layout.getStrategy().ensureSpace(this, layout.getPrimitiveArrayLocation());
-        ShapeImpl newShape = makeShapeWithPrimitiveExtensionArray(oldShape, key);
-        oldShape.addDirectTransition(key, newShape);
+        ShapeImpl newShape = makeShapeWithPrimitiveExtensionArray(oldShape, transition);
+        oldShape.addDirectTransition(transition, newShape);
         return newShape;
     }
 
@@ -629,9 +635,9 @@
     @Override
     public final ShapeImpl removeProperty(Property prop) {
         RemovePropertyTransition transition = new RemovePropertyTransition(prop);
-        ShapeImpl cachedShape = getTransitionMapForRead().get(transition);
+        ShapeImpl cachedShape = queryTransition(transition);
         if (cachedShape != null) {
-            return (ShapeImpl) layout.getStrategy().returnCached(cachedShape);
+            return cachedShape;
         }
 
         ShapeImpl shape = getShapeFromProperty(prop);
@@ -684,9 +690,9 @@
     @Override
     public final ShapeImpl replaceProperty(Property oldProperty, Property newProperty) {
         Transition replacePropertyTransition = new Transition.ReplacePropertyTransition(oldProperty, newProperty);
-        ShapeImpl cachedShape = getTransitionMapForRead().get(replacePropertyTransition);
+        ShapeImpl cachedShape = queryTransition(replacePropertyTransition);
         if (cachedShape != null) {
-            return (ShapeImpl) layout.getStrategy().returnCached(cachedShape);
+            return cachedShape;
         }
 
         ShapeImpl top = this;
@@ -761,9 +767,6 @@
         return newShape;
     }
 
-    /**
-     * NB: this is not an accurate property count.
-     */
     @Override
     public final int getPropertyCount() {
         return propertyCount;
@@ -846,14 +849,14 @@
     @TruffleBoundary
     public final ShapeImpl changeType(ObjectType newOps) {
         ObjectTypeTransition transition = new ObjectTypeTransition(newOps);
-        ShapeImpl cachedShape = getTransitionMapForRead().get(transition);
+        ShapeImpl cachedShape = queryTransition(transition);
         if (cachedShape != null) {
             return cachedShape;
-        } else {
-            ShapeImpl newShape = createShape(layout, sharedData, this, newOps, propertyMap, transition, allocator(), id);
-            addDirectTransition(transition, newShape);
-            return newShape;
         }
+
+        ShapeImpl newShape = createShape(layout, sharedData, this, newOps, propertyMap, transition, allocator(), id);
+        addDirectTransition(transition, newShape);
+        return newShape;
     }
 
     @Override