From 6db4db93a7d976f1a3b99f8f1bffff23a1ae3924 Mon Sep 17 00:00:00 2001 From: James Iry Date: Wed, 30 Jan 2013 15:29:42 -0800 Subject: SI-2818 Make List.foldRight always do a reverse/foldLeft flip Benchmarks show that lists smaller than 110 elements or so doing reverse/foldLeft is faster than recursively walking to the end of the list and then folding as the stack unwinds. Above that 110 element threshold the recursive procedure is faster. Unfortunately, at some magic unknown large size the recursive procedure blows the stack. This commit changes List#foldRight to always do reverse/foldLeft. --- src/library/scala/collection/immutable/List.scala | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/library/scala/collection/immutable/List.scala b/src/library/scala/collection/immutable/List.scala index 56e386ad67..55ac3995e9 100644 --- a/src/library/scala/collection/immutable/List.scala +++ b/src/library/scala/collection/immutable/List.scala @@ -295,6 +295,9 @@ sealed abstract class List[+A] extends AbstractSeq[A] } result } + + override def foldRight[B](z: B)(op: (A, B) => B): B = + reverse.foldLeft(z)((right, left) => op(left, right)) override def stringPrefix = "List" -- cgit v1.2.3