@@ -11,7 +11,7 @@ import Decorators._
1111import ast .Trees ._
1212import MegaPhase ._
1313import StdNames .nme
14- import Names .TermName
14+ import Names ._
1515import Constants .Constant
1616
1717
@@ -28,25 +28,32 @@ class Instrumentation extends MiniPhase { thisPhase =>
2828 ctx.settings.YinstrumentClosures .value ||
2929 ctx.settings.YinstrumentAllocations .value
3030
31+ private val namesOfInterest = List (
32+ " ::" , " +=" , " toString" , " newArray" , " box" ,
33+ " map" , " flatMap" , " filter" , " withFilter" , " collect" , " foldLeft" , " foldRight" , " take" ,
34+ " reverse" , " mapConserve" , " mapconserve" , " filterConserve" , " zip" )
35+ private var namesToRecord : Set [Name ] = _
36+
3137 private var consName : TermName = _
3238 private var consEqName : TermName = _
3339
34- override def prepareForUnit (tree : Tree )(using Context ): Context = {
35- consName = " ::" .toTermName
36- consEqName = " +=" .toTermName
40+ override def prepareForUnit (tree : Tree )(using Context ): Context =
41+ namesToRecord = namesOfInterest.map(_.toTermName).toSet
3742 ctx
38- }
3943
4044 private def record (category : String , tree : Tree )(using Context ): Tree = {
41- val key = Literal (Constant (s " $category${tree.sourcePos.show}" ))
45+ val key = Literal (Constant (s " $category@ ${tree.sourcePos.show}" ))
4246 ref(defn.Stats_doRecord ).appliedTo(key, Literal (Constant (1 )))
4347 }
4448
49+ private def ok (using Context ) =
50+ ! ctx.owner.ownersIterator.exists(_.name.toString.startsWith(" Stats" ))
51+
4552 override def transformApply (tree : Apply )(using Context ): Tree = tree.fun match {
4653 case Select (nu : New , _) =>
47- cpy.Block (tree)(record(i " alloc/ ${nu.tpe}@ " , tree) :: Nil , tree)
48- case Select (_, name) if name == consName || name == consEqName =>
49- cpy.Block (tree)(record(" alloc/:: " , tree) :: Nil , tree)
54+ cpy.Block (tree)(record(i " alloc/ ${nu.tpe}" , tree) :: Nil , tree)
55+ case ref : RefTree if namesToRecord.contains(ref. name) && ok =>
56+ cpy.Block (tree)(record(i " call/ ${ref.name} " , tree) :: Nil , tree)
5057 case _ =>
5158 tree
5259 }
0 commit comments