Skip to content

Commit f13fc65

Browse files
committed
Merge pull request scala#4175 from Ichoran/issue/7981
SI-7981 toSeq on immutable Map and Set return ArrayBuffer
2 parents 7a450a0 + e3d5314 commit f13fc65

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

src/library/scala/collection/MapLike.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,20 @@ self =>
323323
res
324324
}
325325

326-
/* Overridden for efficiency. */
327-
override def toSeq: Seq[(A, B)] = toBuffer[(A, B)]
326+
override def toSeq: Seq[(A, B)] = {
327+
if (isEmpty) Vector.empty[(A, B)]
328+
else {
329+
// Default appropriate for immutable collections; mutable collections override this
330+
val vb = Vector.newBuilder[(A, B)]
331+
foreach(vb += _)
332+
vb.result
333+
}
334+
}
335+
328336
override def toBuffer[C >: (A, B)]: mutable.Buffer[C] = {
329337
val result = new mutable.ArrayBuffer[C](size)
330-
copyToBuffer(result)
338+
// Faster to let the map iterate itself than to defer through copyToBuffer
339+
foreach(result += _)
331340
result
332341
}
333342

src/library/scala/collection/SetLike.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,20 @@ self =>
7777

7878
protected[this] override def parCombiner = ParSet.newCombiner[A]
7979

80-
/* Overridden for efficiency. */
81-
override def toSeq: Seq[A] = toBuffer[A]
80+
// Default collection type appropriate for immutable collections; mutable collections override this
81+
override def toSeq: Seq[A] = {
82+
if (isEmpty) Vector.empty[A]
83+
else {
84+
val vb = Vector.newBuilder[A]
85+
foreach(vb += _)
86+
vb.result
87+
}
88+
}
89+
8290
override def toBuffer[A1 >: A]: mutable.Buffer[A1] = {
8391
val result = new mutable.ArrayBuffer[A1](size)
84-
copyToBuffer(result)
92+
// Faster to let the map iterate itself than to defer through copyToBuffer
93+
foreach(result += _)
8594
result
8695
}
8796

src/library/scala/collection/mutable/MapLike.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ trait MapLike[A, B, +This <: MapLike[A, B, This] with Map[A, B]]
5959
override protected[this] def newBuilder: Builder[(A, B), This] = empty
6060

6161
protected[this] override def parCombiner = ParMap.newCombiner[A, B]
62+
63+
/** Converts this $coll to a sequence.
64+
*
65+
* ```Note```: assumes a fast `size` method. Subclasses should override if this is not true.
66+
*/
67+
override def toSeq: collection.Seq[(A, B)] = {
68+
// ArrayBuffer for efficiency, preallocated to the right size.
69+
val result = new ArrayBuffer[(A, B)](size)
70+
foreach(result += _)
71+
result
72+
}
73+
6274

