Skip to content

Commit f9d1970

Browse files
committed
Compute debug messages lazily in safe init
I noticed that a significant amount of time was spent pretty-printing Strings that were never displayed when compiling scalatest in the community-build, fixed by making the message parameter of traceIndented and traceOp by-name, just like printer.println itself already is.
1 parent b9773d6 commit f9d1970

File tree

1 file changed

+6
-5
lines changed
  • compiler/src/dotty/tools/dotc/transform/init

1 file changed

+6
-5
lines changed

compiler/src/dotty/tools/dotc/transform/init/Util.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import config.Printers.Printer
1010
import annotation.tailrec
1111

1212
object Util {
13-
def traceIndented(msg: String, printer: Printer)(using Context): Unit =
13+
def traceIndented(msg: => String, printer: Printer)(using Context): Unit =
1414
printer.println(s"${ctx.base.indentTab * ctx.base.indent} $msg")
1515

16-
def traceOp(msg: String, printer: Printer)(op: => Unit)(using Context): Unit = {
17-
traceIndented(s"==> ${msg}", printer)
16+
def traceOp(msg: => String, printer: Printer)(op: => Unit)(using Context): Unit = {
17+
lazy val computedMsg = msg // Make sure we only compute msg once
18+
traceIndented(s"==> ${computedMsg}", printer)
1819
op
19-
traceIndented(s"<== ${msg}", printer)
20+
traceIndented(s"<== ${computedMsg}", printer)
2021
}
2122

2223
extension (symbol: Symbol) def hasSource(using Context): Boolean =
@@ -38,4 +39,4 @@ object Util {
3839
}
3940
loop(cls.info.baseClasses.dropWhile(sym.owner != _))
4041
}
41-
}
42+
}

0 commit comments

Comments
 (0)