Skip to content

Commit 7f170ba

Browse files
committed
chore: Make aggressive optimization default
1 parent 725f287 commit 7f170ba

File tree

6 files changed

+308
-1029
lines changed

6 files changed

+308
-1029
lines changed

sjsonnet/src/sjsonnet/Settings.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ final case class Settings(
1313
brokenAssertionLogic: Boolean = false,
1414
maxMaterializeDepth: Int = 1000,
1515
materializeRecursiveDepthLimit: Int = 128,
16-
maxStack: Int = 500,
17-
/**
18-
* Enable aggressive static optimizations in the optimization phase, including: constant folding
19-
* for arithmetic, comparison, bitwise, and shift operators; branch elimination for if-else with
20-
* constant conditions; short-circuit elimination for And/Or with constant lhs. These reduce AST
21-
* node count, benefiting long-running Jsonnet programs.
22-
*/
23-
aggressiveStaticOptimization: Boolean = false
16+
maxStack: Int = 500
2417
)
2518

2619
object Settings {

sjsonnet/src/sjsonnet/StaticOptimizer.scala

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
sjsonnet.Error: Max stack frames exceeded.
2-
at [<root>].(error.recursive_import.jsonnet:17:1)
2+
at [<root>].(error.recursive_import.jsonnet:17:15)

0 commit comments

Comments
 (0)