@@ -11,7 +11,7 @@ import Decorators._
11
11
import ast .Trees ._
12
12
import MegaPhase ._
13
13
import StdNames .nme
14
- import Names .TermName
14
+ import Names ._
15
15
import Constants .Constant
16
16
17
17
@@ -28,25 +28,32 @@ class Instrumentation extends MiniPhase { thisPhase =>
28
28
ctx.settings.YinstrumentClosures .value ||
29
29
ctx.settings.YinstrumentAllocations .value
30
30
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
+
31
37
private var consName : TermName = _
32
38
private var consEqName : TermName = _
33
39
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
37
42
ctx
38
- }
39
43
40
44
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}" ))
42
46
ref(defn.Stats_doRecord ).appliedTo(key, Literal (Constant (1 )))
43
47
}
44
48
49
+ private def ok (using Context ) =
50
+ ! ctx.owner.ownersIterator.exists(_.name.toString.startsWith(" Stats" ))
51
+
45
52
override def transformApply (tree : Apply )(using Context ): Tree = tree.fun match {
46
53
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)
50
57
case _ =>
51
58
tree
52
59
}
0 commit comments