Fw: New candidate JEP: 485: Stream Gatherers

Viktor Klang viktor.klang at oracle.com
Tue Oct 15 11:46:34 UTC 2024


Hi Cay!

>AFAIK, the "greedy/short-circuiting" decision point doesn't have a major impact on performance either. Or am I wrong there?

One of my personal goals was to arrive at a point where the built-in intermediate Stream operations could be implemented as Gatherers with a performance around-the-same-or-better. Greedy allows for less signal tracking which in aggregate over several operations can have a noticeable advantage. Keep in mind that this is primarily around sequential performance, which in my experience is vastly more common than parallel streams.

>If I use the factory methods, I have to make a choice between of/ofGreedy

Back when GREEDY was a Characteristic, you'd still have the choice of adding that or not, and since it was served on the side, it'd be much more likely that developers wouldn't know that was a choice they could make, and wouldn't be connected to the Integrator itself. Making of() the short-name is because it is more capable than ofGreedy() (which does not support short-circuiting). So using it signals a choice of less capability.

>and ofSequential/of.

The reason for ofSequential/of instead of ofParallel/of is that a parallel-capable Gatherer covers more scenarios than a sequential-only versison, so giving the shorter name to the less-capable version would not stand out in the same way when reviewing, and Collector.of is parallel-capable, which seemed more consistent to make Gatherer.of parallel-capable as well.

>First off, I think factory methods should be the favored approach.

Is there something which could be made clearer around that, because that's how I already view it.

>I have seen some gatherer implementations that don't use the factory methods, even though they could have. Is this flexibility useful?

Yes, being able to move from an in-line definition, to a class-local implementation, to a dedicated file implementation provides a gradual path based on user needs and operation evolution. That's the same as for Collector.

>The details are fussy, with the marker interface and the magic default combiner.

I think this is worth expanding a bit on, in what way are they fussy?

>That's where I thought the characteristics approach is a better API. It has precedence, and it is unfussy.

See the archives for the discussion around why Characteristics (which were there for a long time) didn't make it.

>I realize it's not a big deal, and I was going to let it slide. Until I heard Brian's Devoxx talk where he mentioned "peak complexity", and I felt, that's, in a small way, what is present here.

I completely understand your line of thinking, and I'm not sure what'll convince you that we're quite far from the peak (you should have seen some of my prototypes! 😄). It is important to remember that there's a pretty vast space of use-cases which needs to be supported, so designing an abstraction which makes sense for a 2-element sequential stream as well as a 1m-element parallel stream, and everything in between had proven to be elusive ever since Streams were introduced in Java 8.

I think it is also worth noting that usage of Gatherers will by far outnumber definition of Gatherers, so appropriate emphasis needs to be placed on usage ergonomics, capabilities, and runtime performance.

Cheers,
√


Viktor Klang
Software Architect, Java Platform Group
Oracle

________________________________
From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Cay Horstmann <cay.horstmann at gmail.com>
Sent: Monday, 14 October 2024 21:27
To: core-libs-dev at openjdk.org <core-libs-dev at openjdk.org>
Subject: Re: Fw: New candidate JEP: 485: Stream Gatherers

Hi Viktor,

thanks for your clarifications.

I agree that from a performance point of view, there isn't all that much to be gained. I thought more about parallelizing distinctBy and windowSliding, Perhaps one can squeeze out a modest gain, but I am not excited by the potential.

AFAIK, the "greedy/short-circuiting" decision point doesn't have a major impact on performance either. Or am I wrong there?

In my mind, given that performance is not worth more than maybe a tweak, this amplifies my first issue with the surface API.

I started out thinking that almost nobody is going to write a gatherer, so why worry? But I found myself writing a couple in the last few days. And I wonder whether the current API is at "peak complexity".

If I use the factory methods, I have to make a choice between of/ofGreedy and ofSequential/of.

And if I don't use the factory methods, I have to mess with a marker interface or a method yielding a magic default.

Is there some virtuous collapse?

First off, I think factory methods should be the favored approach. And "of" should be the safe choice. That's why I would rename ofSequential into of, and introduce ofParallel for optimization. Like with of/ofGreedy.

I have seen some gatherer implementations that don't use the factory methods, even though they could have. Is this flexibility useful? The details are fussy, with the marker interface and the magic default combiner. That's where I thought the characteristics approach is a better API. It has precedence, and it is unfussy.

I realize it's not a big deal, and I was going to let it slide. Until I heard Brian's Devoxx talk where he mentioned "peak complexity", and I felt, that's, in a small way, what is present here.

Cheers,

Cay

--

(Moving this to core-libs-dev)

Cay,

Regarding 1, Characteristics was a part of the Gatherers-contract for a very long time, alas it didn't end up worth its cost. There's a longer discussion on the topic here: https://mail.openjdk.org/pipermail/core-libs-dev/2024-January/118138.html (and I'm sure that there were more, but that's the one which comes to mind)

Regarding 2, I did have a prototype which had a Downstream in the Combiner, but created a new dimension of complexity which made it even harder for people to move from sequential to parallelizable. The door isn't closed on it, but I remain unconvinced it's worth the surface area for performance reasons.

As a bit of a side-note, it's worth knowing that in the reference stream implementation, it is not unusual that parallel-capable stages are executed as "islands" which means that short-circuiting signals cannot travel across those islands. Since parallel-capable Gatherers can be fused to execute in the same "island" if we get to a place where "all" intermediate operations are parallel-capable Gatherers, there'd be a single end-to-end "island" and hence the ability to propagate the short-circuiting would be preserved in all modes of execution. Also worth knowing that a `gather(…)` immediately followed by a `collect(…)` can also be fused to run together.

Cheers,
√


Viktor Klang
Software Architect, Java Platform Group
Oracle

________________________________
From: jdk-dev <jdk-dev-retn at openjdk.org> on behalf of Cay Horstmann <cay.horstmann at gmail.com>
Sent: Friday, 4 October 2024 19:58
To: jdk-dev at openjdk.org <jdk-dev at openjdk.org>
Subject: Re: New candidate JEP: 485: Stream Gatherers

Hi, I have some belated questions about the design choices in this API that I could not find addressed in the JEP.

1. Why aren't characteristics used to express "greediness/short-circuiting" or "sequentialness/parallelizability"?

I understand that for the former I use ofGreedy/of, or implement Gatherers.Integrator.Greedy/Gatherers.Integrator. And for the latter, I use ofSequential/of, or, if I implement the Gatherer interface, have the combiner return defaultCombiner() or not.

But it seems a bit complex and less familiar than the characteristics mechanism that exists for spliterators, streams, and collectors.

The original design document (https://cr.openjdk.org/~vklang/Gatherers.html) used characteristics, so I wonder what motivated the change.

2. Why wasn't the combiner() designed to allow pushing of elements to the end of the first range's sink? Then distinctBy could be parallelized without buffering the elements. More generally, with some state fiddling one can then handle the elements around range splits.

As it is, I don't see how to parallelize such computations other than to buffer all elements.

I looked at the project at https://github.com/jhspetersson/packrat that implements a number of gatherers. Only one uses a combiner, to join buffers until their contents can be pushed in the finisher.

Cheers,

Cay
--

Cay S. Horstmann | https://horstmann.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20241015/b50bade2/attachment-0001.htm>


More information about the core-libs-dev mailing list