<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 0);"><br>
</span></div>
<blockquote style="margin-right: 0px; margin-left: 0.8ex; padding-left: 1ex; border-left: 3px solid rgb(200, 200, 200);">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
And for A.andThen(B), A.flags & B.flags should work, the stream is sorted if both gatherers keep it sorted.</div>
</blockquote>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
That is unfortunately not the case. That would presume that you can implement the composition such that it can preserve all the common flags. Some flags could be "dominant" and some "recessive" to use genetics nomenclature.<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<blockquote style="margin-right: 0px; margin-left: 0.8ex; padding-left: 1ex; border-left: 3px solid rgb(200, 200, 200);">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I suppose that if those flags already exist, it's because they have a purpose and i do not understand how it can make the other operations slower.</div>
</blockquote>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Extra invocations, extra storage, and extra composition overhead is not free. Since Stream is one-shot you need to include the construction cost with the execution cost. For something like an empty Stream construction cost scan be 90+% of the total costs.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="Signature">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
¡Ì</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="appendonsend"></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<hr style="display: inline-block; width: 98%;">
<div id="divRplyFwdMsg" dir="ltr"><span style="font-family: Calibri, sans-serif; font-size: 11pt; color: rgb(0, 0, 0);"><b>From:</b> forax@univ-mlv.fr <forax@univ-mlv.fr><br>
<b>Sent:</b> Thursday, 18 January 2024 16:17<br>
<b>To:</b> Viktor Klang <viktor.klang@oracle.com><br>
<b>Cc:</b> core-libs-dev <core-libs-dev@openjdk.java.net>; Paul Sandoz <psandoz@openjdk.java.net><br>
<b>Subject:</b> [External] : Re: Gatherer: spliterator characteristics are not propagated</span>
<div> </div>
</div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<hr>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<span style="font-family: Helvetica, Arial, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><b>From:
</b>"Viktor Klang" <viktor.klang@oracle.com><br>
<b>To: </b>"Remi Forax" <forax@univ-mlv.fr><br>
<b>Cc: </b>"core-libs-dev" <core-libs-dev@openjdk.java.net>, "Paul Sandoz" <psandoz@openjdk.java.net><br>
<b>Sent: </b>Thursday, January 18, 2024 3:36:07 PM<br>
<b>Subject: </b>Re: Gatherer: spliterator characteristics are not propagated<br>
</span></blockquote>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I suspect that it is a rather slippery slope, once KEEP-flags are added, then others will want to be able to have INJECT-flags, and then people might have different opinions w.r.t. the default should be to clear all flags etc.</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
And that's even before one looks at the composition-part of it, what are the flags for A.andThen(B)? (then extrapolate to N compositions and the available set of flags always approaches 0)</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I spent quite a bit of time on this and in the end tracking all this info, and making sure that the flags of implementations correspond to the actual behavior, just ended up costing performance for most streams and introduced an extra dimension to creation
 and maintenance which I had a hard time justifying.</div>
</blockquote>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">It can be a slippery slope if we were designing from the ground up but the stream implementation already exists and SORTED, DISTINCT and SIZED are the flags
 that are already tracked by the current implementation.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Currently only SHORT_CIRCUIT is set (if not greedy),</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">see
