changeset 22421:71c7a1ae8829

In ExactClassValueProfile: Perform direct class check, make profile thread-safe, avoid cast in the interpreter, add documentation.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 28 Nov 2015 00:42:25 +0100
parents 5e2dd9c3fa7d
children 30644770899d
files truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ExactClassValueProfile.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ValueProfile.java
diffstat 2 files changed, 16 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ExactClassValueProfile.java	Thu Nov 26 14:22:48 2015 +0100
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ExactClassValueProfile.java	Sat Nov 28 00:42:25 2015 +0100
@@ -40,12 +40,17 @@
     @SuppressWarnings("unchecked")
     @Override
     public <T> T profile(T value) {
-        if (cachedClass != Object.class) {
-            if (cachedClass != null && cachedClass.isInstance(value)) {
-                return (T) cachedClass.cast(value);
+        Class<?> c = cachedClass;
+        if (c != Object.class) {
+            if (c != null && value != null && c == value.getClass()) {
+                if (CompilerDirectives.inInterpreter()) {
+                    return value;
+                } else {
+                    return (T) c.cast(value);
+                }
             } else {
                 CompilerDirectives.transferToInterpreterAndInvalidate();
-                if (cachedClass == null && value != null) {
+                if (c == null && value != null) {
                     cachedClass = value.getClass();
                 } else {
                     cachedClass = Object.class;
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ValueProfile.java	Thu Nov 26 14:22:48 2015 +0100
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ValueProfile.java	Sat Nov 28 00:42:25 2015 +0100
@@ -57,7 +57,13 @@
     }
 
     /**
-     * Returns a {@link ValueProfile} that speculates on the exact class of a value.
+     * Returns a {@link ValueProfile} that speculates on the exact class of a value. It will check
+     * the class of the profiled value and provide additional information to the compiler if only
+     * non-null values of exactly one concrete Java class are passed as a parameter to the
+     * {@link ValueProfile#profile} method. This can be beneficial if subsequent code can take
+     * advantage of knowing the concrete class of the value. The profile will degrade to the generic
+     * case if a null value or if at least two instances of two different Java classes are
+     * registered.
      */
     public static ValueProfile createClassProfile() {
         return new ExactClassValueProfile();