Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sjsonnet/src/sjsonnet/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,8 @@ object Val {
def evalRhs(scope: ValScope, ev: EvalScope, fs: FileScope, pos: Position): Val

/**
* Override to expose the function's body AST for pattern detection (e.g. foldl string concat).
* Override to expose the function's body AST for pattern detection (e.g. foldl string concat,
* constant-body makeArray optimization). Returns null by default.
*/
def bodyExpr: Expr = null

Expand Down
18 changes: 13 additions & 5 deletions sjsonnet/src/sjsonnet/stdlib/ArrayModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,19 @@ object ArrayModule extends AbstractFunctionModule {
pos, {
val sz = size.cast[Val.Num].asPositiveInt
val a = new Array[Eval](sz)
val noOff = pos.noOffset
var i = 0
while (i < sz) {
a(i) = new LazyApply1(func, Val.Num(pos, i), noOff, ev)
i += 1
val body = func.bodyExpr
if (func.params.names.length == 1 && body != null && body.isInstanceOf[Val.Literal]) {
// Function body is a constant (e.g. `function(_) 'x'`).
// Skip lazy thunk + Val.Num(index) allocation per element.
val constVal = body.asInstanceOf[Val]
java.util.Arrays.fill(a.asInstanceOf[Array[AnyRef]], constVal)
} else {
val noOff = pos.noOffset
var i = 0
while (i < sz) {
a(i) = new LazyApply1(func, Val.Num(pos, i), noOff, ev)
i += 1
}
}
a
}
Expand Down