RFR: 8327858: Improve spliterator and forEach for single-element immutable collections
Chen Liang
liach at openjdk.org
Tue Mar 12 00:11:17 UTC 2024
On Wed, 20 Sep 2023 04:52:31 GMT, Chen Liang <liach at openjdk.org> wrote:
> Please review this patch that:
> 1. Implemented `forEach` to optimize for 1 or 2 element collections.
> 2. Implemented `spliterator` to optimize for a single element.
>
> The default implementations for multiple-element immutable collections are fine as-is, specializing implementation doesn't provide much benefit.
Benchmark results of Oracle JDK 21 vs the initial revision: https://jmh.morethan.io/?gists=c7a8398f291bedb2e1ad6d07c1d5cd97,8ef1c0826e0ae4e3e9a5e43df3dda230
It might be worth experimenting if manually unrolling the loop to be:
`for (int i = startIndex(); i > 0; i--)` and `for (int i = n; i > startIndex; i--)`
will be faster than the current index fetching.
Baseline results:
Benchmark Mode Cnt Score Error Units
ImmutableColls.forEachOverList thrpt 15 351.497 ± 2.108 ops/us
ImmutableColls.forEachOverSet thrpt 15 71.954 ± 3.732 ops/us
ImmutableColls.getOrDefault thrpt 15 243.968 ± 0.823 ops/us
ImmutableColls.iterateOverList thrpt 15 150.105 ± 3.371 ops/us
ImmutableColls.iterateOverSet thrpt 15 62.071 ± 4.239 ops/us
Current version 8036e9e:
Benchmark Mode Cnt Score Error Units
ImmutableColls.forEachOverList thrpt 15 364.515 ± 8.076 ops/us
ImmutableColls.forEachOverSet thrpt 15 86.012 ± 2.420 ops/us
ImmutableColls.getOrDefault thrpt 15 243.723 ± 1.071 ops/us
ImmutableColls.iterateOverList thrpt 15 149.392 ± 5.328 ops/us
ImmutableColls.iterateOverSet thrpt 15 63.579 ± 4.671 ops/us
Improvements can be seen in the `forEachOver(Set|List)` benchmarks.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15834#issuecomment-1727000156
PR Comment: https://git.openjdk.org/jdk/pull/15834#issuecomment-1811960742
PR Comment: https://git.openjdk.org/jdk/pull/15834#issuecomment-1989671088
More information about the core-libs-dev
mailing list