RFR: 8327858: Improve spliterator and forEach for single-element immutable collections [v2]
Chen Liang
liach at openjdk.org
Fri Mar 22 00:25:25 UTC 2024
On Thu, 21 Mar 2024 20:09:23 GMT, Viktor Klang <vklang at openjdk.org> wrote:
>> Chen Liang has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains 13 commits:
>>
>> - Use the improved form in forEach
>> - Merge branch 'master' of https://github.com/openjdk/jdk into feature/imm-coll-stream
>> - Null checks should probably be in the beginning...
>> - mark implicit null checks
>> - Merge branch 'master' of https://github.com/openjdk/jdk into feature/imm-coll-stream
>> - Copyright year, revert changes for non-few element collections
>> - Merge branch 'master' of https://github.com/openjdk/jdk into feature/imm-coll-stream
>> - Merge branch 'feature/imm-coll-stream' of https://github.com/liachmodded/jdk into feature/imm-coll-stream
>> - Spliterator for 12, iterate/forEach benchmark
>> - fix comments
>> - ... and 3 more: https://git.openjdk.org/jdk/compare/d5b95a0e...69bd0e9c
>
> src/java.base/share/classes/java/util/ImmutableCollections.java line 924:
>
>> 922: action.accept(REVERSE ? (E)e1 : e0); // implicit null check
>> 923: action.accept(REVERSE ? e0 : (E)e1);
>> 924: }
>
> Out of curiosity, how does the following fare performance-wise?
>
> Suggestion:
>
> action.accept((!REVERSE || e1 == EMPTY) ? e0 : (E)e1); // implicit null check
> if (e1 != EMPTY)
> action.accept(!REVERSE ? (E)e1 : e0);
Benchmark Mode Cnt Score Error Units
ImmutableColls.forEachOverList thrpt 15 361.423 ± 8.751 ops/us
ImmutableColls.forEachOverSet thrpt 15 79.158 ± 5.064 ops/us
ImmutableColls.getOrDefault thrpt 15 244.012 ± 0.943 ops/us
ImmutableColls.iterateOverList thrpt 15 152.598 ± 3.687 ops/us
ImmutableColls.iterateOverSet thrpt 15 61.969 ± 4.453 ops/us
The 3 results are also available at https://gist.github.com/f0b4336e5b1cf9c5299ebdbcd82232bf, where baseline is the master this patch currently is based on (which has WhiteBoxResizeTest failures), patch-0 being the current code, and patch-1 being your proposal (uncommited patch below).
diff --git a/src/java.base/share/classes/java/util/ImmutableCollections.java b/src/java.base/share/classes/java/util/ImmutableCollections.java
index fc232a521fb..f38b093cf60 100644
--- a/src/java.base/share/classes/java/util/ImmutableCollections.java
+++ b/src/java.base/share/classes/java/util/ImmutableCollections.java
@@ -916,12 +916,9 @@ public <T> T[] toArray(T[] a) {
@Override
@SuppressWarnings("unchecked")
public void forEach(Consumer<? super E> action) {
- if (e1 == EMPTY) {
- action.accept(e0); // implicit null check
- } else {
- action.accept(REVERSE ? (E)e1 : e0); // implicit null check
- action.accept(REVERSE ? e0 : (E)e1);
- }
+ action.accept((!REVERSE || e1 == EMPTY) ? e0 : (E) e1); // implicit null check
+ if (e1 != EMPTY)
+ action.accept(!REVERSE ? (E) e1 : e0);
}
@Override
My testing shows that the existing version I have is most likely faster than your proposed version.
Also note that the test failures are from WhiteBoxResizeTest that's fixed in latest master; I decide not to pull as not to invalidate the existing benchmark baselines.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15834#discussion_r1534886983
More information about the core-libs-dev
mailing list