@@ -24,15 +24,27 @@ class OptimizerBenchmark {
2424 def setup (): Unit = {
2525 val (allFiles, ev) = MainBenchmark .findFiles()
2626 this .inputs = allFiles.map { case (p, s) =>
27- fastparse.parse(s, new Parser (p, true , mutable.HashMap .empty, mutable.HashMap .empty).document(_)) match {
27+ fastparse.parse(
28+ s,
29+ new Parser (p, true , mutable.HashMap .empty, mutable.HashMap .empty).document(_)
30+ ) match {
2831 case Success (v, _) => v
2932 case f : fastparse.Parsed .Failure =>
3033 throw new Exception (s " Failed to parse ${p}: ${f.msg}" )
3134 }
3235 }
3336 this .ev = ev
34- val static = inputs.map {
35- case (expr, fs) => ((new StaticOptimizer (ev,_ => None , new Std ().Std , mutable.HashMap .empty, mutable.HashMap .empty)).optimize(expr), fs)
37+ val static = inputs.map { case (expr, fs) =>
38+ (
39+ (new StaticOptimizer (
40+ ev,
41+ _ => None ,
42+ new Std ().Std ,
43+ mutable.HashMap .empty,
44+ mutable.HashMap .empty
45+ )).optimize(expr),
46+ fs
47+ )
3648 }
3749 val countBefore, countStatic = new Counter
3850 inputs.foreach(t => assert(countBefore.transform(t._1) eq t._1))
@@ -45,53 +57,66 @@ class OptimizerBenchmark {
4557 @ Benchmark
4658 def main (bh : Blackhole ): Unit = {
4759 bh.consume(inputs.foreach { case (expr, fs) =>
48- bh.consume((new StaticOptimizer (ev,
49- _ => None ,
50- new Std ().Std , mutable.HashMap .empty, mutable.HashMap .empty)).optimize(expr))
60+ bh.consume(
61+ (new StaticOptimizer (
62+ ev,
63+ _ => None ,
64+ new Std ().Std ,
65+ mutable.HashMap .empty,
66+ mutable.HashMap .empty
67+ )).optimize(expr)
68+ )
5169 })
5270 }
5371
5472 class Counter extends ExprTransform {
5573 var total, vals, exprs, arrVals, staticArrExprs, otherArrExprs, staticObjs, missedStaticObjs ,
56- otherObjs, namedApplies, applies, arityApplies, builtin = 0
74+ otherObjs, namedApplies, applies, arityApplies, builtin = 0
5775 val applyArities = new mutable.LongMap [Int ]()
5876 val ifElseChains = new mutable.LongMap [Int ]()
5977 val selectChains = new mutable.LongMap [Int ]()
6078 def transform (e : Expr ) = {
6179 total += 1
62- if (e.isInstanceOf [Val ]) vals += 1
80+ if (e.isInstanceOf [Val ]) vals += 1
6381 else exprs += 1
6482 e match {
6583 case _ : Val .Arr => arrVals += 1
6684 case a : Expr .Arr =>
67- if (a.value.forall(_.isInstanceOf [Val ])) staticArrExprs += 1
85+ if (a.value.forall(_.isInstanceOf [Val ])) staticArrExprs += 1
6886 else otherArrExprs += 1
6987 case _ : Val .Obj => staticObjs += 1
7088 case e : Expr .ObjBody .MemberList =>
71- if (e.binds == null && e.asserts == null && e.fields.forall(_.isStatic)) missedStaticObjs += 1
89+ if (e.binds == null && e.asserts == null && e.fields.forall(_.isStatic))
90+ missedStaticObjs += 1
7291 else otherObjs += 1
7392 case e : Expr .Apply =>
74- if (e.namedNames == null ) {
93+ if (e.namedNames == null ) {
7594 applies += 1
7695 val a = e.args.length
7796 applyArities.put(a.toLong, applyArities.getOrElse(a.toLong, 0 ) + 1 )
7897 } else namedApplies += 1
7998
8099 case _ : Expr .Apply0 | _ : Expr .Apply1 | _ : Expr .Apply2 | _ : Expr .Apply3 => arityApplies += 1
81100 case _ : Expr .ApplyBuiltin | _ : Expr .ApplyBuiltin1 | _ : Expr .ApplyBuiltin2 => builtin += 1
82- case _ =>
101+ case _ =>
83102 }
84103 val ifElseCount = countIfElse(e)
85- if (ifElseCount > 0 ) {
104+ if (ifElseCount > 0 ) {
86105 ifElseChains.put(ifElseCount.toLong, ifElseChains.getOrElse(ifElseCount.toLong, 0 ) + 1 )
87- if (ifElseCount > 1 )
88- ifElseChains.put(ifElseCount.toLong- 1L , ifElseChains.getOrElse(ifElseCount.toLong- 1L , 0 ) - 1 )
106+ if (ifElseCount > 1 )
107+ ifElseChains.put(
108+ ifElseCount.toLong - 1L ,
109+ ifElseChains.getOrElse(ifElseCount.toLong - 1L , 0 ) - 1
110+ )
89111 }
90112 val selectCount = countSelectOnId(e)
91- if (selectCount >= 0 ) {
113+ if (selectCount >= 0 ) {
92114 selectChains.put(selectCount.toLong, selectChains.getOrElse(selectCount.toLong, 0 ) + 1 )
93- if (selectCount > 0 )
94- selectChains.put(selectCount.toLong- 1L , selectChains.getOrElse(selectCount.toLong- 1L , 0 ) - 1 )
115+ if (selectCount > 0 )
116+ selectChains.put(
117+ selectCount.toLong - 1L ,
118+ selectChains.getOrElse(selectCount.toLong - 1L , 0 ) - 1
119+ )
95120 }
96121 rec(e)
97122 }
@@ -102,20 +127,21 @@ class OptimizerBenchmark {
102127 }
103128 def countSelectOnId (e : Expr ): Int = e match {
104129 case Expr .Select (_, x, _) =>
105- val c = countSelectOnId(x)
106- if (c == - 1 ) - 1 else c + 1
130+ val c = countSelectOnId(x)
131+ if (c == - 1 ) - 1 else c + 1
107132 case _ : Expr .ValidId => 0
108- case _ => - 1
133+ case _ => - 1
109134 }
110135 override def toString = {
111- val arities = applyArities.toSeq.sortBy(_._1).map { case (a,b) => s " $a: $b" }.mkString(" , " )
112- val chains = ifElseChains.toSeq.sortBy(_._1).map { case (a,b) => s " $a: $b" }.mkString(" , " )
113- val selChains = selectChains.toSeq.sortBy(_._1).map { case (a,b) => s " $a: $b" }.mkString(" , " )
114- s " Total: $total, Val: $vals, Expr: $exprs, Val.Arr: $arrVals, static Expr.Arr: $staticArrExprs, " +
115- s " other Expr.Arr: $otherArrExprs, Val.Obj: $staticObjs, static MemberList: $missedStaticObjs, " +
116- s " other MemberList: $otherObjs, named Apply: $namedApplies, other Apply: $applies, " +
117- s " ApplyN: $arityApplies, ApplyBuiltin*: $builtin; Apply arities: { $arities}, " +
118- s " if/else chains: $chains, Select/ValidId chains: $selChains"
136+ val arities = applyArities.toSeq.sortBy(_._1).map { case (a, b) => s " $a: $b" }.mkString(" , " )
137+ val chains = ifElseChains.toSeq.sortBy(_._1).map { case (a, b) => s " $a: $b" }.mkString(" , " )
138+ val selChains =
139+ selectChains.toSeq.sortBy(_._1).map { case (a, b) => s " $a: $b" }.mkString(" , " )
140+ s " Total: $total, Val: $vals, Expr: $exprs, Val.Arr: $arrVals, static Expr.Arr: $staticArrExprs, " +
141+ s " other Expr.Arr: $otherArrExprs, Val.Obj: $staticObjs, static MemberList: $missedStaticObjs, " +
142+ s " other MemberList: $otherObjs, named Apply: $namedApplies, other Apply: $applies, " +
143+ s " ApplyN: $arityApplies, ApplyBuiltin*: $builtin; Apply arities: { $arities}, " +
144+ s " if/else chains: $chains, Select/ValidId chains: $selChains"
119145 }
120146 }
121147}
0 commit comments