summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/parallel/package.scala
diff options
context:
space:
mode:
authorAleksandar Prokopec <axel22@gmail.com>2012-02-01 16:50:31 +0100
committerAleksandar Prokopec <axel22@gmail.com>2012-02-01 17:04:47 +0100
commitfe289dc0fd8172012e4d57d09658e2dfd0a4cdcf (patch)
tree4f2cbda8a78221524c5b4ba400a040061bfb43f8 /src/library/scala/collection/parallel/package.scala
parent37bcff7956451cd74d08899e0e49c8b569d3a882 (diff)
downloadscala-fe289dc0fd8172012e4d57d09658e2dfd0a4cdcf.tar.gz
scala-fe289dc0fd8172012e4d57d09658e2dfd0a4cdcf.tar.bz2
scala-fe289dc0fd8172012e4d57d09658e2dfd0a4cdcf.zip
Add support for combiner sharing to parallel collections.
Concurrent (thread-safe) collections can implement builders directly for parallel collections by having themselves as builders, similar to buffers being builders for themselves in sequential collections. Combining 2 shared combiners can be expensive (more than logn), but is never done, since `combine` is always called with `this` (receiver) combiner, so `combine` just returns `this`. This commit adds support for implementing a parallel collection by having itself as combiner (parallel builder). To do this, clients must now merely implement their combiners by overriding the method `canBeShared` and having it return `true`. The support is implemented by refining the abstraction which creates combiners in tasks - this abstraction is provided by the protected `combinerFactory` method, previously `cbfactory`. The essential difference is that if the combiner can be shared, this method returns a combiner factory which always returns the same combiner. Otherwise, it always creates a new combiner. Span and takeWhile had to be modified for shared combiners to make this work, but for all other collections they work as before. Several other tasks had to be modified slightly, as well.
Diffstat (limited to 'src/library/scala/collection/parallel/package.scala')
-rw-r--r--src/library/scala/collection/parallel/package.scala15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/library/scala/collection/parallel/package.scala b/src/library/scala/collection/parallel/package.scala
index f152629c50..f154019bac 100644
--- a/src/library/scala/collection/parallel/package.scala
+++ b/src/library/scala/collection/parallel/package.scala
@@ -83,6 +83,7 @@ package object parallel {
}
}
+
package parallel {
trait FactoryOps[From, Elem, To] {
trait Otherwise[R] {
@@ -113,7 +114,19 @@ package parallel {
}
/* classes */
-
+
+ trait CombinerFactory[U, Repr] {
+ /** Provides a combiner used to construct a collection. */
+ def apply(): Combiner[U, Repr]
+ /** The call to the `apply` method can create a new combiner each time.
+ * If it does, this method returns `false`.
+ * The same combiner factory may be used each time (typically, this is
+ * the case for concurrent collections, which are thread safe).
+ * If so, the method returns `true`.
+ */
+ def doesShareCombiners: Boolean
+ }
+
/** Composite throwable - thrown when multiple exceptions are thrown at the same time. */
final case class CompositeThrowable(
val throwables: Set[Throwable]