Skip to content

Commit

Permalink
8274715: Implement forEach in Collections.CopiesList
Browse files Browse the repository at this point in the history
Reviewed-by: martin
  • Loading branch information
stsypanov authored and Martin Buchholz committed Oct 5, 2021
1 parent d4e8712 commit df7b0c7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/java.base/share/classes/java/util/Collections.java
Expand Up @@ -5182,6 +5182,16 @@ public E get(int index) {
return element;
}

@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
int n = this.n;
E 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)
Expand Down
34 changes: 34 additions & 0 deletions test/micro/org/openjdk/bench/java/util/NCopiesBenchmarks.java
@@ -0,0 +1,34 @@
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 <a href="https://bugs.openjdk.java.net/browse/JDK-8274715">JDK-8274715</a>
*/
@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<Object> list;

@Setup
public void prepare() {
list = Collections.nCopies(size, new Object());
}

@Benchmark
public void forEach(Blackhole bh) {
list.forEach(bh::consume);
}

}

1 comment on commit df7b0c7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.