RFR: 8153293 - Stream API: Preserve SORTED and DISTINCT characteristics for boxed() and asLongStream() operations

Tagir F. Valeev amaembo at gmail.com
Tue Apr 12 16:37:35 UTC 2016


Hello!

Thank you, Stefan and Paul for review. Here's updated webrev:

http://cr.openjdk.java.net/~tvaleev/webrev/8153293/r2/

Changes:
- all new mapToObj are private and not final
- IntStream.asDoubleStream preserves distinct now
- Tests fixed according to Stefan's suggestions
- Additional tests added which test how sorted and distinct actually
  work, as Paul suggests. Values close to Integer.MAX_VALUE and Long.MAX_VALUE
  are tested. TreeSet<Long> and TreeSet<Double> is used to produce
  expected result.

With best regards,
Tagir Valeev.

SZ> Hi Tagir,

SZ> another minor issue. The testFlags() methods in IntPrimitiveOpsTests
SZ>  / LongPrimitiveOpsTests each have a duplicated assert:


SZ> IntPrimitiveOpsTests:

SZ> assertFalse(IntStreams.of(1, 10).boxed().spliterator()
SZ>               .hasCharacteristics(Spliterator.SORTED));


SZ> LongPrimitiveOpsTests:

SZ> assertFalse(LongStreams.of(1, 10).boxed().spliterator()
SZ>               .hasCharacteristics(Spliterator.SORTED));


SZ> The asserts for IntStreams.range(1, 10).asDoubleStream() would have
SZ> to be changed to account for DISTINCTness, of course.

SZ> Regards,
SZ> Stefan


SZ> 2016-04-01 18:25 GMT+02:00 Tagir F. Valeev <amaembo at gmail.com>:
>> Hello!
>>
>> Please review and sponsor the following patch:
>> http://cr.openjdk.java.net/~tvaleev/webrev/8153293/r1/
>>
>> The patch preserves more characteristics on primitive stream
>> operations:
>> IntStream/LongStream/DoubleStream.boxed() preserves SORTED and DISTINCT
>> IntStream.asLongStream() preserves SORTED and DISTINCT
>> IntStream.asDoubleStream() and LongStream.asDoubleStream() preserves SORTED
>> (different longs can be converted into the same double, so DISTINCT is
>> not preserved here; not sure whether this is possible for ints)
>>
>> Fixing the boxed() case is especially important as distinct() for
>> primitive streams is implemented like boxed().distinct().unbox, so the
>> actual distinct() operation cannot take the advantage of DISTINCT flag
>> (skip the operation at all) or SORTED flag (switch to more efficient
>> implementation).
>>
>> Here's the small JMH benchmark which measures the performance boost of
>> quite common operation: sort the input numbers and leave only distinct
>> ones:
>> http://cr.openjdk.java.net/~tvaleev/webrev/8153293/jmh/
>>
>> new Random(1).ints(size).sorted().distinct().toArray()
>>
>> I've got the following results.
>>
>> 9ea+111:
>>
>> Benchmark                        (size)  Mode  Cnt      Score      Error  Units
>> SortDistinctTest.sortedDistinct      10  avgt   30      0,612 ±    0,004  us/op
>> SortDistinctTest.sortedDistinct    1000  avgt   30     92,848 ±    1,039  us/op
>> SortDistinctTest.sortedDistinct  100000  avgt   30  32147,205 ± 3487,422  us/op
>>
>> 9ea+111 patched:
>>
>> Benchmark                        (size)  Mode  Cnt     Score    Error  Units
>> SortDistinctTest.sortedDistinct      10  avgt   30     0,435 ±  0,001  us/op
>> SortDistinctTest.sortedDistinct    1000  avgt   30    40,555 ±  0,772  us/op
>> SortDistinctTest.sortedDistinct  100000  avgt   30  9031,651 ± 73,956  us/op
>>
>> With best regards,
>> Tagir Valeev.
>>




More information about the core-libs-dev mailing list