@@ -132,19 +132,25 @@ override def companion: GenericCompanion[Vector] = Vector
132
132
throw new IndexOutOfBoundsException (index.toString)
133
133
}
134
134
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
+
136
139
// SeqLike api
137
140
138
141
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
140
144
else super .updated(index, elem)(bf)
141
145
142
146
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
144
149
else super .+: (elem)(bf)
145
150
146
151
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
148
154
else super .:+ (elem)(bf)
149
155
150
156
override def take (n : Int ): Vector [A ] = {
@@ -211,7 +217,8 @@ override def companion: GenericCompanion[Vector] = Vector
211
217
212
218
// concat (suboptimal but avoids worst performance gotchas)
213
219
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
215
222
import Vector .{Log2ConcatFaster , TinyAppendFaster }
216
223
if (that.isEmpty) this .asInstanceOf [That ]
217
224
else {
0 commit comments