< prev index next >

test/micro/org/openjdk/bench/java/util/ImmutableColls.java

Print this page
rev 57961 : 8238684: Override getOrDefault in immutable Map implementation
Reviewed-by: smarks


  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.java.util;
  24 
  25 import org.openjdk.jmh.annotations.*;
  26 import org.openjdk.jmh.infra.Blackhole;
  27 
  28 import java.util.*;
  29 import java.util.concurrent.TimeUnit;
  30 
  31 /**
  32  * Micros for the various collections implemented in
  33  * java.util.ImmutableCollections
  34  */
  35 @State(Scope.Benchmark)
  36 @OutputTimeUnit(TimeUnit.MICROSECONDS)



  37 public class ImmutableColls {
  38 
  39     public static String[] STRINGS = {"hi", "all", "of", "you"};
  40 
  41     public static List<String> l0 = List.of();
  42     public static List<String> l1 = List.of(Arrays.copyOf(STRINGS, 1));
  43     public static List<String> l2 = List.of(Arrays.copyOf(STRINGS, 2));
  44     public static List<String> l3 = List.of(Arrays.copyOf(STRINGS, 3));
  45     public static List<String> l4 = List.of(Arrays.copyOf(STRINGS, 4));
  46 
  47     public static Set<String> s0 = Set.copyOf(l0);
  48     public static Set<String> s1 = Set.copyOf(l1);
  49     public static Set<String> s2 = Set.copyOf(l2);
  50     public static Set<String> s3 = Set.copyOf(l3);
  51     public static Set<String> s4 = Set.copyOf(l4);
  52 
  53     public static Map<String, String> m0 = Map.of();
  54     public static Map<String, String> m1 = Map.of(STRINGS[0], STRINGS[0]);
  55     public static Map<String, String> m2 = Map.of(STRINGS[0], STRINGS[0],
  56                                                   STRINGS[1], STRINGS[1]);


 198     }
 199 
 200     @Benchmark
 201     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 202     public boolean containsKeyFinalMap() {
 203         return fm0.containsKey("hi") &
 204                 fm1.containsKey("hi") &
 205                 fm2.containsKey("hi") &
 206                 fm3.containsKey("hi") &
 207                 fm4.containsKey("hi");
 208     }
 209 
 210     @Benchmark
 211     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 212     public boolean containsValueFinalMap() {
 213         return fm0.containsValue("hi") &
 214                 fm1.containsValue("hi") &
 215                 fm2.containsValue("hi") &
 216                 fm3.containsValue("hi") &
 217                 fm4.containsValue("hi");







 218     }
 219 
 220     public int sizeOf(List<String> list) {
 221         return list.size();
 222     }
 223 
 224     /**
 225      * Same as sizeOf(List), but duplicated to avoid any
 226      * potential profile pollution with tests using
 227      * sizeOf.
 228      */
 229     public int sizeOf2(List<String> list) {
 230         return list.size();
 231     }
 232 
 233     public int sizeOf(Set<String> set) {
 234         return set.size();
 235     }
 236 
 237     /**




  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.java.util;
  24 
  25 import org.openjdk.jmh.annotations.*;
  26 import org.openjdk.jmh.infra.Blackhole;
  27 
  28 import java.util.*;
  29 import java.util.concurrent.TimeUnit;
  30 
  31 /**
  32  * Micros for the various collections implemented in
  33  * java.util.ImmutableCollections
  34  */
  35 @State(Scope.Benchmark)
  36 @OutputTimeUnit(TimeUnit.MICROSECONDS)
  37 @Fork(value = 3)
  38 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
  39 @Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
  40 public class ImmutableColls {
  41 
  42     public static String[] STRINGS = {"hi", "all", "of", "you"};
  43 
  44     public static List<String> l0 = List.of();
  45     public static List<String> l1 = List.of(Arrays.copyOf(STRINGS, 1));
  46     public static List<String> l2 = List.of(Arrays.copyOf(STRINGS, 2));
  47     public static List<String> l3 = List.of(Arrays.copyOf(STRINGS, 3));
  48     public static List<String> l4 = List.of(Arrays.copyOf(STRINGS, 4));
  49 
  50     public static Set<String> s0 = Set.copyOf(l0);
  51     public static Set<String> s1 = Set.copyOf(l1);
  52     public static Set<String> s2 = Set.copyOf(l2);
  53     public static Set<String> s3 = Set.copyOf(l3);
  54     public static Set<String> s4 = Set.copyOf(l4);
  55 
  56     public static Map<String, String> m0 = Map.of();
  57     public static Map<String, String> m1 = Map.of(STRINGS[0], STRINGS[0]);
  58     public static Map<String, String> m2 = Map.of(STRINGS[0], STRINGS[0],
  59                                                   STRINGS[1], STRINGS[1]);


 201     }
 202 
 203     @Benchmark
 204     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 205     public boolean containsKeyFinalMap() {
 206         return fm0.containsKey("hi") &
 207                 fm1.containsKey("hi") &
 208                 fm2.containsKey("hi") &
 209                 fm3.containsKey("hi") &
 210                 fm4.containsKey("hi");
 211     }
 212 
 213     @Benchmark
 214     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 215     public boolean containsValueFinalMap() {
 216         return fm0.containsValue("hi") &
 217                 fm1.containsValue("hi") &
 218                 fm2.containsValue("hi") &
 219                 fm3.containsValue("hi") &
 220                 fm4.containsValue("hi");
 221     }
 222 
 223     @Benchmark
 224     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 225     public void getOrDefault(Blackhole bh) {
 226         bh.consume(fm4.getOrDefault("hi", "test"));
 227         bh.consume(fm4.getOrDefault("not_in_this_map", "test"));
 228     }
 229 
 230     public int sizeOf(List<String> list) {
 231         return list.size();
 232     }
 233 
 234     /**
 235      * Same as sizeOf(List), but duplicated to avoid any
 236      * potential profile pollution with tests using
 237      * sizeOf.
 238      */
 239     public int sizeOf2(List<String> list) {
 240         return list.size();
 241     }
 242 
 243     public int sizeOf(Set<String> set) {
 244         return set.size();
 245     }
 246 
 247     /**


< prev index next >