Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added alternative compile.to syntax, providing more efficient and uniform collection building #1659

Merged
merged 5 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ language: scala

scala:
- 2.11.12
- 2.13.0
- 2.12.9
- 2.13.0

jdk:
- openjdk11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class FreeCBenchmark {
case Result.Pure(r) => F.pure(Some(r))
case Result.Fail(e) => F.raiseError(e)
case Result.Interrupted(_, err) => err.fold[F[Option[R]]](F.pure(None)) { F.raiseError }
case v @ ViewL.View(_) => F.raiseError(new RuntimeException("Never get here)"))
case _ @ViewL.View(_) => F.raiseError(new RuntimeException("Never get here)"))
}

}
24 changes: 10 additions & 14 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,17 @@ lazy val commonSettings = Seq(
"-language:implicitConversions",
"-language:higherKinds"
) ++
(if (scalaBinaryVersion.value.startsWith("2.12"))
List(
"-Xlint",
"-Xfatal-warnings",
"-Yno-adapted-args",
"-Ywarn-value-discard",
"-Ywarn-unused-import",
"-Ypartial-unification"
)
else Nil) ++ (if (scalaBinaryVersion.value.startsWith("2.11"))
List("-Xexperimental", "-Ypartial-unification")
else
Nil), // 2.11 needs -Xexperimental to enable SAM conversion
(scalaBinaryVersion.value match {
case v if v.startsWith("2.13") =>
List("-Xlint", "-Ywarn-unused")
case v if v.startsWith("2.12") =>
List("-Ypartial-unification")
case v if v.startsWith("2.11") =>
List("-Xexperimental", "-Ypartial-unification")
case other => sys.error(s"Unsupported scala version: $other")
}),
scalacOptions in (Compile, console) ~= {
_.filterNot("-Ywarn-unused-import" == _)
_.filterNot("-Ywarn-unused" == _)
.filterNot("-Xlint" == _)
.filterNot("-Xfatal-warnings" == _)
},
Expand Down
2 changes: 1 addition & 1 deletion core/jvm/src/main/scala/fs2/internal/ThreadFactories.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private[fs2] object ThreadFactories {
if (exitJvmOnFatalError) {
e match {
case NonFatal(_) => ()
case fatal => System.exit(-1)
case _ => System.exit(-1)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/jvm/src/test/scala/fs2/CompressSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class CompressSpec extends Fs2Spec {
val bytes = s
.through(compress.gzip(1024))
.compile
.to[Array]
.to(Array)

val bis = new ByteArrayInputStream(bytes)
val gzis = new GZIPInputStream(bis)
Expand Down
2 changes: 1 addition & 1 deletion core/jvm/src/test/scala/fs2/MemorySanityChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object TopicContinuousPublishSanityTest extends App {
}

object ResourceTrackerSanityTest extends App {
val big = Stream.constant(1).flatMap { n =>
val big = Stream.constant(1).flatMap { _ =>
Stream.bracket(IO(()))(_ => IO(())).flatMap(_ => Stream.emits(List(1, 2, 3)))
}
big.compile.drain.unsafeRunSync()
Expand Down
43 changes: 43 additions & 0 deletions core/shared/src/main/scala-2.12-/fs2/CollectorPlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fs2

import scala.collection.generic.{GenericTraversableTemplate, MapFactory, TraversableFactory}
import scala.collection.{MapLike, Traversable}

import fs2.internal._

private[fs2] trait CollectorPlatform { self: Collector.type =>
implicit def supportsFactory[A, C[_], B](
f: Factory[A, C[B]]
): Collector.Aux[A, C[B]] = make(Builder.fromFactory(f))

implicit def supportsTraversableFactory[A, C[x] <: Traversable[x] with GenericTraversableTemplate[
x,
C
]](
f: TraversableFactory[C]
): Collector.Aux[A, C[A]] = make(Builder.fromTraversableFactory(f))

implicit def supportsMapFactory[K, V, C[a, b] <: collection.Map[a, b] with MapLike[
a,
b,
C[a, b]
]](
f: MapFactory[C]
): Collector.Aux[(K, V), C[K, V]] =
make(Builder.fromMapFactory(f))

private[fs2] trait BuilderPlatform { self: Collector.Builder.type =>
def fromFactory[A, C[_], B](f: Factory[A, C[B]]): Builder[A, C[B]] =
fromBuilder(f())

def fromTraversableFactory[A, C[x] <: Traversable[x] with GenericTraversableTemplate[x, C]](
f: TraversableFactory[C]
): Builder[A, C[A]] =
fromBuilder(f.newBuilder[A])

def fromMapFactory[K, V, C[a, b] <: collection.Map[a, b] with MapLike[a, b, C[a, b]]](
f: MapFactory[C]
): Builder[(K, V), C[K, V]] =
fromBuilder(f.newBuilder)
}
}
28 changes: 28 additions & 0 deletions core/shared/src/main/scala-2.13+/fs2/CollectorPlatform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fs2

import scala.collection.{Factory, IterableFactory, MapFactory}

import fs2.internal.{Resource => _, _}

private[fs2] trait CollectorPlatform { self: Collector.type =>
implicit def supportsFactory[A, C[_], B](
f: Factory[A, C[B]]
): Collector.Aux[A, C[B]] = make(Builder.fromFactory(f))

implicit def supportsIterableFactory[A, C[_]](f: IterableFactory[C]): Collector.Aux[A, C[A]] =
make(Builder.fromIterableFactory(f))

implicit def supportsMapFactory[K, V, C[_, _]](f: MapFactory[C]): Collector.Aux[(K, V), C[K, V]] =
make(Builder.fromMapFactory(f))

private[fs2] trait BuilderPlatform { self: Collector.Builder.type =>
def fromFactory[A, C[_], B](f: Factory[A, C[B]]): Builder[A, C[B]] =
fromBuilder(f.newBuilder)

def fromIterableFactory[A, C[_]](f: IterableFactory[C]): Builder[A, C[A]] =
fromBuilder(f.newBuilder)

def fromMapFactory[K, V, C[_, _]](f: MapFactory[C]): Builder[(K, V), C[K, V]] =
fromBuilder(f.newBuilder)
}
}
62 changes: 52 additions & 10 deletions core/shared/src/main/scala/fs2/Chunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ abstract class Chunk[+O] extends Serializable { self =>
arr
}

/** Returns the elements of this chunk as an array, avoiding a copy if possible. */
protected[fs2] def toArrayUnsafe[O2 >: O: ClassTag]: Array[O2] = toArray

/**
* Converts this chunk to a `Chunk.Booleans`, allowing access to the underlying array of elements.
* If this chunk is already backed by an unboxed array of booleans, this method runs in constant time.
Expand All @@ -257,7 +260,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Booleans => c
case other =>
case _ =>
Chunk.Booleans(this.asInstanceOf[Chunk[Boolean]].toArray, 0, size)
}
}
Expand All @@ -271,7 +274,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Bytes => c
case other => Chunk.Bytes(this.asInstanceOf[Chunk[Byte]].toArray, 0, size)
case _ => Chunk.Bytes(this.asInstanceOf[Chunk[Byte]].toArray, 0, size)
}
}

Expand All @@ -289,7 +292,7 @@ abstract class Chunk[+O] extends Serializable { self =>
(b: JBuffer).limit(c.offset.toInt + c.size)
b
}
case other =>
case _ =>
JByteBuffer.wrap(this.asInstanceOf[Chunk[Byte]].toArray, 0, size)
}
}
Expand All @@ -303,7 +306,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Shorts => c
case other =>
case _ =>
Chunk.Shorts(this.asInstanceOf[Chunk[Short]].toArray, 0, size)
}
}
Expand All @@ -317,7 +320,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Ints => c
case other => Chunk.Ints(this.asInstanceOf[Chunk[Int]].toArray, 0, size)
case _ => Chunk.Ints(this.asInstanceOf[Chunk[Int]].toArray, 0, size)
}
}

Expand All @@ -330,7 +333,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Longs => c
case other => Chunk.Longs(this.asInstanceOf[Chunk[Long]].toArray, 0, size)
case _ => Chunk.Longs(this.asInstanceOf[Chunk[Long]].toArray, 0, size)
}
}

Expand All @@ -343,7 +346,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Floats => c
case other =>
case _ =>
Chunk.Floats(this.asInstanceOf[Chunk[Float]].toArray, 0, size)
}
}
Expand All @@ -357,7 +360,7 @@ abstract class Chunk[+O] extends Serializable { self =>
val _ = ev // Convince scalac that ev is used
this match {
case c: Chunk.Doubles => c
case other =>
case _ =>
Chunk.Doubles(this.asInstanceOf[Chunk[Double]].toArray, 0, size)
}
}
Expand Down Expand Up @@ -475,7 +478,7 @@ abstract class Chunk[+O] extends Serializable { self =>
iterator.mkString("Chunk(", ", ", ")")
}

object Chunk {
object Chunk extends CollectorK[Chunk] {

/** Optional mix-in that provides the class tag of the element type in a chunk. */
trait KnownElementType[A] { self: Chunk[A] =>
Expand Down Expand Up @@ -646,7 +649,7 @@ object Chunk {
values.size match {
case 0 => empty
case 1 => singleton(values(0))
case n =>
case _ =>
values match {
case a: Array[Boolean] => booleans(a)
case a: Array[Byte] => bytes(a)
Expand Down Expand Up @@ -678,6 +681,10 @@ object Chunk {
def size = length
def apply(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: O: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: O](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[AnyRef]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -721,6 +728,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Boolean: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Boolean](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Boolean]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -763,6 +774,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Byte: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Byte](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Byte]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -1127,6 +1142,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Short: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Short](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Short]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -1168,6 +1187,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Int: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Int](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Int]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -1209,6 +1232,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Long: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Long](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Long]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -1251,6 +1278,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Float: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Float](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Float]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -1293,6 +1324,10 @@ object Chunk {
def apply(i: Int) = values(offset + i)
def at(i: Int) = values(offset + i)

protected[fs2] override def toArrayUnsafe[O2 >: Double: ClassTag]: Array[O2] =
if (offset == 0 && length == values.length) values.asInstanceOf[Array[O2]]
else values.slice(offset, length).asInstanceOf[Array[O2]]

def copyToArray[O2 >: Double](xs: Array[O2], start: Int): Unit =
if (xs.isInstanceOf[Array[Double]])
System.arraycopy(values, offset, xs, start, length)
Expand Down Expand Up @@ -1687,4 +1722,11 @@ object Chunk {
def empty[A]: Queue[A] = empty_.asInstanceOf[Queue[A]]
def apply[A](chunks: Chunk[A]*): Queue[A] = chunks.foldLeft(empty[A])(_ :+ _)
}

def newBuilder[A]: Collector.Builder[A, Chunk[A]] =
new Collector.Builder[A, Chunk[A]] {
private[this] var queue = Chunk.Queue.empty[A]
def +=(c: Chunk[A]): Unit = queue = queue :+ c
def result: Chunk[A] = queue.toChunk
}
}
Loading