Skip to content

Commit c9c5d89

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 071db84 commit c9c5d89

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
@@ -124,11 +124,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
124124
atPrec(InfixPrec) { argText(args.head) }
125125
else
126126
toTextTuple(args.init)
127-
(keywordStr("implicit ") provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
127+
(keywordText("implicit ") provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
128128
}
129129

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

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

277277
def varianceText(mods: untpd.Modifiers) =
@@ -342,7 +342,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
342342

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

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

348348
def toTextPackageId(pid: Tree): Text =
@@ -411,7 +411,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
411411
blockText(stats :+ expr)
412412
case If(cond, thenp, elsep) =>
413413
changePrec(GlobalPrec) {
414-
keywordStr("if ") ~ toText(cond) ~ (keywordStr(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
414+
keywordStr("if ") ~ toText(cond) ~ (keywordText(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
415415
}
416416
case Closure(env, ref, target) =>
417417
"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)