You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 81d63a8
Browse filesBrowse the repository at this point in the historyBrowse files
Use constant arguments directly when inlining a function in JavaScript (#8942)
* Use constant arguments directly when inlining a function in JavaScript
Why:
When the JavaScript pass inlines a function called once, it replaced a
parameter by a variable argument but bound any other argument with
`let`, so a constant argument stayed a variable nothing folds away.
The pass now also substitutes numbers, booleans, `undefined` and `null`,
the constants the Lambda inliner substitutes too, under the same condition
as variables: the parameter is never assigned by a tail call. Strings and
anything that allocates stay bound once.
JS modified (inlined partial application):
Example:
let g1 = (x, y) => {
let u = x + y
(xx, yy) => xx + yy + u
}
let v = g1(3, 4)(6, ...)
Before (js):
function v(extra) {
let xx = 6;
return (xx + extra | 0) + 7 | 0;
}
After (js):
function v(extra) {
return (6 + extra | 0) + 7 | 0;
}
Review: Can a substituted parameter be assigned? No: a parameter a tail
call assigns is unmasked in the function's immutable mask and stays bound
with `let`, as before for variable arguments (`count` in the new test).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo
Signed-off-by: Christoph Knittel <ck@cca.io>
* Keep only the taken branch of a condition on an inlined constant
Why:
Substituting a boolean argument left conditions such as
`true ? b - a : a - b` and `if (false)` in the inlined body.
When a parameter replaced by a boolean or integer constant is the
condition of a `?:` or an `if`, or is negated, the substitution now keeps
only the branch taken, or yields the boolean, as `Js_stmt_make.if_` does
for constant conditions during code generation. Other conditions and
arithmetic are not folded.
JS modified (condition on a constant argument):
Example:
let logged = {
let log = (verbose, x) => {
let out = []
if verbose {
out->Array.push(Int.toString(x))
}
if !verbose {
out->Array.push("done")
}
out->Array.push(verbose ? "verbose" : "brief")
out
}
log(false, ...)
}
Before (js):
function logged(extra) {
let out = [];
if (false) {
out.push(extra.toString());
}
if (!false) {
out.push("done");
}
out.push(false ? "verbose" : "brief");
return out;
}
After (js):
function logged(extra) {
let out = [];
out.push("done");
out.push("brief");
return out;
}
Review: Can a declaration in the kept branch clash once the branch is
flattened into the enclosing block? No more than for the constant
conditions `Js_stmt_make.if_` already flattens: names are allocated per
function, so a declaration of the same name elsewhere is renamed.
Review: Why not rebuild through `Js_exp_make.econd`? It treats any
number condition as true, which is wrong for `0.0`, `NaN` and `0n`, so
only boolean and integer constants decide a condition here.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo
Signed-off-by: Christoph Knittel <ck@cca.io>
---------
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
0 commit comments