Gatherer: spliterator characteristics are not propagated
forax at univ-mlv.fr
forax at univ-mlv.fr
Thu Jan 18 09:28:27 UTC 2024
> From: "Viktor Klang" <viktor.klang at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>, "core-libs-dev"
> <core-libs-dev at openjdk.java.net>
> Sent: Wednesday, January 17, 2024 8:48:07 PM
> Subject: Re: Gatherer: spliterator characteristics are not propagated
> Hi Rémi,
> You can find some of my benches here: [
> https://github.com/openjdk/jdk/tree/master/test/micro/org/openjdk/bench/java/util/stream/ops/ref
> |
> https://github.com/openjdk/jdk/tree/master/test/micro/org/openjdk/bench/java/util/stream/ops/ref
> ]
> 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.
I think there are 3 gatherer characteristics that make sense: KEEP_SORTED, KEEP_DISTINCT and KEEP_SIZED,
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.
As examples, map() is KEEP_SIZED, filter() is KEEP_SORTED | KEEP_DISTINCT and windowFixed is KEEP_DISTINCT.
[CC Paul, so he can correct me if i'm saying something stupid]
Now for the benchmarks, it depends what you want to measure, benchmarking streams is tricky. This is what i know about benchmarking streams.
First, the JIT has two ways to profile types at runtime,
Either a method takes a function as parameter
void map(Function function) {
function.apply(...)
}
and when map is called with a subtype of Function, the JIT will propagate the exact type when map is inlined,
Or a method use a field
class Op {
Function function;
void map() {
function.apply(...)
}
}
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.
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.
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.
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.
> Cheers,
> √
regards,
Rémi
> Viktor Klang
> Software Architect, Java Platform Group
> Oracle
> From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Remi Forax
> <forax at univ-mlv.fr>
> Sent: Wednesday, 17 January 2024 16:48
> To: core-libs-dev <core-libs-dev at openjdk.java.net>
> Subject: Gatherer: spliterator characteristics are not propagated
> 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.
> 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.
> See [
> https://github.com/forax/we_are_all_to_gather/blob/master/src/main/java/com/gihtub/forax/wearealltogather/bench/MapGathererBenchmark.java
> |
> https://github.com/forax/we_are_all_to_gather/blob/master/src/main/java/com/gihtub/forax/wearealltogather/bench/MapGathererBenchmark.java
> ]
> 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).
> 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.
> regards,
> Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20240118/33a59f4b/attachment-0001.htm>
More information about the core-libs-dev
mailing list