Inline other modules' functions called through a variable
Why
Since the late inlining went, a call through a variable bound to another module's function, as in let f = M.f or let {f} = module(M), was compiled as a call even when M.f is @inline. The alias pass now remembers which variables are bound to a field of another module and calls that field directly, so the existing case for other modules' functions inlines it. This works for top-level and local bindings alike.
modifiedJS let {f} = module(M) in a function
let orEmpty = (x: option<string>) => { let {getOr} = module(Option) x->getOr("")}
js
Before
After
function orEmpty(x) {function orEmpty(x) { return Stdlib_Option.getOr(x, ""); if (x !== undefined) { return x; } else { return ""; }}}
modifiedJS a top-level let f = M.f
let getOr = Option.getOrlet orZero = (x: option<int>) => x->getOr(0)
js
Before
After
function orZero(x) {function orZero(x) { return Stdlib_Option.getOr(x, 0); if (x !== undefined) { return x; } else { return 0; }}}
Review
- What does
inline_through_alias_testcover? The three shapes,let f = M.f,let {f} = module(M)and a locallet f = M.f, each withOptionfunctions that inline, and a call whose arguments must still run in order.hello_res.mjsis back to its output on master, whereuis neither exported nor used, so only thelengthcall remains.