1 /*
 2  * Copyright (c) 2016, 2018, 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.sandbox.corelibs.corelibs.mapprotos;
24 
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.CompilerControl;
27 import org.openjdk.jmh.annotations.Setup;
28 import org.openjdk.jmh.annotations.State;
29 
30 import java.util.Iterator;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.function.IntFunction;
34 
35 public class WalkX extends MapBase {
36 
37     IntFunction<Map<Integer, Integer>> mapSupplier;
38     Map<Integer, Integer> map;
39 
40     @Setup
41     public void setup() {
42         super.init(size);
43         try {
44             Class<?> mapClass = Class.forName(mapType);
45             mapSupplier =  (size) -> newInstance(mapClass, size);
46         } catch (Exception ex) {
47             System.out.printf("%s: %s%n", mapType, ex.getMessage());
48             return;
49         }
50 
51         map = mapSupplier.apply(0);
52         for (Integer k : keys) {
53             map.put(k, k);
54         }
55     }
56 
57     Map<Integer, Integer> newInstance(Class<?> mapClass, int size) {
58         try {
59             return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size);
60         } catch (Exception ex) {
61             throw new RuntimeException("failed", ex);
62         }
63     }
64 
65     @Benchmark
66     public int sumIterator() {
67         int s = 0;
68         for (Iterator<Integer> iterator = map.keySet().iterator(); iterator.hasNext(); ) {
69             s += iterator.next();
70         }
71         return s;
72     }
73 
74     @Benchmark
75     public int sumIteratorHidden() {
76         int s = 0;
77         for (Iterator<Integer> iterator = hide(map.keySet().iterator()); iterator.hasNext(); ) {
78             s += iterator.next();
79         }
80         return s;
81     }
82 
83     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
84     private static Iterator<Integer> hide(Iterator<Integer> it) {
85         return it;
86     }
87 
88 }