6375
/** Adds a new key/value pair to this map and optionally returns previously bound value.
6476
* If the map already contains a

src/library/scala/collection/mutable/SetLike.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ trait SetLike[A, +This <: SetLike[A, This] with Set[A]]
7272

7373
protected[this] override def parCombiner = ParSet.newCombiner[A]
7474

75+
/** Converts this $coll to a sequence.
76+
*
77+
* ```Note```: assumes a fast `size` method. Subclasses should override if this is not true.
78+
*/
79+
override def toSeq: collection.Seq[A] = {
80+
// ArrayBuffer for efficiency, preallocated to the right size.
81+
val result = new ArrayBuffer[A](size)
82+
foreach(result += _)
83+
result
84+
}
85+
7586
/** Adds an element to this $coll.
7687
*
7788
* @param elem the element to be added

test/files/run/inline-ex-handlers.check

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@
123123
300 RETURN(UNIT)
124124
@@ -583,6 +603,6 @@
125125
with finalizer: null
126-
- catch (Throwable) in ArrayBuffer(7, 9, 10) starting at: 6
127-
+ catch (Throwable) in ArrayBuffer(7, 9, 10, 11) starting at: 6
126+
- catch (Throwable) in Vector(7, 9, 10) starting at: 6
127+
+ catch (Throwable) in Vector(7, 9, 10, 11) starting at: 6
128128
consisting of blocks: List(6)
129129
with finalizer: null
130-
- catch (Throwable) in ArrayBuffer(4, 6, 7, 9, 10) starting at: 3
131-
+ catch (Throwable) in ArrayBuffer(4, 6, 7, 9, 10, 11, 12) starting at: 3
130+
- catch (Throwable) in Vector(4, 6, 7, 9, 10) starting at: 3
131+
+ catch (Throwable) in Vector(4, 6, 7, 9, 10, 11, 12) starting at: 3
132132
consisting of blocks: List(3)
133133
@@ -618,3 +638,3 @@
134134
startBlock: 1
@@ -171,8 +171,8 @@
171171
}
172172
@@ -690,3 +730,3 @@
173173
with finalizer: null
174-
- catch (<none>) in ArrayBuffer(4, 5, 6, 8) starting at: 3
175-
+ catch (<none>) in ArrayBuffer(4, 5, 6, 8, 10) starting at: 3
174+
- catch (<none>) in Vector(4, 5, 6, 8) starting at: 3
175+
+ catch (<none>) in Vector(4, 5, 6, 8, 10) starting at: 3
176176
consisting of blocks: List(3)
177177
@@ -714,5 +754,5 @@
178178
def main(args: Array[String] (ARRAY[REF(class String)])): Unit {
@@ -276,12 +276,12 @@
276276
}
277277
@@ -852,6 +918,6 @@
278278
with finalizer: null
279-
- catch (Throwable) in ArrayBuffer(13, 14, 15, 18, 20, 21, 23) starting at: 4
280-
+ catch (Throwable) in ArrayBuffer(13, 14, 15, 18, 20, 21, 23, 25) starting at: 4
279+
- catch (Throwable) in Vector(13, 14, 15, 18, 20, 21, 23) starting at: 4
280+
+ catch (Throwable) in Vector(13, 14, 15, 18, 20, 21, 23, 25) starting at: 4
281281
consisting of blocks: List(9, 8, 6, 5, 4)
282282
with finalizer: null
283-
- catch (<none>) in ArrayBuffer(4, 5, 6, 9, 13, 14, 15, 18, 20, 21, 23) starting at: 3
284-
+ catch (<none>) in ArrayBuffer(4, 5, 6, 9, 13, 14, 15, 18, 20, 21, 23, 25, 26) starting at: 3
283+
- catch (<none>) in Vector(4, 5, 6, 9, 13, 14, 15, 18, 20, 21, 23) starting at: 3
284+
+ catch (<none>) in Vector(4, 5, 6, 9, 13, 14, 15, 18, 20, 21, 23, 25, 26) starting at: 3
285285
consisting of blocks: List(3)
286286
@@ -879,5 +945,5 @@
287287
def main(args: Array[String] (ARRAY[REF(class String)])): Unit {
@@ -317,8 +317,8 @@
317317
127 CALL_METHOD scala.Predef.println (dynamic)
318318
@@ -964,3 +1034,3 @@
319319
with finalizer: null
320-
- catch (IllegalArgumentException) in ArrayBuffer(6, 7, 8, 11, 13, 14, 16) starting at: 3
321-
+ catch (IllegalArgumentException) in ArrayBuffer(6, 7, 8, 11, 13, 14, 16, 17) starting at: 3
320+
- catch (IllegalArgumentException) in Vector(6, 7, 8, 11, 13, 14, 16) starting at: 3
321+
+ catch (IllegalArgumentException) in Vector(6, 7, 8, 11, 13, 14, 16, 17) starting at: 3
322322
consisting of blocks: List(3)
323323
@@ -988,5 +1058,5 @@
324324
def main(args: Array[String] (ARRAY[REF(class String)])): Unit {

0 commit comments

Comments
 (0)