RFR: 8327858: Improve spliterator and forEach for single-element immutable collections [v2]
Viktor Klang
vklang at openjdk.org
Fri Mar 22 09:27:23 UTC 2024
On Fri, 22 Mar 2024 00:21:56 GMT, Chen Liang <liach at openjdk.org> wrote:
>> 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.
Thanks. I was mostly trying to gauge what the bottleneck might be.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15834#discussion_r1535286326
More information about the core-libs-dev
mailing list