summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/SetLike.scala
diff options
context:
space:
mode:
authorRex Kerr <ichoran@gmail.com>2014-11-30 13:43:21 -0800
committerRex Kerr <ichoran@gmail.com>2014-11-30 20:49:16 -0800
commite3d5314ac13f32cb2dde747bad8f8633f223a2e2 (patch)
tree63079a6ac70ab05bc5b71c6b959a5e4ef94f3ad7 /src/library/scala/collection/SetLike.scala
parent7a450a0fd4298f8b8ac2de241ffdb3fcf1b2cae5 (diff)
downloadscala-e3d5314ac13f32cb2dde747bad8f8633f223a2e2.tar.gz
scala-e3d5314ac13f32cb2dde747bad8f8633f223a2e2.tar.bz2
scala-e3d5314ac13f32cb2dde747bad8f8633f223a2e2.zip
SI-7981 toSeq on immutable Map and Set return ArrayBuffer
Switched to `Vector` for the default `MapLike`/`SetLike`, then back again in `mutable.MapLike`/`mutable.SetLike`. Preliminary performance testing indicates an across-the-board improvement due to cutting out a great deal of indirection. In the case of empty immutable maps or sets, the improvement is enormous: it quickly returns Vector.empty instead of jumping through all sorts of hoops to eventually create an empty `ArrayBuffer`. One possible downside is that `Vector` is pretty memory-hungry for small collections. Need to evalute this; rather than abandoning this change, it's probably better to make `Vector` smarter about small collections. Also fixed test with literal "ArrayBuffer" in output (`s/ArrayBuffer/Vector/g`).
Diffstat (limited to 'src/library/scala/collection/SetLike.scala')
-rw-r--r--src/library/scala/collection/SetLike.scala15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/library/scala/collection/SetLike.scala b/src/library/scala/collection/SetLike.scala
index 3e549f72cd..80a344e6a8 100644
--- a/src/library/scala/collection/SetLike.scala
+++ b/src/library/scala/collection/SetLike.scala
@@ -77,11 +77,20 @@ self =>
protected[this] override def parCombiner = ParSet.newCombiner[A]
- /* Overridden for efficiency. */
- override def toSeq: Seq[A] = toBuffer[A]
+ // Default collection type appropriate for immutable collections; mutable collections override this
+ override def toSeq: Seq[A] = {
+ if (isEmpty) Vector.empty[A]
+ else {
+ val vb = Vector.newBuilder[A]
+ foreach(vb += _)
+ vb.result
+ }
+ }
+
override def toBuffer[A1 >: A]: mutable.Buffer[A1] = {
val result = new mutable.ArrayBuffer[A1](size)
- copyToBuffer(result)
+ // Faster to let the map iterate itself than to defer through copyToBuffer
+ foreach(result += _)
result
}