32 33 package compiler.profiling; 34 35 import jdk.internal.misc.Unsafe; 36 37 public class UnsafeAccess { 38 private static final Unsafe U = Unsafe.getUnsafe(); 39 40 static Class cls = Object.class; 41 static long off = U.ARRAY_OBJECT_BASE_OFFSET; 42 43 static Object testUnsafeAccess(Object o, boolean isObjArray) { 44 if (o != null && cls.isInstance(o)) { // speculates "o" type to int[] 45 return helperUnsafeAccess(o, isObjArray); 46 } 47 return null; 48 } 49 50 static Object helperUnsafeAccess(Object o, boolean isObjArray) { 51 if (isObjArray) { 52 U.putObject(o, off, new Object()); 53 } 54 return o; 55 } 56 57 static Object testUnsafeLoadStore(Object o, boolean isObjArray) { 58 if (o != null && cls.isInstance(o)) { // speculates "o" type to int[] 59 return helperUnsafeLoadStore(o, isObjArray); 60 } 61 return null; 62 } 63 64 static Object helperUnsafeLoadStore(Object o, boolean isObjArray) { 65 if (isObjArray) { 66 Object o1 = U.getObject(o, off); 67 U.compareAndSetObject(o, off, o1, new Object()); 68 } 69 return o; 70 } 71 72 public static void main(String[] args) { 73 Object[] objArray = new Object[10]; 74 int[] intArray = new int[10]; 75 76 for (int i = 0; i < 20_000; i++) { 77 helperUnsafeAccess(objArray, true); 78 } 79 for (int i = 0; i < 20_000; i++) { 80 testUnsafeAccess(intArray, false); 81 } 82 83 for (int i = 0; i < 20_000; i++) { 84 helperUnsafeLoadStore(objArray, true); 85 } 86 for (int i = 0; i < 20_000; i++) { 87 testUnsafeLoadStore(intArray, false); | 32 33 package compiler.profiling; 34 35 import jdk.internal.misc.Unsafe; 36 37 public class UnsafeAccess { 38 private static final Unsafe U = Unsafe.getUnsafe(); 39 40 static Class cls = Object.class; 41 static long off = U.ARRAY_OBJECT_BASE_OFFSET; 42 43 static Object testUnsafeAccess(Object o, boolean isObjArray) { 44 if (o != null && cls.isInstance(o)) { // speculates "o" type to int[] 45 return helperUnsafeAccess(o, isObjArray); 46 } 47 return null; 48 } 49 50 static Object helperUnsafeAccess(Object o, boolean isObjArray) { 51 if (isObjArray) { 52 U.putReference(o, off, new Object()); 53 } 54 return o; 55 } 56 57 static Object testUnsafeLoadStore(Object o, boolean isObjArray) { 58 if (o != null && cls.isInstance(o)) { // speculates "o" type to int[] 59 return helperUnsafeLoadStore(o, isObjArray); 60 } 61 return null; 62 } 63 64 static Object helperUnsafeLoadStore(Object o, boolean isObjArray) { 65 if (isObjArray) { 66 Object o1 = U.getReference(o, off); 67 U.compareAndSetReference(o, off, o1, new Object()); 68 } 69 return o; 70 } 71 72 public static void main(String[] args) { 73 Object[] objArray = new Object[10]; 74 int[] intArray = new int[10]; 75 76 for (int i = 0; i < 20_000; i++) { 77 helperUnsafeAccess(objArray, true); 78 } 79 for (int i = 0; i < 20_000; i++) { 80 testUnsafeAccess(intArray, false); 81 } 82 83 for (int i = 0; i < 20_000; i++) { 84 helperUnsafeLoadStore(objArray, true); 85 } 86 for (int i = 0; i < 20_000; i++) { 87 testUnsafeLoadStore(intArray, false); |