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 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.CompilerControl; 28 import org.openjdk.jmh.annotations.Fork; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Param; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 36 import java.util.Arrays; 37 import java.util.Collections; 38 import java.util.HashMap; 39 import java.util.Map; 40 import java.util.function.Supplier; 41 import java.util.function.IntFunction; 42 import java.util.concurrent.TimeUnit; 43 44 45 @BenchmarkMode(Mode.AverageTime) 46 @OutputTimeUnit(TimeUnit.MILLISECONDS) 47 @Fork(1) 48 @State(Scope.Thread) 49 public class ReplX extends MapBase { 50 IntFunction<Map<Integer, Integer>> mapSupplier; 51 Integer[] mixed; 52 Map<Integer, Integer> map; 53 54 @Param(value = {"org.openjdk.bench.valhalla.corelibs.mapprotos.YHashMap", 55 "org.openjdk.bench.valhalla.corelibs.mapprotos.XHashMap", 56 "java.util.HashMap"}) 57 private String mapType; 58 59 @Setup 60 public void setup() { 61 super.init(size); 62 try { 63 Class<?> mapClass = Class.forName(mapType); 64 mapSupplier = (size) -> newInstance(mapClass, size); 65 66 map = mapSupplier.apply(0); 67 for (Integer k : keys) { 68 map.put(k, k); 69 } 70 mixed = new Integer[size]; 71 System.arraycopy(keys, 0, mixed, 0, size / 2); 72 System.arraycopy(nonKeys, 0, mixed, size / 2, size / 2); 73 Collections.shuffle(Arrays.asList(mixed), rnd); 74 } catch (Exception ex) { 75 System.out.printf("%s: %s%n", mapType, ex.getMessage()); 76 return; 77 } 78 } 79 80 Map<Integer, Integer> newInstance(Class<?> mapClass, int size) { 81 try { 82 return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size); 83 } catch (Exception ex) { 84 throw new RuntimeException("failed", ex); 85 } 86 } 87 88 @Benchmark 89 public Map<Integer, Integer> replace() { 90 Integer[] keys = this.keys; 91 for (Integer k : mixed) { 92 if (map.size() < size) { 93 map.put(k, k); 94 } else { 95 map.remove(k); 96 } 97 } 98 return map; 99 } 100 101 }