1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.vm.compiler;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Level;
  28 import org.openjdk.jmh.annotations.Mode;
  29 import org.openjdk.jmh.annotations.OutputTimeUnit;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 
  34 import java.util.Stack;
  35 import java.util.Vector;
  36 import java.util.concurrent.TimeUnit;
  37 
  38 /**
  39  * Benchmarking measuring ArrayStoreCheck-performance plus the ability of the optimizer to remove storechecks
  40  * altogether.
  41  */
  42 @BenchmarkMode(Mode.AverageTime)
  43 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  44 @SuppressWarnings("rawtypes")
  45 @State(Scope.Thread)
  46 public class ArrayStoreCheck {
  47 
  48     /** How large should the test-arrays be. */
  49     public static final int TESTSIZE = 1000;
  50 
  51     private Vector[] fromVectorArr, toVectorArr;
  52 
  53     private Object[] fromObjectArr, toObjectArr;
  54 
  55     private Object[] fromObject2Arr, toObject2Arr;
  56 
  57     private Object[] fromObject3Arr, toObject3Arr;
  58 
  59     private Object[] fromObject4Arr, toObject4Arr;
  60 
  61     @Setup(Level.Iteration)
  62     public void createArrays() {
  63         fromVectorArr = new Vector[TESTSIZE];
  64         toVectorArr = new Vector[TESTSIZE];
  65 
  66         fromObjectArr = fromVectorArr;
  67         toObjectArr = toVectorArr;
  68 
  69         /* set every almost 90% of all indices to an object. */
  70         for (int i = 0; i < TESTSIZE; i++) {
  71             fromVectorArr[i] = new Vector();
  72         }
  73         for (int i = 0; i < TESTSIZE; i += 10) {
  74             fromVectorArr[i] = null;
  75         }
  76 
  77         fromObject2Arr = new Vector[TESTSIZE][1][][][];
  78         toObject2Arr = new Vector[TESTSIZE][1][][][];
  79 
  80         fromObject3Arr = new Stack[TESTSIZE][1][][][];
  81         toObject3Arr = new Vector[TESTSIZE][1][][][];
  82 
  83         fromObject4Arr = new Object[TESTSIZE];
  84         toObject4Arr = new Comparable[TESTSIZE];
  85         /* set every two indices to an object. */
  86         for (int i = 0; i < TESTSIZE; i += 2) {
  87             fromObject4Arr[i] = new String("apa?");
  88         }
  89     }
  90 
  91     /**
  92      * Test that loads from an Vector[] and stores in another Vector[]. The local types of the arrays are both Vector[].
  93      * Hopefully we only will do a runtime check that we are storing in an exact Vector[].
  94      */
  95     @Benchmark
  96     public void testArrayStoreCheckRT1() throws Exception {
  97         Vector[] localFromArray = fromVectorArr;
  98         Vector[] localToArray = toVectorArr;
  99 
 100         for (int i = 0; i < TESTSIZE; i++) {
 101             localToArray[i] = localFromArray[i];
 102         }
 103     }
 104 
 105     /**
 106      * Test that stores a newly created Vector in a Vector[]. The local type of the array is Vector[]. Hopefully we only
 107      * will do a runtime check that we are storing in the same Vector[].
 108      */
 109     @Benchmark
 110     public void testArrayStoreCheckRT2() throws Exception {
 111         Vector[] localToArray = toVectorArr;
 112         Vector localVector = new Vector();
 113 
 114         for (int i = 0; i < TESTSIZE; i++) {
 115             localToArray[i] = localVector;
 116         }
 117     }
 118 
 119     /**
 120      * Test that loads from a Vector[] and stores in the same Vector[]. Hopefully we only will remove the storecheck
 121      * altogether due to the fact that the arrays are the same (and easily proven that they are they same).
 122      */
 123     @Benchmark
 124     public void testArrayStoreCheckRemove1() throws Exception {
 125         Vector[] localToArray = toVectorArr;
 126         for (int i = 2; i < TESTSIZE; i++) {
 127             localToArray[i] = localToArray[i - 2];
 128         }
 129     }
 130 
 131     /**
 132      * Test that loads from a Vector[] and stores in another Vector[]. The local types of the arrays are both Object[].
 133      * This should be a tricky case where we statically have no clue what type the arrays actually are of. We should have
 134      * to do a complex check.
 135      */
 136     @Benchmark
 137     public void testArrayStoreCheckComplex1() throws Exception {
 138         Object[] localFromArray = fromObjectArr;
 139         Object[] localToArray = toObjectArr;
 140 
 141         for (int i = 0; i < TESTSIZE; i++) {
 142             localToArray[i] = localFromArray[i];
 143         }
 144     }
 145 
 146     /**
 147      * Test that loads from a Vector[][][][] and stores in another Vector[][][][]. The local types of the arrays are both
 148      * Object[]. This should be a tricky case where we statically have no clue what type the arrays actually are of. We
 149      * should have to do a complex check. Difference from complex1-test is that the actual types of the arrays are
 150      * multi-dimensioned.
 151      */
 152     @Benchmark
 153     public void testArrayStoreCheckComplex2() throws Exception {
 154         Object[] localFromArray = fromObject2Arr;
 155         Object[] localToArray = toObject2Arr;
 156 
 157         for (int i = 0; i < TESTSIZE; i++) {
 158             localToArray[i] = localFromArray[i];
 159         }
 160     }
 161 
 162     /**
 163      * Test that loads from a Stack[][][][] and stores in a Vector[][][][]. The local types of the arrays are both
 164      * Object[]. This should be a tricky case where we statically have no clue what type the arrays actually are of. We
 165      * should have to do a complex check. Difference from complex2-test is that the actual types of the from-array is
 166      * different from the actual type of the to-array.
 167      */
 168     @Benchmark
 169     public void testArrayStoreCheckComplex3() throws Exception {
 170         Object[] localFromArray = fromObject3Arr;
 171         Object[] localToArray = toObject3Arr;
 172 
 173         for (int i = 0; i < TESTSIZE; i++) {
 174             localToArray[i] = localFromArray[i];
 175         }
 176     }
 177 
 178     /**
 179      * Test that loads from a Object[] and stores in a Comparable[]. The local types of the arrays are both Object[]. This
 180      * should be a tricky case where we statically have no clue what type the arrays actually are of. We should have to do
 181      * a complex check. The interesting part with this test is that the destination array is an interface array.
 182      */
 183     @Benchmark
 184     public void testArrayStoreCheckComplex4() throws Exception {
 185         Object[] localFromArray = fromObject4Arr;
 186         Object[] localToArray = toObject4Arr;
 187 
 188         for (int i = 0; i < TESTSIZE; i++) {
 189             localToArray[i] = localFromArray[i];
 190         }
 191     }
 192 
 193 }