Skip to content

Commit c59b8d0

Browse files
nicolasstuckiallanrenucci
authored andcommitted
Mitigate #2924
To make the stack shorter we implement foldRightBN with a foldLeftBN which allocates only half of the closures. By doing so we need to eagerly reverse the list which is in most cases small or empty.
1 parent 42e1f22 commit c59b8d0

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

compiler/src/dotty/tools/dotc/core/Decorators.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,15 @@ object Decorators {
112112
else x1 :: xs1
113113
}
114114

115-
def foldRightBN[U](z: => U)(op: (T, => U) => U): U = xs match {
116-
case Nil => z
117-
case x :: xs1 => op(x, xs1.foldRightBN(z)(op))
115+
def foldRightBN[U](z: => U)(op: (T, => U) => U): U =
116+
xs.reverse.foldLeftBN(z)(op)
117+
118+
final def foldLeftBN[U](acc: => U)(op: (T, => U) => U): U = {
119+
@tailrec def fold(xs: List[T], acc: => U): U = xs match {
120+
case x :: xs1 => fold(xs1, op(x, acc))
121+
case Nil => acc
122+
}
123+
fold(xs, acc)
118124
}
119125

120126
final def hasSameLengthAs[U](ys: List[U]): Boolean = {

0 commit comments

Comments
 (0)