Stream.findFirst for unordered source optimization

Paul Sandoz paul.sandoz at oracle.com
Fri Jan 22 11:56:25 UTC 2016


Hi Tagir,

Thanks for looking at this.

I would prefer if a boolean was passed into the task constructor e.g.:

@Override
public <P_IN> O evaluateParallel(PipelineHelper<T> helper,
                                 Spliterator<P_IN> spliterator) {
    // This takes into account the upstream ops flags and the terminal
    // op flags and therefore takes into account findFirst or findAny
    boolean mustFindFirst = StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags());
    return new FindTask<>(this, mustFindFirst, helper, spliterator).invoke();
}

so we keep the flag evaluation logic within the evaluate method.

Then change FindOp.mustFindFirst to be an int of the opFlags calculated in the constructor:

  this.opFlags = StreamOpFlag.IS_SHORT_CIRCUIT | (mustFindFirst ? 0 : StreamOpFlag.NOT_ORDERED);

Paul.

> On 22 Jan 2016, at 07:19, Tagir F. Valeev <amaembo at gmail.com> wrote:
> 
> Hello!
> 
> Seems that currently Stream.findFirst is not optimized for unordered
> source. I think it should work as findAny in this case. Here's a small
> patch which fixes this:
> 
> http://cr.openjdk.java.net/~tvaleev/patches/findFirst/find_patch.txt
> 
> Simple JMH test:
> http://cr.openjdk.java.net/~tvaleev/patches/findFirst/FindTest.java
> 
> Original:
> http://cr.openjdk.java.net/~tvaleev/patches/findFirst/jmh_out_orig.txt
> # JMH 1.11.2 (released 85 days ago)
> # VM version: JDK 9-ea, VM 9-ea+99-2015-12-23-183325.javare.4146.nc
> Benchmark                    Mode  Cnt      Score      Error  Units
> FindTest.findAny             avgt   30  12439,631 ± 1787,866  us/op
> FindTest.findAnyUnordered    avgt   30  12923,080 ± 1072,537  us/op
> FindTest.findFirst           avgt   30  48047,467 ± 2713,489  us/op
> FindTest.findFirstUnordered  avgt   30  52648,893 ± 3934,682  us/op
> 
> Patched:
> http://cr.openjdk.java.net/~tvaleev/patches/findFirst/jmh_out_patched.txt
> Benchmark                    Mode  Cnt      Score      Error  Units
> FindTest.findAny             avgt   30  11312,238 ±  386,627  us/op
> FindTest.findAnyUnordered    avgt   30  12136,953 ± 1536,817  us/op
> FindTest.findFirst           avgt   30  47517,776 ± 2844,607  us/op
> FindTest.findFirstUnordered  avgt   30  13147,492 ± 1140,592  us/op
> 
> If you think it's a reasonable thing to patch, I can log an issue,
> generate webrev and check whether jtreg tests pass.
> 
> With best regards,
> Tagir Valeev.
> 




More information about the core-libs-dev mailing list