RFR: 8327858: Improve spliterator and forEach for single-element immutable collections
Viktor Klang
vklang at openjdk.org
Tue Mar 12 10:15:14 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.
src/java.base/share/classes/java/util/ImmutableCollections.java line 926:
> 924: if (!REVERSE && e1 != EMPTY) {
> 925: action.accept((E) e1);
> 926: }
I'm curious to know how the following alternative would fare:
Suggestion:
if (e1 != EMPTY) {
action.accept(REVERSE ? (E)e1 : (E)e0); // implicit null check
action.accept(REVERSE ? (E)e0 : (E)e1);
} else {
action.accept(e0); // Implicit null check
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15834#discussion_r1521196650
More information about the core-libs-dev
mailing list