On Wed, 20 Sep 2023 04:52:31 GMT, Chen Liang <li...@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