Skip to content

Commit

Permalink
Merge pull request #49 from danicheg/fix-#44
Browse files Browse the repository at this point in the history
Enrich the debug info with source files for implicit searches by type
  • Loading branch information
danicheg authored Feb 11, 2024
2 parents 2c67d3e + 848d42f commit 2ebc390
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
11 changes: 11 additions & 0 deletions plugin/src/main/scala/ch/epfl/scala/ImplicitSearchDebugInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.epfl.scala

final case class ImplicitSearchDebugInfo private (firings: Int, sourceFiles: List[String])

object ImplicitSearchDebugInfo {
def apply(firings: Int, sourceFiles: List[String]): Option[ImplicitSearchDebugInfo] =
if (firings > 0 && sourceFiles.nonEmpty)
Some(new ImplicitSearchDebugInfo(firings, sourceFiles))
else
None
}
34 changes: 26 additions & 8 deletions plugin/src/main/scala/ch/epfl/scala/ProfilingPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ProfilingPlugin(val global: Global) extends Plugin { self =>

// This is just for displaying purposes
import scala.collection.mutable.LinkedHashMap
private def toLinkedHashMap[K, V](xs: List[(K, V)]): LinkedHashMap[K, V] = {
private def toLinkedHashMap[K, V](xs: Seq[(K, V)]): LinkedHashMap[K, V] = {
val builder = LinkedHashMap.newBuilder[K, V]
builder.++=(xs)
builder.result()
Expand Down Expand Up @@ -146,15 +146,33 @@ class ProfilingPlugin(val global: Global) extends Plugin { self =>
val macrosTypeLines = global.exitingTyper(macrosType.map(kv => kv._1.toString -> kv._2))
logger.info("Macro expansions by type", toLinkedHashMap(macrosTypeLines))

import implementation.{implicitSearchesByPos, implicitSearchesByType}
val implicitSearchesPosition = toLinkedHashMap(implicitSearchesByPos.toList.sortBy(_._2))
val implicitSearchesPosition = toLinkedHashMap(
implementation.implicitSearchesByPos.toList.sortBy(_._2)
)
logger.info("Implicit searches by position", implicitSearchesPosition)
val sortedImplicitSearches = implicitSearchesByType.toList.sortBy(_._2)

val sortedImplicitSearches =
implementation.implicitSearchesSourceFilesByType.toVector
.flatMap {
case (tpe, sourceFiles) =>
val firings = implementation.implicitSearchesByType.getOrElse(tpe, 0)
val files = sourceFiles.toList.flatMap {
case f if f.length > 0 =>
List(f.path)
case _ =>
List.empty
}

ImplicitSearchDebugInfo(firings, files).map(tpe -> _)
}
.sortBy(_._2.firings)
// Make sure to stringify types right after typer to avoid compiler crashes
val stringifiedSearchCounter =
global.exitingTyper(sortedImplicitSearches.map(kv => kv._1.toString -> kv._2))
logger.info("Implicit searches by type", toLinkedHashMap(stringifiedSearchCounter))
()
val stringifiedSortedImplicitSearches =
global.exitingTyper(
sortedImplicitSearches
.map(kv => kv._1.toString() -> kv._2)
)
logger.info("Implicit searches by type", toLinkedHashMap(stringifiedSortedImplicitSearches))
}
}

Expand Down
18 changes: 16 additions & 2 deletions plugin/src/main/scala/ch/epfl/scala/profilers/ProfilingImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import java.nio.file.{Files, Path, StandardOpenOption}

import ch.epfl.scala.PluginConfig
import ch.epfl.scala.profiledb.utils.AbsolutePath
import ch.epfl.scala.profilers.tools.{Logger, QuantitiesHijacker, SettingsOps}

import scala.tools.nsc.Global
import ch.epfl.scala.profilers.tools.{Logger, QuantitiesHijacker, SettingsOps}
import scala.reflect.internal.util.SourceFile

final class ProfilingImpl[G <: Global](
override val global: G,
Expand Down Expand Up @@ -344,6 +345,15 @@ final class ProfilingImpl[G <: Global](
implicitSearchesByType.update(targetType, typeCounter + 1)
val posCounter = implicitSearchesByPos.getOrElse(targetPos, 0)
implicitSearchesByPos.update(targetPos, posCounter + 1)

if (config.showProfiles) {
val sourceFiles =
implicitSearchesSourceFilesByType.getOrElseUpdate(targetType, mutable.HashSet.empty)
if (!sourceFiles.contains(targetPos.source)) {
sourceFiles.add(targetPos.source)
}
}

if (global.analyzer.openMacros.nonEmpty)
statistics.incCounter(implicitSearchesByMacrosCount)

Expand Down Expand Up @@ -715,7 +725,11 @@ trait ProfilingStats {
final val implicitSearchesByMacrosCount = newSubCounter(" from macros", implicitSearchCount)

import scala.reflect.internal.util.Position
final val macrosByType = new scala.collection.mutable.HashMap[global.Type, Int]()
import scala.collection.mutable

final val macrosByType = new mutable.HashMap[global.Type, Int]()
final val implicitSearchesByType = global.perRunCaches.newMap[global.Type, Int]()
final val implicitSearchesByPos = global.perRunCaches.newMap[Position, Int]()
final val implicitSearchesSourceFilesByType =
global.perRunCaches.newMap[global.Type, mutable.HashSet[SourceFile]]()
}

0 comments on commit 2ebc390

Please sign in to comment.