1 /*
  2  * Copyright (c) 2019, 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.valhalla.corelibs.mapprotos;
 24 
 25 
 26 import org.openjdk.jmh.annotations.Benchmark;
 27 import org.openjdk.jmh.annotations.BenchmarkMode;
 28 import org.openjdk.jmh.annotations.Fork;
 29 import org.openjdk.jmh.annotations.Measurement;
 30 import org.openjdk.jmh.annotations.Mode;
 31 import org.openjdk.jmh.annotations.OutputTimeUnit;
 32 import org.openjdk.jmh.annotations.Param;
 33 import org.openjdk.jmh.annotations.Scope;
 34 import org.openjdk.jmh.annotations.Setup;
 35 import org.openjdk.jmh.annotations.State;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.util.Arrays;
 39 import java.util.HashMap;
 40 import java.util.LinkedHashMap;
 41 import java.util.Map;
 42 import java.util.function.IntFunction;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 /**
 46  * Benchmark                               (mapType)  (size)  Mode  Cnt         Score         Error  Units
 47  * XHashMapToArray.testKeySetToArray        XHashMap       0  avgt   10         9.996 ±       2.010  ns/op
 48  * XHashMapToArray.testKeySetToArray        XHashMap       1  avgt   10      4142.267 ±     180.789  ns/op
 49  * XHashMapToArray.testKeySetToArray        XHashMap      10  avgt   10      4240.651 ±     202.434  ns/op
 50  * XHashMapToArray.testKeySetToArray        XHashMap    1000  avgt   10    494789.609 ±   13140.044  ns/op
 51  * XHashMapToArray.testKeySetToArray        XHashMap  100000  avgt   10  60665517.560 ± 2386064.520  ns/op
 52  * XHashMapToArray.testKeySetToArray         HashMap       0  avgt   10        10.547 ±       1.317  ns/op
 53  * XHashMapToArray.testKeySetToArray         HashMap       1  avgt   10        30.381 ±       2.341  ns/op
 54  * XHashMapToArray.testKeySetToArray         HashMap      10  avgt   10        71.436 ±       1.356  ns/op
 55  * XHashMapToArray.testKeySetToArray         HashMap    1000  avgt   10      6184.091 ±     391.274  ns/op
 56  * XHashMapToArray.testKeySetToArray         HashMap  100000  avgt   10    635572.324 ±   12811.539  ns/op
 57  * XHashMapToArray.testKeySetToArrayTyped   XHashMap       0  avgt   10         7.350 ±       1.093  ns/op
 58  * XHashMapToArray.testKeySetToArrayTyped   XHashMap       1  avgt   10      3949.242 ±     162.359  ns/op
 59  * XHashMapToArray.testKeySetToArrayTyped   XHashMap      10  avgt   10      4078.345 ±     194.142  ns/op
 60  * XHashMapToArray.testKeySetToArrayTyped   XHashMap    1000  avgt   10    527815.326 ±   27872.781  ns/op
 61  * XHashMapToArray.testKeySetToArrayTyped   XHashMap  100000  avgt   10  63033220.728 ± 3078028.203  ns/op
 62  * XHashMapToArray.testKeySetToArrayTyped    HashMap       0  avgt   10         7.607 ±       0.863  ns/op
 63  * XHashMapToArray.testKeySetToArrayTyped    HashMap       1  avgt   10        34.312 ±       0.831  ns/op
 64  * XHashMapToArray.testKeySetToArrayTyped    HashMap      10  avgt   10       105.760 ±       0.670  ns/op
 65  * XHashMapToArray.testKeySetToArrayTyped    HashMap    1000  avgt   10      9994.524 ±     156.952  ns/op
 66  * XHashMapToArray.testKeySetToArrayTyped    HashMap  100000  avgt   10    991598.536 ±   12037.671  ns/op
 67  * XHashMapToArray.testValuesToArray        XHashMap       0  avgt   10        10.078 ±       1.625  ns/op
 68  * XHashMapToArray.testValuesToArray        XHashMap       1  avgt   10      3798.391 ±     192.066  ns/op
 69  * XHashMapToArray.testValuesToArray        XHashMap      10  avgt   10      4035.747 ±     181.076  ns/op
 70  * XHashMapToArray.testValuesToArray        XHashMap    1000  avgt   10    500692.191 ±   17923.078  ns/op
 71  * XHashMapToArray.testValuesToArray        XHashMap  100000  avgt   10  64450148.368 ± 5112519.509  ns/op
 72  * XHashMapToArray.testValuesToArray         HashMap       0  avgt   10        10.510 ±       1.691  ns/op
 73  * XHashMapToArray.testValuesToArray         HashMap       1  avgt   10        40.833 ±       4.335  ns/op
 74  * XHashMapToArray.testValuesToArray         HashMap      10  avgt   10        70.101 ±       2.298  ns/op
 75  * XHashMapToArray.testValuesToArray         HashMap    1000  avgt   10      6154.291 ±     173.222  ns/op
 76  * XHashMapToArray.testValuesToArray         HashMap  100000  avgt   10    647159.888 ±   28626.401  ns/op
 77  * XHashMapToArray.testValuesToArrayTyped   XHashMap       0  avgt   10         7.440 ±       1.522  ns/op
 78  * XHashMapToArray.testValuesToArrayTyped   XHashMap       1  avgt   10      3912.895 ±     229.372  ns/op
 79  * XHashMapToArray.testValuesToArrayTyped   XHashMap      10  avgt   10      4221.022 ±     248.348  ns/op
 80  * XHashMapToArray.testValuesToArrayTyped   XHashMap    1000  avgt   10    508209.209 ±   20811.752  ns/op
 81  * XHashMapToArray.testValuesToArrayTyped   XHashMap  100000  avgt   10  63079414.458 ± 3164118.537  ns/op
 82  * XHashMapToArray.testValuesToArrayTyped    HashMap       0  avgt   10         7.948 ±       1.340  ns/op
 83  * XHashMapToArray.testValuesToArrayTyped    HashMap       1  avgt   10        35.295 ±       2.964  ns/op
 84  * XHashMapToArray.testValuesToArrayTyped    HashMap      10  avgt   10       102.239 ±       1.685  ns/op
 85  * XHashMapToArray.testValuesToArrayTyped    HashMap    1000  avgt   10      9913.778 ±     577.131  ns/op
 86  * XHashMapToArray.testValuesToArrayTyped    HashMap  100000  avgt   10   1178696.903 ±   39326.922  ns/op
 87  * Finished running test 'micro:valhalla.corelibs.XHashMapToArray'
 88  */
 89 
 90 @BenchmarkMode(Mode.AverageTime)
 91 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 92 @Warmup(iterations = 3, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 93 @Measurement(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 94 @Fork(1)
 95 @State(Scope.Thread)
 96 public class HashMapToArray {
 97 
 98     private IntFunction<Map<Integer, Integer>> mapSupplier;
 99     Map<Integer, Integer> map;
100 
101 
102     @Param(value = {"org.openjdk.bench.valhalla.corelibs.mapprotos.YHashMap",
103             "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap",
104             "java.util.HashMap"})
105     private String mapType;
106 
107     @Param({"1", "10", "1000", "100000"})
108     public int size;
109 
110     @Setup
111     public void setup() {
112         try {
113             Class<?> mapClass = Class.forName(mapType);
114             mapSupplier =  (size) -> newInstance(mapClass, size);
115         } catch (Exception ex) {
116             System.out.printf("%s: %s%n", mapType, ex.getMessage());
117             return;
118         }
119 
120         map = mapSupplier.apply(0);
121         for (int i = 0; i < size; i++) {
122             map.put(i, i * i);
123         }
124     }
125 
126     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
127         try {
128             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
129         } catch (Exception ex) {
130             throw new RuntimeException("failed", ex);
131         }
132     }
133 
134     @Benchmark
135     public Object[] testKeySetToArray() {
136         return map.keySet().toArray();
137     }
138 
139     @Benchmark
140     public Object[] testKeySetToArrayTyped() {
141         return map.keySet().toArray(new Integer[0]);
142     }
143 
144     @Benchmark
145     public Object[] testValuesToArray() {
146         return map.values().toArray();
147     }
148 
149     @Benchmark
150     public Object[] testValuesToArrayTyped() {
151         return map.values().toArray(new Integer[0]);
152     }
153 }