Skip to content

Commit 67d5cb0

Browse files
authored
Merge pull request #133 from DSlug/macro-time
re-add code tracking macroExpand times
2 parents 0c0541a + 5d3c9b4 commit 67d5cb0

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

plugin/src/main/scala/ch/epfl/scala/profilers/ProfilingImpl.scala

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ final class ProfilingImpl[G <: Global](
8080
import ProfilingMacroPlugin.macroInfos // , repeatedTrees}
8181
val perCallSite = macroInfos.toMap
8282
val perFile = groupPerFile(perCallSite)(MacroInfo.Empty, _ + _)
83-
.map {
84-
case (sf, mi) => sf -> mi.copy(expansionNanos = toMillis(mi.expansionNanos))
85-
}
8683
val inTotal = MacroInfo.aggregate(perFile.valuesIterator)
8784

8885
/* val repeated = repeatedTrees.toMap.valuesIterator
@@ -91,9 +88,7 @@ final class ProfilingImpl[G <: Global](
9188
.toMap*/
9289

9390
// perFile and inTotal are already converted to millis
94-
val callSiteNanos = perCallSite.map {
95-
case (pos, mi) => pos -> mi.copy(expansionNanos = toMillis(mi.expansionNanos))
96-
}
91+
val callSiteNanos = perCallSite
9792
MacroProfiler(callSiteNanos, perFile, inTotal, Map.empty) // repeated)
9893
}
9994

@@ -537,9 +532,23 @@ final class ProfilingImpl[G <: Global](
537532
macroCounter = macroCounter + 1
538533

539534
object expander extends analyzer.DefMacroExpander(t, expandee, md, pt) {
535+
private var alreadyTracking: Boolean = false
540536

541537
/** The default method that expands all macros. */
542538
override def apply(desugared: Tree): Tree = {
539+
def updateExpansionTime(desugared: Tree, start: statistics.TimerSnapshot): Unit = {
540+
statistics.stopTimer(preciseMacroTimer, start)
541+
val (nanos0, _) = start
542+
val timeNanos = (preciseMacroTimer.nanos - nanos0)
543+
val callSitePos = desugared.pos
544+
// Those that are not present failed to expand
545+
macroInfos.get(callSitePos).foreach { found =>
546+
val updatedInfo = found.copy(expansionNanos = timeNanos)
547+
macroInfos(callSitePos) = updatedInfo
548+
}
549+
}
550+
val shouldTrack = statistics.enabled && !alreadyTracking
551+
543552
val prevData = macrosStack.headOption.map { prev =>
544553
macroIdsToTimers.getOrElse(
545554
prev.id,
@@ -556,7 +565,10 @@ final class ProfilingImpl[G <: Global](
556565
val searchPrefix = s" macro ${macroId}"
557566
val macroTimer = registerTyperTimerFor(searchPrefix)
558567
macroIdsToTimers += ((macroId, macroTimer))
559-
val start = statistics.startTimer(macroTimer)
568+
val start = {
569+
alreadyTracking = true
570+
statistics.startTimer(macroTimer)
571+
}
560572

561573
val entry = MacroEntry(macroId, pt, start, None)
562574

@@ -573,6 +585,11 @@ final class ProfilingImpl[G <: Global](
573585
macrosStack = entry :: macrosStack
574586
try super.apply(desugared)
575587
finally {
588+
if (shouldTrack) {
589+
alreadyTracking = false
590+
updateExpansionTime(desugared, start)
591+
}
592+
576593
val children = macroChildren.getOrElse(macroId, Nil)
577594
if (config.generateMacroFlamegraph) {
578595
// Complete stack names of triggered implicit searches
@@ -714,9 +731,9 @@ final class ProfilingImpl[G <: Global](
714731

715732
trait ProfilingStats {
716733
val global: Global
717-
import global.statistics.{newSubCounter, macroExpandCount, implicitSearchCount}
734+
import global.statistics.{newSubCounter, macroExpandCount, implicitSearchCount, newTimer}
718735
macroExpandCount.children.clear()
719-
736+
final val preciseMacroTimer = newTimer("precise time in macroExpand")
720737
final val failedMacros = newSubCounter(" of which failed macros", macroExpandCount)
721738
final val delayedMacros = newSubCounter(" of which delayed macros", macroExpandCount)
722739
final val suppressedMacros = newSubCounter(" of which suppressed macros", macroExpandCount)

0 commit comments

Comments
 (0)