summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/MapLike.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/MapLike.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/MapLike.scala')
-rw-r--r--src/library/scala/collection/MapLike.scala15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala
index 3de30c2c8b..38a598321f 100644
--- a/src/library/scala/collection/MapLike.scala
+++ b/src/library/scala/collection/MapLike.scala
@@ -323,11 +323,20 @@ self =>
res
}
- /* Overridden for efficiency. */
- override def toSeq: Seq[(A, B)] = toBuffer[(A, B)]
+ override def toSeq: Seq[(A, B)] = {
+ if (isEmpty) Vector.empty[(A, B)]
+ else {
+ // Default appropriate for immutable collections; mutable collections override this
+ val vb = Vector.newBuilder[(A, B)]
+ foreach(vb += _)
+ vb.result
+ }
+ }
+
override def toBuffer[C >: (A, B)]: mutable.Buffer[C] = {
val result = new mutable.ArrayBuffer[C](size)
- copyToBuffer(result)
+ // Faster to let the map iterate itself than to defer through copyToBuffer
+ foreach(result += _)
result
}