RFR: JDK-8306860: Avoid unnecessary allocation in List.map() when list is empty

Archie Cobbs acobbs at openjdk.org
Tue Apr 25 15:41:59 UTC 2023


On Tue, 11 Apr 2023 10:07:20 GMT, ExE Boss <duke at openjdk.org> wrote:

>> I agree with @dreis2211 here. Someone might want to use/mutate the returned `List` later on.
>
> This won’t be a change in behaviour as `ListBuffer::toList()` returns `List.nil()` when called on an empty `ListBuffer`.
> 
> --------------------------------------------------------------------------------
> 
> e.g.: The following evaluates to `true`:
> 
> new ListBuffer().toList() == List.nil()

Jumping in here... I think what @ExE-Boss is getting at is that every empty `List` is the same object, i.e., `List.EMPTY_LIST`, i.e., `List.nil()`. In other words, it's not possible to ever have an empty `List` that's **not** `List.nil()`.

So while it appears that replacing `(List<Z>)this` with `List.<Z>nil()` is a change in behavior, in the special case that `this.isEmpty()` is true (which is the case here), it's not a change in behavior.

Of course this is only due to a quirk in the implementation, but the statement is still correct.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/13407#discussion_r1172640189


More information about the compiler-dev mailing list