Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit e1cc99f

Browse files
committed
Merge pull request scala#4331 from Ichoran/issue/8930
SI-8930 - Vector updated, +:, and :+ slow when typed as Seq[A]
2 parents 305dd96 + bb4b79c commit e1cc99f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/library/scala/collection/immutable/Vector.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,25 @@ override def companion: GenericCompanion[Vector] = Vector
132132
throw new IndexOutOfBoundsException(index.toString)
133133
}
134134

135-
135+
// If we have a default builder, there are faster ways to perform some operations
136+
@inline private[this] def isDefaultCBF[A, B, That](bf: CanBuildFrom[Vector[A], B, That]): Boolean =
137+
(bf eq IndexedSeq.ReusableCBF) || (bf eq collection.immutable.Seq.ReusableCBF) || (bf eq collection.Seq.ReusableCBF)
138+
136139
// SeqLike api
137140

138141
override def updated[B >: A, That](index: Int, elem: B)(implicit bf: CanBuildFrom[Vector[A], B, That]): That =
139-
if (bf eq IndexedSeq.ReusableCBF) updateAt(index, elem).asInstanceOf[That] // just ignore bf
142+
if (isDefaultCBF[A, B, That](bf))
143+
updateAt(index, elem).asInstanceOf[That] // ignore bf--it will just give a Vector, and slowly
140144
else super.updated(index, elem)(bf)
141145

142146
override def +:[B >: A, That](elem: B)(implicit bf: CanBuildFrom[Vector[A], B, That]): That =
143-
if (bf eq IndexedSeq.ReusableCBF) appendFront(elem).asInstanceOf[That] // just ignore bf
147+
if (isDefaultCBF[A, B, That](bf))
148+
appendFront(elem).asInstanceOf[That] // ignore bf--it will just give a Vector, and slowly
144149
else super.+:(elem)(bf)
145150

146151
override def :+[B >: A, That](elem: B)(implicit bf: CanBuildFrom[Vector[A], B, That]): That =
147-
if (bf eq IndexedSeq.ReusableCBF) appendBack(elem).asInstanceOf[That] // just ignore bf
152+
if (isDefaultCBF(bf))
153+
appendBack(elem).asInstanceOf[That] // ignore bf--it will just give a Vector, and slowly
148154
else super.:+(elem)(bf)
149155

150156
override def take(n: Int): Vector[A] = {
@@ -211,7 +217,8 @@ override def companion: GenericCompanion[Vector] = Vector
211217

212218
// concat (suboptimal but avoids worst performance gotchas)
213219
override def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Vector[A], B, That]): That = {
214-
if (bf eq IndexedSeq.ReusableCBF) {
220+
if (isDefaultCBF(bf)) {
221+
// We are sure we will create a Vector, so let's do it efficiently
215222
import Vector.{Log2ConcatFaster, TinyAppendFaster}
216223
if (that.isEmpty) this.asInstanceOf[That]
217224
else {

0 commit comments

Comments
 (0)