RFR: JDK-8277095 : Empty streams create too many objects

kabutz duke at openjdk.java.net
Mon Nov 15 20:00:41 UTC 2021


On Mon, 15 Nov 2021 18:23:16 GMT, Philippe Marschall <duke at openjdk.java.net> wrote:

> > ```
> > 3. I made many methods just return `this` after checking for operated on and closed:
> > ```
> 
> Reading the Javadoc again I'm not sure this is allowed. The method Javadoc doesn't clearly say it but the package Javadoc for intermediate operations says a new stream is returned.

Yes, I also returned "this" initially to avoid object allocation, but fortunately escape analysis helps get rid of them if they are indeed unnecessary. My object allocation for the new empty streams is minimal.

The one thing that is still hindering me is the lazy creation of streams.


$ jshell
|  Welcome to JShell -- Version 11.0.12
|  For an introduction type: /help intro

jshell> var list = new ArrayList<Integer>()
list ==> []

jshell> var stream = list.stream()
stream ==> java.util.stream.ReferencePipeline$Head at cb5822

jshell> Collections.addAll(list, 1,2,3)
$3 ==> true

jshell> stream.forEach(System.out::print)
123
jshell> 



Does anyone use this "feature"? I hope not, but unfortunately it's the current behavior and we probably have to support it :-(

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

PR: https://git.openjdk.java.net/jdk/pull/6275


More information about the core-libs-dev mailing list