1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class TypeCheckBenchmark {
timeCheckCastLevel1ToLevel1(int count)18     public void timeCheckCastLevel1ToLevel1(int count) {
19         Object[] arr = arr1;
20         for (int i = 0; i < count; ++i) {
21             Level1 l1 = (Level1) arr[i & 1023];
22         }
23     }
24 
timeCheckCastLevel2ToLevel1(int count)25     public void timeCheckCastLevel2ToLevel1(int count) {
26         Object[] arr = arr2;
27         for (int i = 0; i < count; ++i) {
28             Level1 l1 = (Level1) arr[i & 1023];
29         }
30     }
31 
timeCheckCastLevel3ToLevel1(int count)32     public void timeCheckCastLevel3ToLevel1(int count) {
33         Object[] arr = arr3;
34         for (int i = 0; i < count; ++i) {
35             Level1 l1 = (Level1) arr[i & 1023];
36         }
37     }
38 
timeCheckCastLevel9ToLevel1(int count)39     public void timeCheckCastLevel9ToLevel1(int count) {
40         Object[] arr = arr9;
41         for (int i = 0; i < count; ++i) {
42             Level1 l1 = (Level1) arr[i & 1023];
43         }
44     }
45 
timeCheckCastLevel9ToLevel2(int count)46     public void timeCheckCastLevel9ToLevel2(int count) {
47         Object[] arr = arr9;
48         for (int i = 0; i < count; ++i) {
49             Level2 l2 = (Level2) arr[i & 1023];
50         }
51     }
52 
timeInstanceOfLevel1ToLevel1(int count)53     public void timeInstanceOfLevel1ToLevel1(int count) {
54         int sum = 0;
55         Object[] arr = arr1;
56         for (int i = 0; i < count; ++i) {
57             if (arr[i & 1023] instanceof Level1) {
58               ++sum;
59             }
60         }
61         result = sum;
62     }
63 
timeInstanceOfLevel2ToLevel1(int count)64     public void timeInstanceOfLevel2ToLevel1(int count) {
65         int sum = 0;
66         Object[] arr = arr2;
67         for (int i = 0; i < count; ++i) {
68             if (arr[i & 1023] instanceof Level1) {
69               ++sum;
70             }
71         }
72         result = sum;
73     }
74 
timeInstanceOfLevel3ToLevel1(int count)75     public void timeInstanceOfLevel3ToLevel1(int count) {
76         int sum = 0;
77         Object[] arr = arr3;
78         for (int i = 0; i < count; ++i) {
79             if (arr[i & 1023] instanceof Level1) {
80               ++sum;
81             }
82         }
83         result = sum;
84     }
85 
timeInstanceOfLevel9ToLevel1(int count)86     public void timeInstanceOfLevel9ToLevel1(int count) {
87         int sum = 0;
88         Object[] arr = arr9;
89         for (int i = 0; i < count; ++i) {
90             if (arr[i & 1023] instanceof Level1) {
91               ++sum;
92             }
93         }
94         result = sum;
95     }
96 
timeInstanceOfLevel9ToLevel2(int count)97     public void timeInstanceOfLevel9ToLevel2(int count) {
98         int sum = 0;
99         Object[] arr = arr9;
100         for (int i = 0; i < count; ++i) {
101             if (arr[i & 1023] instanceof Level2) {
102               ++sum;
103             }
104         }
105         result = sum;
106     }
107 
createArray(int level)108     public static Object[] createArray(int level) {
109         try {
110             Class<?>[] ls = {
111                     null,
112                     Level1.class,
113                     Level2.class,
114                     Level3.class,
115                     Level4.class,
116                     Level5.class,
117                     Level6.class,
118                     Level7.class,
119                     Level8.class,
120                     Level9.class,
121             };
122             Class<?> l = ls[level];
123             Object[] array = new Object[1024];
124             for (int i = 0; i < array.length; ++i) {
125                 array[i] = l.newInstance();
126             }
127             return array;
128         } catch (Exception unexpected) {
129             throw new Error("Initialization failure!");
130         }
131     }
132     Object[] arr1 = createArray(1);
133     Object[] arr2 = createArray(2);
134     Object[] arr3 = createArray(3);
135     Object[] arr9 = createArray(9);
136     int result;
137 }
138 
139 class Level1 { }
140 class Level2 extends Level1 { }
141 class Level3 extends Level2 { }
142 class Level4 extends Level3 { }
143 class Level5 extends Level4 { }
144 class Level6 extends Level5 { }
145 class Level7 extends Level6 { }
146 class Level8 extends Level7 { }
147 class Level9 extends Level8 { }
148