RFR: JDK-8277095 : Empty streams create too many objects
kabutz
duke at openjdk.java.net
Mon Nov 15 12:46:27 UTC 2021
On Sun, 7 Nov 2021 04:26:13 GMT, Michael Bien <duke at openjdk.java.net> wrote:
>> 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)
> 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.
Yes, I also thought about the cost of isEmpty() on concurrent collections. There are four concurrent collections that have a linear time cost size() method: CLQ, CLD, LTQ and CHM. However, in each of these cases, the isEmpty() method has constant time cost. There might be collections defined outside the JDK where this could be the case.
However, I will extend the benchmark to include a few of those cases too, as well as different sizes and collection sizes.
Thank you so much for your input.
-------------
PR: https://git.openjdk.java.net/jdk/pull/6275
More information about the core-libs-dev
mailing list