Path: blob/master/test/micro/org/openjdk/bench/java/util/ImmutableColls.java
41161 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.util;2324import org.openjdk.jmh.annotations.*;25import org.openjdk.jmh.infra.Blackhole;2627import java.util.*;28import java.util.concurrent.TimeUnit;2930/**31* Micros for the various collections implemented in32* java.util.ImmutableCollections33*/34@State(Scope.Benchmark)35@OutputTimeUnit(TimeUnit.MICROSECONDS)36@Fork(value = 3)37@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)38@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)39public class ImmutableColls {4041public static String[] STRINGS = {"hi", "all", "of", "you"};4243public static List<String> l0 = List.of();44public static List<String> l1 = List.of(Arrays.copyOf(STRINGS, 1));45public static List<String> l2 = List.of(Arrays.copyOf(STRINGS, 2));46public static List<String> l3 = List.of(Arrays.copyOf(STRINGS, 3));47public static List<String> l4 = List.of(Arrays.copyOf(STRINGS, 4));4849public static Set<String> s0 = Set.copyOf(l0);50public static Set<String> s1 = Set.copyOf(l1);51public static Set<String> s2 = Set.copyOf(l2);52public static Set<String> s3 = Set.copyOf(l3);53public static Set<String> s4 = Set.copyOf(l4);5455public static Map<String, String> m0 = Map.of();56public static Map<String, String> m1 = Map.of(STRINGS[0], STRINGS[0]);57public static Map<String, String> m2 = Map.of(STRINGS[0], STRINGS[0],58STRINGS[1], STRINGS[1]);59public static Map<String, String> m3 = Map.of(STRINGS[0], STRINGS[0],60STRINGS[1], STRINGS[1],61STRINGS[2], STRINGS[2]);62public static Map<String, String> m4 = Map.of(STRINGS[0], STRINGS[0],63STRINGS[1], STRINGS[1],64STRINGS[2], STRINGS[2],65STRINGS[3], STRINGS[3]);6667public static List<String> a0 = new ArrayList<>(l0);68public static List<String> a1 = new ArrayList<>(l1);69public static List<String> a2 = new ArrayList<>(l2);70public static List<String> a3 = new ArrayList<>(l3);71public static List<String> a4 = new ArrayList<>(l4);7273public static final List<String> fl0 = List.of();74public static final List<String> fl1 = List.copyOf(l1);75public static final List<String> fl2 = List.copyOf(l2);76public static final List<String> fl3 = List.copyOf(l3);77public static final List<String> fl4 = List.copyOf(l4);7879public static final List<String> fsl0 = fl0.subList(0, 0);80public static final List<String> fsl1 = fl2.subList(1, 2);81public static final List<String> fsl2 = fl3.subList(0, 2);82public static final List<String> fsl3 = fl4.subList(0, 3);8384public static final Set<String> fs0 = Set.copyOf(l0);85public static final Set<String> fs1 = Set.copyOf(l1);86public static final Set<String> fs2 = Set.copyOf(l2);87public static final Set<String> fs3 = Set.copyOf(l3);88public static final Set<String> fs4 = Set.copyOf(l4);8990public static final Map<String, String> fm0 = Map.copyOf(m0);91public static final Map<String, String> fm1 = Map.copyOf(m1);92public static final Map<String, String> fm2 = Map.copyOf(m2);93public static final Map<String, String> fm3 = Map.copyOf(m3);94public static final Map<String, String> fm4 = Map.copyOf(m4);9596@Benchmark97@CompilerControl(CompilerControl.Mode.DONT_INLINE)98public void constructLists(Blackhole bh) {99bh.consume(List.of(STRINGS[0]));100bh.consume(List.of(STRINGS[0], STRINGS[1]));101bh.consume(List.of(STRINGS[0], STRINGS[1], STRINGS[2]));102}103104@Benchmark105@CompilerControl(CompilerControl.Mode.DONT_INLINE)106public void constructSets(Blackhole bh) {107bh.consume(Set.of(STRINGS[0]));108bh.consume(Set.of(STRINGS[0], STRINGS[1]));109bh.consume(Set.of(STRINGS[0], STRINGS[1], STRINGS[2]));110}111112@Benchmark113@CompilerControl(CompilerControl.Mode.DONT_INLINE)114public int sumSizesList() {115return sizeOf(l0) +116sizeOf(l1) +117sizeOf(l2) +118sizeOf(l3) +119sizeOf(l4);120}121122@Benchmark123@CompilerControl(CompilerControl.Mode.DONT_INLINE)124public int sumSizesFinalList() {125return sizeOf(fl0) +126sizeOf(fl1) +127sizeOf(fl2) +128sizeOf(fl3) +129sizeOf(fl4);130}131132@Benchmark133@CompilerControl(CompilerControl.Mode.DONT_INLINE)134public int sumSizesSet() {135return sizeOf(s0) +136sizeOf(s1) +137sizeOf(s2) +138sizeOf(s3) +139sizeOf(s4);140}141142@Benchmark143@CompilerControl(CompilerControl.Mode.DONT_INLINE)144public int sumSizesFinalSet() {145return sizeOf2(fs0) +146sizeOf2(fs1) +147sizeOf2(fs2) +148sizeOf2(fs3) +149sizeOf2(fs4);150}151152@Benchmark153@CompilerControl(CompilerControl.Mode.DONT_INLINE)154public boolean emptyFinalSet() {155return fs0.isEmpty() &156fs1.isEmpty() &157fs2.isEmpty() &158fs3.isEmpty() &159fs4.isEmpty();160}161162@Benchmark163@CompilerControl(CompilerControl.Mode.DONT_INLINE)164public boolean emptyFinalList() {165return fl0.isEmpty() &166fl1.isEmpty() &167fl2.isEmpty() &168fl3.isEmpty() &169fl4.isEmpty();170}171172@Benchmark173@CompilerControl(CompilerControl.Mode.DONT_INLINE)174public boolean emptyFinalMap() {175return fm0.isEmpty() &176fm1.isEmpty() &177fm2.isEmpty() &178fm3.isEmpty() &179fm4.isEmpty();180}181182@Benchmark183@CompilerControl(CompilerControl.Mode.DONT_INLINE)184public boolean containsFinalSet() {185return fs0.contains("hi") &186fs1.contains("hi") &187fs2.contains("hi") &188fs3.contains("hi") &189fs4.contains("hi");190}191192@Benchmark193@CompilerControl(CompilerControl.Mode.DONT_INLINE)194public boolean containsFinalList() {195return fl0.contains("hi") &196fl1.contains("hi") &197fl2.contains("hi") &198fl3.contains("hi") &199fl4.contains("hi");200}201202@Benchmark203@CompilerControl(CompilerControl.Mode.DONT_INLINE)204public boolean containsKeyFinalMap() {205return fm0.containsKey("hi") &206fm1.containsKey("hi") &207fm2.containsKey("hi") &208fm3.containsKey("hi") &209fm4.containsKey("hi");210}211212@Benchmark213@CompilerControl(CompilerControl.Mode.DONT_INLINE)214public boolean containsValueFinalMap() {215return fm0.containsValue("hi") &216fm1.containsValue("hi") &217fm2.containsValue("hi") &218fm3.containsValue("hi") &219fm4.containsValue("hi");220}221222@Benchmark223@CompilerControl(CompilerControl.Mode.DONT_INLINE)224public void getOrDefault(Blackhole bh) {225bh.consume(fm4.getOrDefault("hi", "test"));226bh.consume(fm4.getOrDefault("not_in_this_map", "test"));227}228229public int sizeOf(List<String> list) {230return list.size();231}232233/**234* Same as sizeOf(List), but duplicated to avoid any235* potential profile pollution with tests using236* sizeOf.237*/238public int sizeOf2(List<String> list) {239return list.size();240}241242public int sizeOf(Set<String> set) {243return set.size();244}245246/**247* Same as sizeOf(Set), but duplicated to avoid any248* potential profile pollution with tests using249* sizeOf.250*/251public int sizeOf2(Set<String> set) {252return set.size();253}254255@Benchmark256@CompilerControl(CompilerControl.Mode.DONT_INLINE)257public int getFromList() {258return get(l1, 0).length() +259get(l2, 1).length() +260get(l3, 2).length() +261get(l4, 3).length();262}263264@Benchmark265@CompilerControl(CompilerControl.Mode.DONT_INLINE)266public int finalGetFromList() {267return get(fl1, 0).length() +268get(fl2, 1).length() +269get(fl3, 2).length() +270get(fl4, 3).length();271}272273@Benchmark274@CompilerControl(CompilerControl.Mode.DONT_INLINE)275public void toArrayFromSet(Blackhole bh) {276bh.consume(fs4.toArray());277bh.consume(s1.toArray());278bh.consume(s3.toArray());279bh.consume(fs2.toArray());280bh.consume(s0.toArray());281}282283@Benchmark284@CompilerControl(CompilerControl.Mode.DONT_INLINE)285public void iterateOverSet(Blackhole bh) {286iterateSet(bh, fs4);287iterateSet(bh, s1);288iterateSet(bh, s3);289iterateSet(bh, fs2);290iterateSet(bh, s0);291}292293public void iterateSet(Blackhole bh, Set<String> coll) {294for (String s : coll) {295bh.consume(s);296}297}298299@Benchmark300@CompilerControl(CompilerControl.Mode.DONT_INLINE)301public void toArrayFromMap(Blackhole bh) {302bh.consume(fm4.entrySet().toArray());303bh.consume(m1.entrySet().toArray());304bh.consume(m3.entrySet().toArray());305bh.consume(fm2.entrySet().toArray());306bh.consume(m0.entrySet().toArray());307}308309@Benchmark310@CompilerControl(CompilerControl.Mode.DONT_INLINE)311public void toArrayFromList(Blackhole bh) {312bh.consume(fl4.toArray());313bh.consume(fl1.toArray());314bh.consume(l3.toArray());315bh.consume(l0.toArray());316bh.consume(fsl3.toArray());317}318319@Benchmark320@CompilerControl(CompilerControl.Mode.DONT_INLINE)321public void toTypedArrayFromSet(Blackhole bh) {322bh.consume(fs4.toArray(new String[0]));323bh.consume(s1.toArray(new String[0]));324bh.consume(s3.toArray(new String[0]));325bh.consume(fs2.toArray(new String[0]));326bh.consume(s0.toArray(new String[0]));327}328329@Benchmark330@CompilerControl(CompilerControl.Mode.DONT_INLINE)331public void toTypedArrayFromMap(Blackhole bh) {332bh.consume(fm4.entrySet().toArray(new Map.Entry[0]));333bh.consume(m1.entrySet().toArray(new Map.Entry[0]));334bh.consume(m3.entrySet().toArray(new Map.Entry[0]));335bh.consume(fm2.entrySet().toArray(new Map.Entry[0]));336bh.consume(m0.entrySet().toArray(new Map.Entry[0]));337}338339@Benchmark340@CompilerControl(CompilerControl.Mode.DONT_INLINE)341public void toTypedArrayFromList(Blackhole bh) {342bh.consume(fl4.toArray(new String[0]));343bh.consume(fl1.toArray(new String[0]));344bh.consume(l3.toArray(new String[0]));345bh.consume(l0.toArray(new String[0]));346bh.consume(fsl3.toArray(new String[0]));347}348349@Benchmark350@CompilerControl(CompilerControl.Mode.DONT_INLINE)351public void copyOfLists(Blackhole bh) {352bh.consume(List.copyOf(fl1));353bh.consume(List.copyOf(fl4));354bh.consume(List.copyOf(fsl2));355bh.consume(List.copyOf(fsl3));356}357358public String get2(List<String> list, int idx) {359return list.get(idx);360}361362public String get(List<String> list, int idx) {363return list.get(idx);364}365366@Benchmark367@CompilerControl(CompilerControl.Mode.DONT_INLINE)368public Set<String> createSetOf() {369return Set.of(STRINGS);370}371}372373374