Skip to content

Commit be7524f

Browse files
authored
Merge pull request scala/scala#8641 from retronym/faster/partition-conserve
Reduce allocations for one-sided usages of List.partition
2 parents 6cf527f + a959817 commit be7524f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

library/src/scala/collection/immutable/List.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,15 @@ sealed abstract class List[+A]
556556
result
557557
}
558558

559+
override def partition(p: A => Boolean): (List[A], List[A]) = {
560+
if (isEmpty) List.TupleOfNil
561+
else super.partition(p) match {
562+
case (Nil, xs) => (Nil, this)
563+
case (xs, Nil) => (this, Nil)
564+
case pair => pair
565+
}
566+
}
567+
559568
final override def toList: List[A] = this
560569

561570
// Override for performance
@@ -610,6 +619,7 @@ case object Nil extends List[Nothing] {
610619
*/
611620
@SerialVersionUID(3L)
612621
object List extends StrictOptimizedSeqFactory[List] {
622+
private val TupleOfNil = (Nil, Nil)
613623

614624
def from[B](coll: collection.IterableOnce[B]): List[B] = coll match {
615625
case coll: List[B] => coll

0 commit comments

Comments
 (0)