@@ -9,8 +9,7 @@ import ScopedExprTransform.*
99 * StaticOptimizer performs necessary transformations for the evaluator (assigning ValScope indices)
1010 * plus additional optimizations (post-order) and static checking (pre-order).
1111 *
12- * When `aggressiveStaticOptimization` is enabled, the optimizer additionally performs during the
13- * optimization phase:
12+ * The optimizer performs:
1413 * - Constant folding for arithmetic (+, -, *, /, %), comparison (<, >, <=, >=, ==, !=), bitwise
1514 * (&, ^, |), shift (<<, >>), and unary (!, -, ~, +) operators.
1615 * - Branch elimination for if-else with constant conditions.
@@ -32,8 +31,6 @@ class StaticOptimizer(
3231 extends ScopedExprTransform {
3332 def optimize (e : Expr ): Expr = transform(e)
3433
35- private val aggressiveOptimization = ev.settings.aggressiveStaticOptimization
36-
3734 override def transform (_e : Expr ): Expr = super .transform(check(_e)) match {
3835 case a : Apply => transformApply(a)
3936
@@ -104,18 +101,8 @@ class StaticOptimizer(
104101 if (binds == null && asserts == null && allFieldsStaticAndUniquelyNamed)
105102 Val .staticObject(pos, fields, internedStaticFieldSets, internedStrings)
106103 else m
107-
108104 // Aggressive optimizations: constant folding, branch elimination, short-circuit elimination.
109105 // These reduce AST node count at parse time, benefiting long-running Jsonnet programs.
110- case e => if (aggressiveOptimization) tryAggressiveOptimize(e) else e
111- }
112-
113- /**
114- * Aggressive static optimizations that benefit long-running programs by reducing AST size.
115- * Includes: branch elimination, short-circuit elimination, constant folding for arithmetic,
116- * comparison, bitwise, and shift operators.
117- */
118- private def tryAggressiveOptimize (e : Expr ): Expr = e match {
119106 // Constant folding: BinaryOp with two constant operands (most common case first)
120107 case e @ BinaryOp (pos, lhs : Val , op, rhs : Val ) => tryFoldBinaryOp(pos, lhs, op, rhs, e)
121108
@@ -139,8 +126,7 @@ class StaticOptimizer(
139126 case And (pos, _ : Val .False , _) => Val .False (pos)
140127 case Or (pos, _ : Val .True , _) => Val .True (pos)
141128 case Or (_, _ : Val .False , rhs : Val .Bool ) => rhs
142-
143- case _ => e
129+ case e => e
144130 }
145131
146132 private object ValidSuper {
@@ -400,25 +386,20 @@ class StaticOptimizer(
400386 }
401387 case BinaryOp .OP_& =>
402388 (lhs, rhs) match {
403- case (Val .Num (_, _), Val .Num (_, _)) =>
404- Val .Num (
405- pos,
406- (lhs.asInstanceOf [Val .Num ].asSafeLong & rhs
407- .asInstanceOf [Val .Num ]
408- .asSafeLong).toDouble
409- )
389+ case (l : Val .Num , r : Val .Num ) =>
390+ Val .Num (pos, (l.asSafeLong & r.asSafeLong).toDouble)
410391 case _ => fallback
411392 }
412393 case BinaryOp .OP_^ =>
413394 (lhs, rhs) match {
414- case (Val .Num (_, _), Val .Num (_, _) ) =>
415- Val .Num (pos, (lhs.asLong ^ rhs.asLong ).toDouble)
395+ case (l : Val .Num , r : Val .Num ) =>
396+ Val .Num (pos, (l.asSafeLong ^ r.asSafeLong ).toDouble)
416397 case _ => fallback
417398 }
418399 case BinaryOp .OP_| =>
419400 (lhs, rhs) match {
420- case (Val .Num (_, _), Val .Num (_, _) ) =>
421- Val .Num (pos, (lhs.asLong | rhs.asLong ).toDouble)
401+ case (l : Val .Num , r : Val .Num ) =>
402+ Val .Num (pos, (l.asSafeLong | r.asSafeLong ).toDouble)
422403 case _ => fallback
423404 }
424405 case _ => fallback
0 commit comments