RFR: JDK-8277095 : Empty streams create too many objects
Michael Bien
duke at openjdk.java.net
Mon Nov 15 12:46:26 UTC 2021
On Sun, 7 Nov 2021 03:53:29 GMT, Michael Bien <duke at openjdk.java.net> wrote:
>> src/java.base/share/classes/java/util/Collection.java line 743:
>>
>>> 741: */
>>> 742: default Stream<E> stream() {
>>> 743: if (isEmpty()) return Stream.empty();
>>
>> The net effect of this change might depend on your workload. If you call stream() on empty collections that have cheap isEmpty(), this change will likely improve performance and reduce waste. However, this same change might do the opposite if some of your collections aren't empty or have costly isEmpty(). It would be good to have benchmarks for different workloads.
>
> wouldn't this make streams no longer lazy if the collection is empty?
>
> List<String> list = new ArrayList<>();
> Stream<String> stream = list.stream();
>
> list.addAll(List.of("one", "two", "three"));
>
> stream.forEach(System.out::println); // prints one two three
(immutable collections could override stream() instead, since they don't have that problem)
-------------
PR: https://git.openjdk.java.net/jdk/pull/6275
More information about the core-libs-dev
mailing list