Skip to content

Commit

Permalink
Restore special case for Vector.++ to detect prepends
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym authored and lrytz committed Jun 5, 2020
1 parent e4e3596 commit 5d2b94c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/library/scala/collection/immutable/Vector.scala
Expand Up @@ -198,6 +198,7 @@ sealed abstract class Vector[+A] private[immutable] (private[immutable] final va
if(k == 0) this
else appendedAll0(suffix, k)
}
private final val Log2ConcatFaster = 5

protected[this] def appendedAll0[B >: A](suffix: collection.IterableOnce[B], k: Int): Vector[B] = {
val tinyAppendLimit = 4 + vectorSliceCount
Expand All @@ -208,6 +209,11 @@ sealed abstract class Vector[+A] private[immutable] (private[immutable] final va
case _ => suffix.iterator.foreach(x => v = v.appended(x))
}
v
} else if (this.size < (k >>> Log2ConcatFaster) && suffix.isInstanceOf[Vector[B]]) {
var v = suffix.asInstanceOf[Vector[B]]
val ri = this.reverseIterator
while (ri.hasNext) v = v.prepended(ri.next())
v
} else new VectorBuilder[B].initFrom(this).addAll(suffix).result()
}

Expand Down
6 changes: 6 additions & 0 deletions test/junit/scala/collection/immutable/VectorTest.scala
Expand Up @@ -412,6 +412,12 @@ class VectorTest {
assertEquals(0 until (size + appendSize), v3)
}
}

// scala/bug#12027
// This took forever in 2.13.2
@Test def testAppendWithTinyLhs(): Unit = {
(1 to 1000000).foldLeft(Vector.empty[Int]) { (v, i) => Vector(i) ++ v }
}
}

object VectorUtils {
Expand Down

0 comments on commit 5d2b94c

Please sign in to comment.