Skip to content

Commit

Permalink
enable ability to only record Stats.trackTime (scala#18337)
Browse files Browse the repository at this point in the history
also aggregate total phase time.

when combined with `-Ydetailed-stats` then you can see the time taken by
each phase, without blowing up the hot paths, also makes it easier to
see the total time taken by all phases.
  • Loading branch information
bishabosha authored Aug 3, 2023
2 parents ee9d929 + 4ff8d1f commit e4480a6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint

for (phase <- ctx.base.allPhases)
if (phase.isRunnable)
Stats.trackTime(s"$phase ms ") {
Stats.trackTime(s"phase time ms/$phase") {
val start = System.currentTimeMillis
val profileBefore = profiler.beforePhase(phase)
units = phase.runOn(units)
Expand Down
10 changes: 7 additions & 3 deletions compiler/src/dotty/tools/dotc/util/Stats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import collection.mutable

@sharable object Stats {

// when false, Stats.record and Stats.trackTime are elided.
inline val enabled = false

// set to true if only `trackTime` should be recorded by default
inline val timerOnly = false

var monitored: Boolean = false

@volatile private var stack: List[String] = Nil
Expand All @@ -19,8 +23,8 @@ import collection.mutable
override def default(key: String): Int = 0
}

inline def record(inline fn: String, inline n: Int = 1): Unit =
if (enabled) doRecord(fn, n)
inline def record(inline fn: String, inline n: Int = 1, inline skip: Boolean = timerOnly): Unit =
if (enabled && !skip) doRecord(fn, n)

def doRecord(fn: String, n: Int) =
if (monitored) {
Expand All @@ -38,7 +42,7 @@ import collection.mutable
def doTrackTime[T](fn: String)(op: => T): T = {
if (monitored) {
val start = System.nanoTime
try op finally record(fn, ((System.nanoTime - start) / 1000).toInt)
try op finally record(fn, ((System.nanoTime - start) / 1000).toInt, skip = false)
}
else op
}
Expand Down

0 comments on commit e4480a6

Please sign in to comment.