package org.openjdk; import org.openjdk.jmh.annotations.*; import java.util.*; import java.util.function.*; import java.io.IOException; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.ObjectStreamException; import java.io.Serializable; @State(Scope.Benchmark) public class ListMorphism { public static List l0 = List.of(); public static List l1 = List.of("hi"); public static List l2 = List.of("hi", "all"); public static List l3 = List.of("hi", "all", "of"); public static List l4 = List.of("hi", "all", "of", "you"); public static List a0 = new ArrayList<>(List.of()); public static List a1 = new ArrayList<>(List.of("hi")); public static List a2 = new ArrayList<>(List.of("hi", "all")); public static List a3 = new ArrayList<>(List.of("hi", "all", "of")); public static List a4 = new ArrayList<>(List.of("hi", "all", "of", "you")); public static final List fl0 = List.of(); public static final List fl1 = List.of("hi"); public static final List fl2 = List.of("hi", "all"); public static final List fl3 = List.of("hi", "all", "of"); public static final List fl4 = List.of("hi", "all", "of", "you"); public static final List fa0 = new ArrayList<>(List.of()); public static final List fa1 = new ArrayList<>(List.of("hi")); public static final List fa2 = new ArrayList<>(List.of("hi", "all")); public static final List fa3 = new ArrayList<>(List.of("hi", "all", "of")); public static final List fa4 = new ArrayList<>(List.of("hi", "all", "of", "you")); @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int listIndexOf() { return indexOf(l0) + indexOf(l1) + indexOf(l2) + indexOf(l3) + indexOf(l4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int arrayListIndexOf() { return indexOf2(a0) + indexOf2(a1) + indexOf2(a2) + indexOf2(a3) + indexOf2(a4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int listHash() { return hash(l0) + hash(l1) + hash(l2) + hash(l3) + hash(l4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int arrayListHash() { return hash2(a0) + hash2(a1) + hash2(a2) + hash2(a3) + hash2(a4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int sumSizesList() { return sizeOf(l0) + sizeOf(l1) + sizeOf(l2) + sizeOf(l3) + sizeOf(l4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int sumSizesList12() { return sizeOf(l1) + sizeOf(l2); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int finalSumSizesList() { return sizeOf(fl0) + sizeOf(fl1) + sizeOf(fl2) + sizeOf(fl3) + sizeOf(fl4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int sumSizesArrayList() { return sizeOf2(a0) + sizeOf2(a1) + sizeOf2(a2) + sizeOf2(a3) + sizeOf2(a4); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int sumSizesArrayList12() { return sizeOf2(a1) + sizeOf2(a2); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int finalSumSizesArrayList() { return sizeOf2(fa0) + sizeOf2(fa1) + sizeOf2(fa2) + sizeOf2(fa3) + sizeOf2(fa4); } public int indexOf2(List list) { return list.indexOf("all"); } public int indexOf(List list) { return list.indexOf("all"); } public int hash2(List list) { return list.hashCode(); } public int hash(List list) { return list.hashCode(); } public int sizeOf2(List list) { return list.size(); } public int sizeOf(List list) { return list.size(); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int getFromList() { return get(l1, 0).length() + get(l2, 1).length() + get(l2, 0).length() + get(l3, 2).length() + get(l4, 3).length(); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int getFromList12() { return get(l1, 0).length() + get(l2, 1).length() + get(l2, 0).length(); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int finalGetFromList() { return get(fl1, 0).length() + get(fl2, 1).length() + get(fl2, 0).length() + get(fl3, 2).length() + get(fl4, 3).length(); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int getFromArrayList() { return get2(a1, 0).length() + get2(a2, 1).length() + get(a2, 0).length() + get2(a3, 2).length() + get2(a4, 3).length(); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int getFromArrayList12() { return get2(a1, 0).length() + get2(a2, 1).length() + get2(a2, 0).length(); } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public int finalGetFromArrayList() { return get2(fa1, 0).length() + get2(fa2, 1).length() + get2(fa3, 2).length() + get2(fa4, 3).length(); } public String get2(List list, int idx) { return list.get(idx); } public String get(List list, int idx) { return list.get(idx); } }