Skip to content

Commit

Permalink
Spliterator for 12, iterate/forEach benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Sep 20, 2023
1 parent 2d0a815 commit fbedf61
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/java.base/share/classes/java/util/ImmutableCollections.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,14 @@ public void forEach(Consumer<? super E> action) {
action.accept((E) e1);
}
}

@Override
public Spliterator<E> spliterator() {
if (e1 == EMPTY) {
return Collections.singletonSpliterator(e0);
}
return super.spliterator();
}
}

@jdk.internal.ValueBased
Expand Down Expand Up @@ -916,6 +924,14 @@ public void forEach(Consumer<? super E> action) {
action.accept((E) e1);
}
}

@Override
public Spliterator<E> spliterator() {
if (e1 == EMPTY) {
return Collections.singletonSpliterator(e0);
}
return super.spliterator();
}
}


Expand Down
46 changes: 45 additions & 1 deletion test/micro/org/openjdk/bench/java/util/ImmutableColls.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -297,6 +297,50 @@ public void iterateSet(Blackhole bh, Set<String> coll) {
}
}

@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void forEachOverSet(Blackhole bh) {
forEachSet(bh, fs4);
forEachSet(bh, s1);
forEachSet(bh, s3);
forEachSet(bh, fs2);
forEachSet(bh, s0);
}

public void forEachSet(Blackhole bh, Set<String> coll) {
coll.forEach(bh::consume);
}

@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void iterateOverList(Blackhole bh) {
iterateList(bh, fl4);
iterateList(bh, fl1);
iterateList(bh, l3);
iterateList(bh, l0);
iterateList(bh, fl2);
}

public void iterateList(Blackhole bh, List<String> coll) {
for (String s : coll) {
bh.consume(s);
}
}

@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void forEachOverList(Blackhole bh) {
forEachList(bh, fl4);
forEachList(bh, fl1);
forEachList(bh, l3);
forEachList(bh, l0);
forEachList(bh, fl2);
}

public void forEachList(Blackhole bh, List<String> coll) {
coll.forEach(bh::consume);
}

@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void toArrayFromMap(Blackhole bh) {
Expand Down

0 comments on commit fbedf61

Please sign in to comment.