RFR: JDK-8306860: Avoid unnecessary allocation in List.map() when list is empty
Christoph Dreis
duke at openjdk.org
Tue Apr 25 15:41:51 UTC 2023
On Mon, 10 Apr 2023 17:14:00 GMT, ExE Boss <duke at openjdk.org> wrote:
>> Hi,
>>
>> I've been profiling our compilation tasks lately and noticed that `List.map` is under the top consumers.
>>
>> <img width="1045" alt="image" src="https://user-images.githubusercontent.com/6304496/230922012-d9c8f63f-beba-4e8a-a33c-367fb3cbf147.png">
>>
>> There are probably more aggressive options to optimize this, but I found that checking for empty lists already reduces the overall allocations by ~500MB for one of our compilation tasks with no measurable regressions (but also no noticeable improvements) in timings.
>>
>> <img width="943" alt="image" src="https://user-images.githubusercontent.com/6304496/230922383-e0363eca-bc87-4ad2-8465-ca1e801bd164.png">
>>
>> In case you consider this worthwhile, I'd appreciate a sponsoring of this (including a ticket because I have no rights to create one, review etc.). I've found https://bugs.openjdk.org/browse/JDK-8032359 but this was closed as won't fix.
>>
>> Let me know what you think.
>>
>> Cheers,
>> Christoph
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/util/List.java line 423:
>
>> 421: public <Z> List<Z> map(Function<A, Z> mapper) {
>> 422: if (isEmpty()) {
>> 423: return (List<Z>)this;
>
> Use `List.nil()` instead, in case this list gets mutated later:
> Suggestion:
>
> return List.<Z>nil();
Wouldn't this change the logic compared to what it did before? If the list gets mutated later - e.g. via `setTail` - this would fail with `List.nil()`.
Do I miss something here?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/13407#discussion_r1161912840
More information about the compiler-dev
mailing list