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

Archie Cobbs acobbs at openjdk.org
Tue Apr 25 15:42:01 UTC 2023

On Thu, 20 Apr 2023 14:01:41 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

>> 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.

Actually, having just said that, I think I'm wrong. It's possible to say `list.setTail(null)`. So I take back what I just said.


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

More information about the compiler-dev mailing list