Skip to content

Commit ce632fc

Browse files
committed
Revert "Allow forcing traces"
This reverts commit 83e6ad4. Also, optimize trace further to be zero overhead
1 parent fb3128e commit ce632fc

File tree

2 files changed

+37
-64
lines changed

2 files changed

+37
-64
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,9 @@ abstract class SymbolLoader extends LazyType { self =>
339339
}
340340
try {
341341
val start = System.currentTimeMillis
342-
if (Config.tracingEnabled && ctx.settings.YdebugTrace.value)
343-
trace(s">>>> loading ${root.debugString}", _ => s"<<<< loaded ${root.debugString}") {
344-
doComplete(root)
345-
}
346-
else
342+
trace.onDebug("loading") {
347343
doComplete(root)
344+
}
348345
report.informTime("loaded " + description, start)
349346
}
350347
catch {

compiler/src/dotty/tools/dotc/reporting/trace.scala

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,28 @@ import config.Config
77
import config.Printers
88
import core.Mode
99

10-
abstract class TraceSyntax {
11-
val isForced: Boolean
10+
/** This module is carefully optimized to give zero overhead if Config.tracingEnabled
11+
* is false. The `trace` operation is called in various hotspots, so every tiny bit
12+
* of overhead is unacceptable: boxing, closures, additional method calls are all out.
13+
*/
14+
object trace:
1215

1316
inline def onDebug[TD](inline question: String)(inline op: TD)(using Context): TD =
1417
conditionally(ctx.settings.YdebugTrace.value, question, false)(op)
1518

16-
inline def conditionally[TC](inline cond: Boolean, inline question: String, inline show: Boolean)(op: => TC)(using Context): TC =
17-
inline if (isForced || Config.tracingEnabled) {
18-
if (cond) apply[TC](question, Printers.default, show)(op)
19-
else op
20-
}
19+
inline def conditionally[TC](inline cond: Boolean, inline question: String, inline show: Boolean)(inline op: TC)(using Context): TC =
20+
if Config.tracingEnabled then
21+
apply(question, if cond then Printers.default else Printers.noPrinter, show)(op)
2122
else op
2223

23-
inline def apply[T](inline question: String, inline printer: Printers.Printer, inline showOp: Any => String)(op: => T)(using Context): T =
24-
inline if (isForced || Config.tracingEnabled) {
25-
if (!isForced && printer.eq(config.Printers.noPrinter)) op
26-
else doTrace[T](question, printer, showOp)(op)
27-
}
24+
inline def apply[T](inline question: String, inline printer: Printers.Printer, inline showOp: Any => String)(inline op: T)(using Context): T =
25+
if Config.tracingEnabled then
26+
doTrace[T](question, printer, showOp)(op)
2827
else op
2928

30-
inline def apply[T](inline question: String, inline printer: Printers.Printer, inline show: Boolean)(op: => T)(using Context): T =
31-
inline if (isForced || Config.tracingEnabled) {
32-
if (!isForced && printer.eq(config.Printers.noPrinter)) op
33-
else doTrace[T](question, printer, if (show) showShowable(_) else alwaysToString)(op)
34-
}
29+
inline def apply[T](inline question: String, inline printer: Printers.Printer, inline show: Boolean)(inline op: T)(using Context): T =
30+
if Config.tracingEnabled then
31+
doTrace[T](question, printer, if show then showShowable(_) else alwaysToString)(op)
3532
else op
3633

3734
inline def apply[T](inline question: String, inline printer: Printers.Printer)(inline op: T)(using Context): T =
@@ -41,62 +38,41 @@ abstract class TraceSyntax {
4138
apply[T](question, Printers.default, show)(op)
4239

4340
inline def apply[T](inline question: String)(inline op: T)(using Context): T =
44-
apply[T](question, Printers.default, false)(op)
41+
apply[T](question, false)(op)
4542

46-
private def showShowable(x: Any)(using Context) = x match {
43+
private def showShowable(x: Any)(using Context) = x match
4744
case x: printing.Showable => x.show
4845
case _ => String.valueOf(x)
49-
}
5046

5147
private val alwaysToString = (x: Any) => String.valueOf(x)
5248

5349
private def doTrace[T](question: => String,
5450
printer: Printers.Printer = Printers.default,
5551
showOp: Any => String = alwaysToString)
56-
(op: => T)(using Context): T = {
57-
// Avoid evaluating question multiple time, since each evaluation
58-
// may cause some extra logging output.
59-
lazy val q: String = question
60-
apply[T](s"==> $q?", (res: Any) => s"<== $q = ${showOp(res)}")(op)
61-
}
62-
63-
def apply[T](leading: => String, trailing: Any => String)(op: => T)(using Context): T = {
64-
val log: String => Unit = if (isForced) Console.println else {
65-
var logctx = ctx
66-
while (logctx.reporter.isInstanceOf[StoreReporter]) logctx = logctx.outer
67-
report.log(_)(using logctx)
68-
}
69-
doApply(leading, trailing, log)(op)
70-
}
71-
72-
def doApply[T](leading: => String, trailing: Any => String, log: String => Unit)(op: => T)(using Context): T =
73-
if (ctx.mode.is(Mode.Printing)) op
74-
else {
52+
(op: => T)(using Context): T =
53+
if ctx.mode.is(Mode.Printing) || (printer eq Printers.noPrinter) then op
54+
else
55+
// Avoid evaluating question multiple time, since each evaluation
56+
// may cause some extra logging output.
57+
val q = question
58+
val leading = s"==> $q?"
59+
val trailing = (res: Any) => s"<== $q = ${showOp(res)}"
7560
var finalized = false
61+
var logctx = ctx
62+
while logctx.reporter.isInstanceOf[StoreReporter] do logctx = logctx.outer
63+
def margin = ctx.base.indentTab * ctx.base.indent
7664
def finalize(result: Any, note: String) =
77-
if (!finalized) {
65+
if !finalized then
7866
ctx.base.indent -= 1
79-
log(s"${ctx.base.indentTab * ctx.base.indent}${trailing(result)}$note")
67+
report.log(s"$margin${trailing(result)}$note")
8068
finalized = true
81-
}
82-
try {
83-
log(s"${ctx.base.indentTab * ctx.base.indent}$leading")
69+
try
70+
report.log(s"$margin$leading")
8471
ctx.base.indent += 1
8572
val res = op
8673
finalize(res, "")
8774
res
88-
}
89-
catch {
90-
case ex: Throwable =>
91-
finalize("<missing>", s" (with exception $ex)")
92-
throw ex
93-
}
94-
}
95-
}
96-
97-
object trace extends TraceSyntax {
98-
final val isForced = false
99-
object force extends TraceSyntax {
100-
final val isForced = true
101-
}
102-
}
75+
catch case ex: Throwable =>
76+
finalize("<missing>", s" (with exception $ex)")
77+
throw ex
78+
end trace

0 commit comments

Comments
 (0)