Skip to content

Commit 6f63f0f

Browse files
committed
use VB.alignTo only if RHS is significantly larger
* To cempensate for alignTo's (small, but constant) overhead, it is only called when the right Vector is mor then 64 elemente longer than the left one. * replaced some `….knownSize` by `k`
1 parent cbe91fa commit 6f63f0f

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ sealed abstract class Vector[+A] private[immutable] (private[immutable] final va
206206
}
207207

208208
protected[this] def prependedAll0[B >: A](prefix: collection.IterableOnce[B], k: Int): Vector[B] = {
209+
// k >= 0, k = prefix.knownSize
209210
val tinyAppendLimit = 4 + vectorSliceCount
210211
if (k < tinyAppendLimit /*|| k < (this.size >>> Log2ConcatFaster)*/) {
211212
var v: Vector[B] = this
@@ -217,14 +218,15 @@ sealed abstract class Vector[+A] private[immutable] (private[immutable] final va
217218
val it = this.iterator
218219
while (it.hasNext) v = v :+ it.next()
219220
v
220-
} else if (prefix.knownSize >= 0 && prefix.knownSize < this.size) {
221-
new VectorBuilder[B].alignTo(prefix.knownSize, this).addAll(prefix).addAll(this).result()
221+
} else if (k < this.size - AlignToFaster) {
222+
new VectorBuilder[B].alignTo(k, this).addAll(prefix).addAll(this).result()
222223
} else super.prependedAll(prefix)
223224
}
224225

225226
protected[this] def appendedAll0[B >: A](suffix: collection.IterableOnce[B], k: Int): Vector[B] = {
227+
// k >= 0, k = suffix.knownSize
226228
val tinyAppendLimit = 4 + vectorSliceCount
227-
if(k > 0 && k < tinyAppendLimit) {
229+
if (k < tinyAppendLimit) {
228230
var v: Vector[B] = this
229231
suffix match {
230232
case it: Iterable[_] => it.asInstanceOf[Iterable[B]].foreach(x => v = v.appended(x))
@@ -236,7 +238,7 @@ sealed abstract class Vector[+A] private[immutable] (private[immutable] final va
236238
val ri = this.reverseIterator
237239
while (ri.hasNext) v = v.prepended(ri.next())
238240
v
239-
} else if (this.size < suffix.knownSize && suffix.isInstanceOf[Vector[_]]) {
241+
} else if (this.size < k - AlignToFaster && suffix.isInstanceOf[Vector[_]]) {
240242
val v = suffix.asInstanceOf[Vector[B]]
241243
new VectorBuilder[B].alignTo(this.size, v).addAll(this).addAll(v).result()
242244
} else new VectorBuilder[B].initFrom(this).addAll(suffix).result()
@@ -2000,6 +2002,7 @@ private[immutable] object VectorInline {
20002002
final val WIDTH5 = 1 << BITS5
20012003
final val LASTWIDTH = WIDTH << 1 // 1 extra bit in the last level to go up to Int.MaxValue (2^31-1) instead of 2^30:
20022004
final val Log2ConcatFaster = 5
2005+
final val AlignToFaster = 64
20032006

20042007
type Arr1 = Array[AnyRef]
20052008
type Arr2 = Array[Array[AnyRef]]

0 commit comments

Comments
 (0)