Skip to content

Commit c022190

Browse files
committed
Special-case primitive WrappedArray types when creating Streams
This was already done for `Array` in the same way. Streams produced by `Arrays.stream` can be faster than going through a `Stepper`.
1 parent 9dd46f3 commit c022190

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/main/scala/scala/compat/java8/StreamConverters.scala

+18
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,24 @@ with converterImpl.Priority1AccumulatorConverters
292292
def parStream: LongStream = seqStream.parallel
293293
}
294294

295+
implicit final class EnrichDoubleWrappedArrayWithStream(private val a: collection.mutable.WrappedArray[Double])
296+
extends AnyVal with MakesSequentialStream[java.lang.Double, DoubleStream] with MakesParallelStream[java.lang.Double, DoubleStream] {
297+
def seqStream: DoubleStream = java.util.Arrays.stream(a.array)
298+
def parStream: DoubleStream = seqStream.parallel
299+
}
300+
301+
implicit final class EnrichIntWrappedArrayWithStream(private val a: collection.mutable.WrappedArray[Int])
302+
extends AnyVal with MakesSequentialStream[java.lang.Integer, IntStream] with MakesParallelStream[java.lang.Integer, IntStream] {
303+
def seqStream: IntStream = java.util.Arrays.stream(a.array)
304+
def parStream: IntStream = seqStream.parallel
305+
}
306+
307+
implicit final class EnrichLongWrappedArrayWithStream(private val a: collection.mutable.WrappedArray[Long])
308+
extends AnyVal with MakesSequentialStream[java.lang.Long, LongStream] with MakesParallelStream[java.lang.Long, LongStream] {
309+
def seqStream: LongStream = java.util.Arrays.stream(a.array)
310+
def parStream: LongStream = seqStream.parallel
311+
}
312+
295313
implicit val primitiveAccumulateDoubleStream = new PrimitiveStreamAccumulator[Stream[Double], DoubleAccumulator] {
296314
def streamAccumulate(stream: Stream[Double]): DoubleAccumulator =
297315
stream.collect(DoubleAccumulator.supplier, DoubleAccumulator.boxedAdder, DoubleAccumulator.merger)

src/test/scala/scala/compat/java8/StreamConvertersTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class StreamConvertersTest {
255255
def streamMaterialization(): Unit = {
256256
val coll = collection.mutable.WrappedArray.make[Int](Array(1,2,3))
257257
val streamize = implicitly[collection.mutable.WrappedArray[Int] => MakesSequentialStream[java.lang.Integer, IntStream]]
258-
assertTrue(streamize(coll).getClass.getName.contains("EnrichScalaCollectionWithSeqIntStream"))
258+
assertTrue(streamize(coll).getClass.getName.contains("EnrichIntWrappedArrayWithStream"))
259259
val steppize = implicitly[collection.mutable.WrappedArray[Int] => MakesStepper[IntStepper]]
260260
assertTrue(steppize(coll).getClass.getName.contains("RichArrayIntCanStep"))
261261
}

0 commit comments

Comments
 (0)