Skip to content

Commit 17b6594

Browse files
committed
More tailrec in handling long seq
1 parent 6b16203 commit 17b6594

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

src/reflect/scala/reflect/internal/Printers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ trait Printers extends api.Printers { self: SymbolTable =>
109109
out.write(indentString, 0, indentMargin)
110110
}
111111

112-
def printSeq[a](ls: List[a])(printelem: a => Unit)(printsep: => Unit): Unit =
112+
@tailrec
113+
final def printSeq[A](ls: List[A])(printelem: A => Unit)(printsep: => Unit): Unit =
113114
ls match {
114115
case List() =>
115116
case List(x) => printelem(x)

src/reflect/scala/reflect/internal/tpe/GlbLubs.scala

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,29 +198,34 @@ private[internal] trait GlbLubs {
198198

199199
/** From a list of types, retain only maximal types as determined by the partial order `po`. */
200200
private def maxTypes(ts: List[Type])(po: (Type, Type) => Boolean): List[Type] = {
201-
def loop(ts: List[Type]): List[Type] = ts match {
202-
case t :: ts1 =>
203-
val ts2 = loop(ts1.filterNot(po(_, t)))
204-
if (ts2.exists(po(t, _))) ts2 else t :: ts2
205-
case Nil => Nil
201+
@tailrec
202+
def loop(remaining: List[Type], hs: List[Type]): List[Type] = remaining match {
203+
case h :: rest =>
204+
loop(rest.filterNot(po(_, h)), h :: hs)
205+
case _ =>
206+
def sieve(res: List[Type], todo: List[Type]): List[Type] = todo match {
207+
case h :: tail =>
208+
val res1 = if (res.exists(po(h, _))) res else h :: res
209+
sieve(res1, tail)
210+
case _ => res
211+
}
212+
sieve(Nil, hs)
206213
}
207214

208215
// The order here matters because type variables and
209216
// wildcards can act both as subtypes and supertypes.
210-
val (ts2, ts1) = partitionConserve(ts) { tp =>
211-
isWildCardOrNonGroundTypeVarCollector.collect(tp).isDefined
212-
}
217+
val (wilds, ts1) = partitionConserve(ts)(isWildCardOrNonGroundTypeVarCollector.collect(_).isDefined)
213218

214-
loop(ts1 ::: ts2)
219+
loop(ts1 ::: wilds, Nil)
215220
}
216221

217222
/** Eliminate from list of types all elements which are a supertype
218-
* of some other element of the list. */
223+
* of some other element of the list. */
219224
private def elimSuper(ts: List[Type]): List[Type] =
220225
maxTypes(ts)((t1, t2) => t2 <:< t1)
221226

222227
/** Eliminate from list of types all elements which are a subtype
223-
* of some other element of the list. */
228+
* of some other element of the list. */
224229
@tailrec private def elimSub(ts: List[Type], depth: Depth): List[Type] = {
225230
val ts1 = maxTypes(ts)(isSubType(_, _, depth.decr))
226231
if (ts1.lengthCompare(1) <= 0) ts1 else {

test/files/run/t12757.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import scala.tools.partest.DirectTest
3+
4+
object Test extends DirectTest {
5+
def header = """|object Test extends App {
6+
| val myStrings: List[String] = List(""".stripMargin.linesIterator
7+
def footer = """| )
8+
| println(myStrings.mkString(","))
9+
|}""".stripMargin.linesIterator
10+
def values = Iterator.tabulate(4000)(i => s" \"$i\",")
11+
def code = (header ++ values ++ footer).mkString("\n")
12+
13+
override def extraSettings: String = "-usejavacp -J-Xms256k"
14+
15+
def show() = assert(compile())
16+
}

0 commit comments

Comments
 (0)