From 74f47cf76ce05ebcfa103332dd8bfe1c184d03fd Mon Sep 17 00:00:00 2001 From: Sergey Tsypanov Date: Thu, 11 Feb 2021 13:40:40 +0200 Subject: [PATCH 1/4] 8274715: Implement forEach in Collections.CopiesList --- .../share/classes/java/util/Collections.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/java.base/share/classes/java/util/Collections.java b/src/java.base/share/classes/java/util/Collections.java index a88d70fedc638..1527738bfc79a 100644 --- a/src/java.base/share/classes/java/util/Collections.java +++ b/src/java.base/share/classes/java/util/Collections.java @@ -140,7 +140,6 @@ private Collections() { * found to violate the {@link Comparable} contract * @see List#sort(Comparator) */ - @SuppressWarnings("unchecked") public static > void sort(List list) { list.sort(null); } @@ -174,7 +173,6 @@ public static > void sort(List list) { * found to violate the {@link Comparator} contract * @see List#sort(Comparator) */ - @SuppressWarnings({"unchecked", "rawtypes"}) public static void sort(List list, Comparator c) { list.sort(c); } @@ -1861,7 +1859,7 @@ static class UnmodifiableNavigableMap private static final long serialVersionUID = -4858195264774772197L; /** - * A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve + * A class for the {@link #EMPTY_NAVIGABLE_MAP} which needs readResolve * to preserve singleton property. * * @param type of keys, if there were any, and of bounds @@ -1884,7 +1882,7 @@ public NavigableSet navigableKeySet() } /** - * Singleton for {@link emptyNavigableMap()} which is also immutable. + * Singleton for {@link #emptyNavigableMap()} which is also immutable. */ private static final EmptyNavigableMap EMPTY_NAVIGABLE_MAP = new EmptyNavigableMap<>(); @@ -5151,6 +5149,16 @@ public E get(int index) { return element; } + @Override + public void forEach(Consumer action) { + Objects.requireNonNull(action); + var n = this.n; + var element = this.element; + for (int i = 0; i < n; i++) { + action.accept(element); + } + } + public Object[] toArray() { final Object[] a = new Object[n]; if (element != null) @@ -5397,7 +5405,7 @@ public Comparator reversed() { * @see Enumeration */ public static Enumeration enumeration(final Collection c) { - return new Enumeration() { + return new Enumeration<>() { private final Iterator i = c.iterator(); public boolean hasMoreElements() { From a005c7ffc92ff321b9b7e44a6a82fe8d857874c8 Mon Sep 17 00:00:00 2001 From: Sergey Tsypanov Date: Tue, 5 Oct 2021 11:21:03 +0300 Subject: [PATCH 2/4] 8274715: Revert some irrelevant changes --- .../share/classes/java/util/Collections.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/java.base/share/classes/java/util/Collections.java b/src/java.base/share/classes/java/util/Collections.java index 1527738bfc79a..e75447f1a4e6a 100644 --- a/src/java.base/share/classes/java/util/Collections.java +++ b/src/java.base/share/classes/java/util/Collections.java @@ -140,6 +140,7 @@ private Collections() { * found to violate the {@link Comparable} contract * @see List#sort(Comparator) */ + @SuppressWarnings("unchecked") public static > void sort(List list) { list.sort(null); } @@ -173,6 +174,7 @@ public static > void sort(List list) { * found to violate the {@link Comparator} contract * @see List#sort(Comparator) */ + @SuppressWarnings({"unchecked", "rawtypes"}) public static void sort(List list, Comparator c) { list.sort(c); } @@ -1859,7 +1861,7 @@ static class UnmodifiableNavigableMap private static final long serialVersionUID = -4858195264774772197L; /** - * A class for the {@link #EMPTY_NAVIGABLE_MAP} which needs readResolve + * A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve * to preserve singleton property. * * @param type of keys, if there were any, and of bounds @@ -1882,7 +1884,7 @@ public NavigableSet navigableKeySet() } /** - * Singleton for {@link #emptyNavigableMap()} which is also immutable. + * Singleton for {@link emptyNavigableMap()} which is also immutable. */ private static final EmptyNavigableMap EMPTY_NAVIGABLE_MAP = new EmptyNavigableMap<>(); @@ -5152,8 +5154,8 @@ public E get(int index) { @Override public void forEach(Consumer action) { Objects.requireNonNull(action); - var n = this.n; - var element = this.element; + int n = this.n; + E element = this.element; for (int i = 0; i < n; i++) { action.accept(element); } @@ -5405,7 +5407,7 @@ public Comparator reversed() { * @see Enumeration */ public static Enumeration enumeration(final Collection c) { - return new Enumeration<>() { + return new Enumeration() { private final Iterator i = c.iterator(); public boolean hasMoreElements() { From 2883f9f3bb72c54609e1b01e8b882f99522f612a Mon Sep 17 00:00:00 2001 From: Sergey Tsypanov Date: Tue, 5 Oct 2021 11:21:23 +0300 Subject: [PATCH 3/4] 8274715: Add NCopiesBenchmarks.java --- .../bench/java/util/NCopiesBenchmarks.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java diff --git a/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java b/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java new file mode 100644 index 0000000000000..9bc77ea6d9a06 --- /dev/null +++ b/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java @@ -0,0 +1,33 @@ +package micro.org.openjdk.bench.java.util; + +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * @see JDK-8274715 + */ +@Fork(value = 3) +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +public class NCopiesBenchmarks { + @Param({"10", "50", "100"}) + int size; + + private List list; + + @Setup + public void prepare() { + list = Collections.nCopies(size, new Object()); + } + + @Benchmark + public void forEach(Blackhole bh) { + list.forEach(bh::consume); + } +} \ No newline at end of file From 7655be8f45651841dd4b324f476ca720c3a6c2b6 Mon Sep 17 00:00:00 2001 From: Sergey Tsypanov Date: Tue, 5 Oct 2021 20:45:42 +0300 Subject: [PATCH 4/4] 8274715: Properly format NCopiesBenchmarks.java --- .../bench/java/util/NCopiesBenchmarks.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java b/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java index 9bc77ea6d9a06..0fb845059ed92 100644 --- a/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java +++ b/test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java @@ -16,18 +16,19 @@ @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) public class NCopiesBenchmarks { - @Param({"10", "50", "100"}) - int size; + @Param({"10", "50", "100"}) + int size; - private List list; + private List list; - @Setup - public void prepare() { - list = Collections.nCopies(size, new Object()); - } + @Setup + public void prepare() { + list = Collections.nCopies(size, new Object()); + } - @Benchmark - public void forEach(Blackhole bh) { - list.forEach(bh::consume); - } -} \ No newline at end of file + @Benchmark + public void forEach(Blackhole bh) { + list.forEach(bh::consume); + } + +}