<a href="https://urldefense.com/v3/__https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/stream/GathererOp.java*L209__;Iw!!ACWV5N9M2RV99hQ!PhMxqlDzLWPRuwYc7ECRKNPVs0BtnoE-RdT-Jdkng7S-iFuERAHYcWvJ-OMKGLrkPdSrUl3xj1R9ypyeqeWI$" id="OWA1a26d136-987c-a5b2-e72e-dde19d919aa9" class="OWAAutoLink" data-auth="NotApplicable" data-loopstyle="linkonly">
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/stream/GathererOp.java#L209</a></span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">And for A.andThen(B), A.flags & B.flags should work, the stream is sorted if both gatherers keep it sorted.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Making specific, rare, combinations of operations faster at the expense of making 99% of all others slower is a hard pill for most to swallow.</div>
</blockquote>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">I suppose that if those flags already exist, it's because they have a purpose and i do not understand how it can make the other operations slower.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="x_Signature">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
¡Ì</div>
</div>
</blockquote>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">regards,</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">R¨¦mi</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div id="x_Signature">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<hr style="display: inline-block; width: 98%;">
<div id="x_divRplyFwdMsg" dir="ltr"><span style="font-family: Calibri, sans-serif; font-size: 11pt; color: rgb(0, 0, 0);"><b>From:</b> forax@univ-mlv.fr <forax@univ-mlv.fr><br>
<b>Sent:</b> Thursday, 18 January 2024 10:28<br>
<b>To:</b> Viktor Klang <viktor.klang@oracle.com><br>
<b>Cc:</b> core-libs-dev <core-libs-dev@openjdk.java.net>; Paul Sandoz <psandoz@openjdk.java.net><br>
<b>Subject:</b> [External] : Re: Gatherer: spliterator characteristics are not propagated</span>
<div> </div>
</div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<hr>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<span style="font-family: Helvetica, Arial, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><b>From:
</b>"Viktor Klang" <viktor.klang@oracle.com><br>
<b>To: </b>"Remi Forax" <forax@univ-mlv.fr>, "core-libs-dev" <core-libs-dev@openjdk.java.net><br>
<b>Sent: </b>Wednesday, January 17, 2024 8:48:07 PM<br>
<b>Subject: </b>Re: Gatherer: spliterator characteristics are not propagated<br>
</span></blockquote>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi R¨¦mi,</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div><span style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">You can find some of my benches here:
<a href="https://urldefense.com/v3/__https://github.com/openjdk/jdk/tree/master/test/micro/org/openjdk/bench/java/util/stream/ops/ref__;!!ACWV5N9M2RV99hQ!JJy6F9NoL6wKZQK5158up_fTRvH8X7F6JK8T7Euuf8vzbSQbr23eWa9S_yb61ksONVrLrdesCF_au5zyje2l$" id="OWA080e8c5b-35bc-94d6-7d9f-60a18ae54e23" class="OWAAutoLink" data-auth="NotApplicable" data-loopstyle="linkonly">
https://github.com/openjdk/jdk/tree/master/test/micro/org/openjdk/bench/java/util/stream/ops/ref</a></span></div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Initially I had Characteristics such as ORDERED etc on Gatherer but it just didn't end up worth it when looking at the bench results over a wide array of stream sizes and number of operations.</div>
</blockquote>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">I think there are 3 gatherer characteristics that make sense: KEEP_SORTED, KEEP_DISTINCT and KEEP_SIZED,</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">all of them say that if the stream was sorted/distinct/sized then the stream returned by a call to gather() is still sorted (with the same comparator), distinct
 or sized.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">As examples, map() is KEEP_SIZED, filter() is KEEP_SORTED | KEEP_DISTINCT and windowFixed is KEEP_DISTINCT.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">[CC Paul, so he can correct me if i'm saying something stupid]</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Now for the benchmarks, it depends what you want to measure, benchmarking streams is tricky. This is what i know about benchmarking streams.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">First, the JIT has two ways to profile types at runtime,</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Either a method takes a function as parameter</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">  void map(Function function) {</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">    function.apply(...)</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">  }</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">and when map is called with a subtype of Function, the JIT will propagate the exact type when map is inlined,</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Or a method use a field</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">  class Op {</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">    Function function;</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">    void map() {</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">       function.apply(...)</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">    }</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">  }</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">in that case, the VM records the profile of function.apply() and if there are more than two different profiles, the VM declare profile poluttion and do not try
 to optimize.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">The Stream implementation tries very hard to use only parameters instead of fields, that's why it does not use classical Iterator that are pull iterator (a filter
 iterator requires a field) but a Spliterator which is a push iterator, the element is sent as parameter of the consumer.That's also why collect does not use the builder pattern (that accumulate values in fields) but a Collector that publish the functions to
 be called as parameter.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Obvisously, this is more complex than that, a Collector stores the functions in fields so it should not work well but the implementation uses a record that plays
 well with escape analysis. Escape analysis see the fields of an instance as parameters so the functions of a Collector are correctly propagated (if the escape analysis works). And lambdas are using invokedynamic, and the VM tries really hard to inline invokedynamic,
 so lambdas (that captures value or not) are routinely fully inlined with the intermediate operation of a stream.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">In your tests, i've not seen comparaisons between an existing method like map() or filter() followed by a sorted()/distinct()/count()/toList(), i.e. operations
 where the characteristics KEEP_* have an impact and their equivalent using a Gatherer.</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="x_x_Signature">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
¡Ì</div>
</div>
</blockquote>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">regards,</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">R¨¦mi</span></div>
<div><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></div>
<blockquote style="margin-left: 5px; padding-left: 5px; border-left: 2px solid rgb(16, 16, 255);">
<div id="x_x_Signature">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<hr style="display: inline-block; width: 98%;">
<div id="x_x_divRplyFwdMsg" dir="ltr"><span style="font-family: Calibri, sans-serif; font-size: 11pt; color: rgb(0, 0, 0);"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Remi Forax <forax@univ-mlv.fr><br>
<b>Sent:</b> Wednesday, 17 January 2024 16:48<br>
<b>To:</b> core-libs-dev <core-libs-dev@openjdk.java.net><br>
<b>Subject:</b> Gatherer: spliterator characteristics are not propagated</span>
<div> </div>
</div>
<div><span style="font-family: Helvetica, Arial, sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">While doing some benchmarking of the Gatherer API, i've found that the characteristics of the spliterator was not propagated by the method Stream.gather() making
 the stream slower than it should.<br>
<br>
As an example, there is no way when reimplementing map() using a Gatherer to say that this intermediate operation keep the size, which is important if the terminal operation is toList() because if the size is known, toList() will presize the List and avoid
 the creation of an intermediary ArrayList.<br>
<br>
See <a href="https://urldefense.com/v3/__https://github.com/forax/we_are_all_to_gather/blob/master/src/main/java/com/gihtub/forax/wearealltogather/bench/MapGathererBenchmark.java__;!!ACWV5N9M2RV99hQ!JJy6F9NoL6wKZQK5158up_fTRvH8X7F6JK8T7Euuf8vzbSQbr23eWa9S_yb61ksONVrLrdesCF_auzwTY8aB$" id="OWA4df2922d-e2cf-d3f2-96d5-6a217e6e8f51" class="OWAAutoLink" data-auth="NotApplicable" data-loopstyle="linkonly">
https://github.com/forax/we_are_all_to_gather/blob/master/src/main/java/com/gihtub/forax/wearealltogather/bench/MapGathererBenchmark.java</a><br>
<br>
I think that adding a way to propagate the spliterator characteristics through a Gatherer would greatly improve the performance of commons streams (at least all the ones that end with a call to toList).<br>
<br>
I have some idea of how to do that, but I prefer first to hear if i've overlook something and if improving the performance is something worth changing the API.<br>
<br>
regards,<br>
R¨¦mi</span></div>
<span style="font-family: Helvetica, Arial, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></blockquote>
<span style="font-family: Helvetica, Arial, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><br>
</span></blockquote>
</body>
</html>