Skip to content

Commit e77ca87

Browse files
committed
Adding benchmarking: made generators for collections.
Also retrofitted stream converters so you can use them generically.
1 parent 86370b0 commit e77ca87

File tree

5 files changed

+111
-11
lines changed

5 files changed

+111
-11
lines changed

benchmark/build.sbt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
enablePlugins(JmhPlugin)
2+
3+
lazy val root = (project in file(".")).settings(
4+
name := "java8-compat-bench",
5+
scalaVersion := "2.11.7",
6+
crossScalaVersions := List("2.11.7" /* TODO, "2.12.0-M3"*/),
7+
organization := "org.scala-lang.modules",
8+
version := "0.6.0-SNAPSHOT",
9+
unmanagedJars in Compile ++= Seq(baseDirectory.value / "../target/scala-2.11/scala-java8-compat_2.11-0.6.0-SNAPSHOT.jar")
10+
)

benchmark/project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.4")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package bench
2+
3+
import java.util.stream._
4+
5+
import scala.collection.generic.CanBuildFrom
6+
import scala.compat.java8.StreamConverters._
7+
import scala.compat.java8.collectionImpl._
8+
import scala.compat.java8.converterImpl._
9+
10+
package object generate {
11+
private def myInty(n: Int) = 0 until n
12+
private def myStringy(n: Int) = myInty(n).map(i => (i*i).toString)
13+
14+
object Coll {
15+
def i[CC[_]](n: Int)(implicit cbf: CanBuildFrom[Nothing, Int, CC[Int]]): CC[Int] = {
16+
val b = cbf();
17+
myInty(n).foreach(b += _)
18+
b.result()
19+
}
20+
def s[CC[_]](n: Int)(implicit cbf: CanBuildFrom[Nothing, String, CC[String]]): CC[String] = {
21+
val b = cbf();
22+
myStringy(n).foreach(b += _)
23+
b.result()
24+
}
25+
}
26+
27+
object Pstep {
28+
def i[CC](cc: CC)(implicit steppize: CC => MakesIntStepper): IntStepper =
29+
steppize(cc).stepper
30+
def s[CC](cc: CC)(implicit steppize: CC => MakesAnyStepper[String]): AnyStepper[String] =
31+
steppize(cc).stepper
32+
}
33+
34+
object Sstep {
35+
def i[CC](cc: CC)(implicit steppize: CC => MakesIntSeqStepper): IntStepper =
36+
steppize(cc).stepper
37+
def s[CC](cc: CC)(implicit steppize: CC => MakesAnySeqStepper[String]): AnyStepper[String] =
38+
steppize(cc).stepper
39+
}
40+
41+
object PsStream {
42+
def i[CC](cc: CC)(implicit steppize: CC => MakesIntStepper): IntStream =
43+
steppize(cc).stepper.parStream
44+
def s[CC](cc: CC)(implicit steppize: CC => MakesAnyStepper[String]): Stream[String] =
45+
steppize(cc).stepper.parStream
46+
}
47+
48+
object SsStream {
49+
def i[CC](cc: CC)(implicit steppize: CC => MakesIntSeqStepper): IntStream =
50+
steppize(cc).stepper.seqStream
51+
def s[CC](cc: CC)(implicit steppize: CC => MakesAnySeqStepper[String]): Stream[String] =
52+
steppize(cc).stepper.seqStream
53+
}
54+
55+
object Sstream {
56+
def i[CC](cc: CC)(implicit streamize: CC => MakesSequentialStream[java.lang.Integer, IntStream]) =
57+
streamize(cc).seqStream
58+
def s[CC](cc: CC)(implicit streamize: CC => MakesSequentialStream[String, Stream[String]]) =
59+
streamize(cc).seqStream
60+
}
61+
62+
object Pstream {
63+
def i[CC](cc: CC)(implicit streamize: CC => MakesParallelStream[java.lang.Integer, IntStream]) =
64+
streamize(cc).parStream
65+
def s[CC](cc: CC)(implicit streamize: CC => MakesParallelStream[String, Stream[String]]) =
66+
streamize(cc).parStream
67+
}
68+
}

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ trait PrimitiveStreamUnboxer[A, S] {
1616

1717
trait Priority5StreamConverters {
1818
// Note--conversion is only to make sure implicit conversion priority is lower than alternatives.
19-
implicit class EnrichScalaCollectionWithSeqStream[A, CC](cc: CC)(implicit steppize: CC => MakesAnySeqStepper[A]) {
19+
implicit class EnrichScalaCollectionWithSeqStream[A, CC](cc: CC)(implicit steppize: CC => MakesAnySeqStepper[A])
20+
extends MakesSequentialStream[A, Stream[A]] {
2021
def seqStream: Stream[A] = StreamSupport.stream(steppize(cc).stepper, false)
2122
}
2223
implicit class EnrichScalaCollectionWithKeySeqStream[K, CC](cc: CC)(implicit steppize: CC => MakesAnyKeySeqStepper[K]) {
@@ -28,13 +29,16 @@ trait Priority5StreamConverters {
2829
}
2930

3031
trait Priority4StreamConverters extends Priority5StreamConverters {
31-
implicit class EnrichScalaCollectionWithSeqDoubleStream[CC](cc: CC)(implicit steppize: CC => MakesDoubleSeqStepper) {
32+
implicit class EnrichScalaCollectionWithSeqDoubleStream[CC](cc: CC)(implicit steppize: CC => MakesDoubleSeqStepper)
33+
extends MakesSequentialStream[java.lang.Double, DoubleStream] {
3234
def seqStream: DoubleStream = StreamSupport.doubleStream(steppize(cc).stepper, false)
3335
}
34-
implicit class EnrichScalaCollectionWithSeqIntStream[CC](cc: CC)(implicit steppize: CC => MakesIntSeqStepper) {
36+
implicit class EnrichScalaCollectionWithSeqIntStream[CC](cc: CC)(implicit steppize: CC => MakesIntSeqStepper)
37+
extends MakesSequentialStream[java.lang.Integer, IntStream] {
3538
def seqStream: IntStream = StreamSupport.intStream(steppize(cc).stepper, false)
3639
}
37-
implicit class EnrichScalaCollectionWithSeqLongStream[CC](cc: CC)(implicit steppize: CC => MakesLongSeqStepper) {
40+
implicit class EnrichScalaCollectionWithSeqLongStream[CC](cc: CC)(implicit steppize: CC => MakesLongSeqStepper)
41+
extends MakesSequentialStream[java.lang.Long, LongStream] {
3842
def seqStream: LongStream = StreamSupport.longStream(steppize(cc).stepper, false)
3943
}
4044
implicit class EnrichScalaCollectionWithSeqDoubleKeyStream[CC](cc: CC)(implicit steppize: CC => MakesDoubleKeySeqStepper) {
@@ -58,7 +62,8 @@ trait Priority4StreamConverters extends Priority5StreamConverters {
5862
}
5963

6064
trait Priority3StreamConverters extends Priority4StreamConverters {
61-
implicit class EnrichAnySteppableWithStream[A, CC](cc: CC)(implicit steppize: CC => MakesAnyStepper[A]) {
65+
implicit class EnrichAnySteppableWithStream[A, CC](cc: CC)(implicit steppize: CC => MakesAnyStepper[A])
66+
extends MakesSequentialStream[A, Stream[A]] with MakesParallelStream[A, Stream[A]] {
6267
def seqStream: Stream[A] = StreamSupport.stream(steppize(cc).stepper, false)
6368
def parStream: Stream[A] = StreamSupport.stream(steppize(cc).stepper.anticipateParallelism, true)
6469
}
@@ -73,7 +78,8 @@ trait Priority3StreamConverters extends Priority4StreamConverters {
7378
}
7479

7580
trait Priority2StreamConverters extends Priority3StreamConverters {
76-
implicit class EnrichDoubleSteppableWithStream[CC](cc: CC)(implicit steppize: CC => MakesDoubleStepper) {
81+
implicit class EnrichDoubleSteppableWithStream[CC](cc: CC)(implicit steppize: CC => MakesDoubleStepper)
82+
extends MakesSequentialStream[java.lang.Double, DoubleStream] with MakesParallelStream[java.lang.Double, DoubleStream] {
7783
def seqStream: DoubleStream = StreamSupport.doubleStream(steppize(cc).stepper, false)
7884
def parStream: DoubleStream = StreamSupport.doubleStream(steppize(cc).stepper.anticipateParallelism, true)
7985
}
@@ -85,7 +91,8 @@ trait Priority2StreamConverters extends Priority3StreamConverters {
8591
def seqValueStream: DoubleStream = StreamSupport.doubleStream(steppize(cc).valueStepper, false)
8692
def parValueStream: DoubleStream = StreamSupport.doubleStream(steppize(cc).valueStepper.anticipateParallelism, true)
8793
}
88-
implicit class EnrichIntSteppableWithStream[CC](cc: CC)(implicit steppize: CC => MakesIntStepper) {
94+
implicit class EnrichIntSteppableWithStream[CC](cc: CC)(implicit steppize: CC => MakesIntStepper)
95+
extends MakesSequentialStream[java.lang.Integer, IntStream] with MakesParallelStream[java.lang.Integer, IntStream] {
8996
def seqStream: IntStream = StreamSupport.intStream(steppize(cc).stepper, false)
9097
def parStream: IntStream = StreamSupport.intStream(steppize(cc).stepper.anticipateParallelism, true)
9198
}
@@ -97,7 +104,8 @@ trait Priority2StreamConverters extends Priority3StreamConverters {
97104
def seqValueStream: IntStream = StreamSupport.intStream(steppize(cc).valueStepper, false)
98105
def parValueStream: IntStream = StreamSupport.intStream(steppize(cc).valueStepper.anticipateParallelism, true)
99106
}
100-
implicit class EnrichLongSteppableWithStream[CC](cc: CC)(implicit steppize: CC => MakesLongStepper) {
107+
implicit class EnrichLongSteppableWithStream[CC](cc: CC)(implicit steppize: CC => MakesLongStepper)
108+
extends MakesSequentialStream[java.lang.Long, LongStream] with MakesParallelStream[java.lang.Long, LongStream] {
101109
def seqStream: LongStream = StreamSupport.longStream(steppize(cc).stepper, false)
102110
def parStream: LongStream = StreamSupport.longStream(steppize(cc).stepper.anticipateParallelism, true)
103111
}
@@ -200,17 +208,20 @@ extends Priority1StreamConverters
200208
with converterImpl.Priority1StepConverters
201209
with converterImpl.Priority1AccumulatorConverters
202210
{
203-
implicit class EnrichDoubleArrayWithStream(a: Array[Double]) {
211+
implicit class EnrichDoubleArrayWithStream(a: Array[Double])
212+
extends MakesSequentialStream[java.lang.Double, DoubleStream] with MakesParallelStream[java.lang.Double, DoubleStream] {
204213
def seqStream: DoubleStream = java.util.Arrays.stream(a)
205214
def parStream: DoubleStream = seqStream.parallel
206215
}
207216

208-
implicit class EnrichIntArrayWithStream(a: Array[Int]) {
217+
implicit class EnrichIntArrayWithStream(a: Array[Int])
218+
extends MakesSequentialStream[java.lang.Integer, IntStream] with MakesParallelStream[java.lang.Integer, IntStream] {
209219
def seqStream: IntStream = java.util.Arrays.stream(a)
210220
def parStream: IntStream = seqStream.parallel
211221
}
212222

213-
implicit class EnrichLongArrayWithStream(a: Array[Long]) {
223+
implicit class EnrichLongArrayWithStream(a: Array[Long])
224+
extends MakesSequentialStream[java.lang.Long, LongStream] with MakesParallelStream[java.lang.Long, LongStream] {
214225
def seqStream: LongStream = java.util.Arrays.stream(a)
215226
def parStream: LongStream = seqStream.parallel
216227
}

src/main/scala/scala/compat/java8/converterImpl/MakesSteppers.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import language.implicitConversions
55
import scala.compat.java8.collectionImpl._
66
import scala.compat.java8.runtime._
77

8+
/** Classes or objects implementing this trait create streams suitable for sequential use */
9+
trait MakesSequentialStream[A, SS <: java.util.stream.BaseStream[A, SS]] extends Any {
10+
def seqStream: SS
11+
}
12+
13+
/** Classes or objects implementing this trait create streams suitable for parallel use */
14+
trait MakesParallelStream[A, SS <: java.util.stream.BaseStream[A, SS]] extends Any {
15+
def parStream: SS
16+
}
17+
818
/** Classes or objects implementing this trait create generic steppers suitable for sequential use. */
919
trait MakesAnySeqStepper[A] extends Any {
1020
/** Generates a fresh stepper over `A`s suitable for sequential use */

0 commit comments

Comments
 (0)