Skip to content

Commit dfaa965

Browse files
committed
Remove printing inefficiencies
Two major changes - Remove the recursive `show` when homogenizing Or-types. This one caused a combinatorial explosion when test-pickling Compiler.scala, because the phases list is an OrType of many element types. - Make `provided` by name in its text prefix.
1 parent 3b2d808 commit dfaa965

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
5353
case AndType(tp1, tp2) =>
5454
homogenize(tp1) & homogenize(tp2)
5555
case OrType(tp1, tp2) =>
56-
if (tp1.show > tp2.show)
57-
homogenize(tp1) | homogenize(tp2)
58-
else
59-
homogenize(tp2) | homogenize(tp1)
56+
homogenize(tp1) | homogenize(tp2)
6057
case tp: SkolemType =>
6158
homogenize(tp.info)
6259
case tp: LazyRef =>
@@ -199,20 +196,20 @@ class PlainPrinter(_ctx: Context) extends Printer {
199196
case tp: TypeLambda =>
200197
changePrec(GlobalPrec) {
201198
"[" ~ paramsText(tp) ~ "]" ~ lambdaHash(tp) ~
202-
(" => " provided !tp.resultType.isInstanceOf[MethodType]) ~
199+
(Str(" => ") provided !tp.resultType.isInstanceOf[MethodType]) ~
203200
toTextGlobal(tp.resultType)
204201
}
205202
case AnnotatedType(tpe, annot) =>
206203
toTextLocal(tpe) ~ " " ~ toText(annot)
207204
case tp: TypeVar =>
208205
if (tp.isInstantiated)
209-
toTextLocal(tp.instanceOpt) ~ ("^" provided ctx.settings.YprintDebug.value)
206+
toTextLocal(tp.instanceOpt) ~ (Str("^") provided ctx.settings.YprintDebug.value)
210207
else {
211208
val constr = ctx.typerState.constraint
212209
val bounds =
213210
if (constr.contains(tp)) constr.fullBounds(tp.origin)(ctx.addMode(Mode.Printing))
214211
else TypeBounds.empty
215-
if (bounds.isAlias) toText(bounds.lo) ~ ("^" provided ctx.settings.YprintDebug.value)
212+
if (bounds.isAlias) toText(bounds.lo) ~ (Str("^") provided ctx.settings.YprintDebug.value)
216213
else if (ctx.settings.YshowVarBounds.value) "(" ~ toText(tp.origin) ~ "?" ~ toText(bounds) ~ ")"
217214
else toText(tp.origin)
218215
}
@@ -498,7 +495,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
498495
else
499496
Text()
500497

501-
nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (node.pos.toString provided ctx.settings.YprintPos.value)
498+
nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(node.pos.toString) provided ctx.settings.YprintPos.value)
502499
case _ =>
503500
tree.fallbackToText(this)
504501
}
@@ -544,6 +541,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
544541
def plain = this
545542

546543
protected def keywordStr(text: String): String = coloredStr(text, SyntaxHighlighting.KeywordColor)
544+
protected def keywordText(text: String): Text = coloredStr(text, SyntaxHighlighting.KeywordColor)
547545
protected def valDefText(text: Text): Text = coloredText(text, SyntaxHighlighting.ValDefColor)
548546
protected def typeText(text: Text): Text = coloredText(text, SyntaxHighlighting.TypeColor)
549547
protected def literalText(text: Text): Text = coloredText(text, SyntaxHighlighting.LiteralColor)

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
125125
atPrec(InfixPrec) { argText(args.head) }
126126
else
127127
toTextTuple(args.init)
128-
(keywordStr("implicit ") provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
128+
(keywordText("implicit ") provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
129129
}
130130

131131
def toTextDependentFunction(appType: MethodType): Text = {
132-
("implicit " provided appType.isImplicitMethod) ~
132+
(keywordText("implicit ") provided appType.isImplicitMethod) ~
133133
"(" ~ paramsText(appType) ~ ") => " ~ toText(appType.resultType)
134134
}
135135

@@ -186,7 +186,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
186186
withoutPos(super.toText(tp))
187187
case tp: SelectionProto =>
188188
return "?{ " ~ toText(tp.name) ~
189-
(" " provided !tp.name.toSimpleName.last.isLetterOrDigit) ~
189+
(Str(" ") provided !tp.name.toSimpleName.last.isLetterOrDigit) ~
190190
": " ~ toText(tp.memberProto) ~ " }"
191191
case tp: ViewProto =>
192192
return toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)
@@ -272,7 +272,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
272272
if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes
273273
val flags = mods.flags & flagMask
274274
val flagsText = if (flags.isEmpty) "" else keywordStr((mods.flags & flagMask).toString)
275-
Text(mods.annotations.map(annotText), " ") ~~ flagsText ~~ (kw provided !suppressKw)
275+
Text(mods.annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
276276
}
277277

278278
def varianceText(mods: untpd.Modifiers) =
@@ -343,7 +343,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
343343

344344
val bodyText = "{" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, "\n") ~ "}"
345345

346-
prefix ~ (keywordStr(" extends") provided !ofNew) ~~ parentsText ~~ bodyText
346+
prefix ~ (keywordText(" extends") provided !ofNew) ~~ parentsText ~~ bodyText
347347
}
348348

349349
def toTextPackageId(pid: Tree): Text =
@@ -412,7 +412,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
412412
blockText(stats :+ expr)
413413
case If(cond, thenp, elsep) =>
414414
changePrec(GlobalPrec) {
415-
keywordStr("if ") ~ toText(cond) ~ (keywordStr(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
415+
keywordStr("if ") ~ toText(cond) ~ (keywordText(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
416416
}
417417
case Closure(env, ref, target) =>
418418
"closure(" ~ (toTextGlobal(env, ", ") ~ " | " provided env.nonEmpty) ~

compiler/src/dotty/tools/dotc/printing/Texts.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ object Texts {
146146
def over (that: Text) =
147147
if (this.isVertical) Vertical(that :: this.relems)
148148
else Vertical(that :: this :: Nil)
149-
150-
def provided(pred: Boolean) = if (pred) this else Str("")
151149
}
152150

153151
object Text {
@@ -169,6 +167,10 @@ object Texts {
169167

170168
/** The given texts `xs`, each on a separate line */
171169
def lines(xs: Traversable[Text]) = Vertical(xs.toList.reverse)
170+
171+
implicit class textDeco(text: => Text) {
172+
def provided(cond: Boolean): Text = if (cond) text else Str("")
173+
}
172174
}
173175

174176
case class Str(s: String, lineRange: LineRange = EmptyLineRange) extends Text {

0 commit comments

Comments
 (0)