Inline other modules' functions early when an argument is a parameter
Why
The alias pass inlined a call to another module's @inline function only when no argument was a parameter of the caller. The check dates from the initial export in 2016, when only functors stored their bodies: it kept a functor that applies another functor to its unknown argument from being inlined there, to limit code size. Since late inlining was added, such calls were inlined anyway, only later, while compiling to JavaScript. There the body missed the alias pass, so a function argument was called instead of inlined and a Some(x) argument was bound and unwrapped again. The pass now inlines these calls like all others.
modifiedJS a function argument next to a parameter
let mapped = (opt: option<int>) => opt->Option.map(x => x + 1)js
Before
After
function mapped(opt) {function mapped(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some((x => x + 1 | 0)(opt)); return Primitive_option.some(opt + 1 | 0); } }}}
modifiedJS a literal Some next to a parameter
let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)js
Before
After
function knownSome(x, f) {function knownSome(x, f) { let opt = x; let opt$1 = opt !== undefined ? Primitive_option.some(f(Primitive_option.valFromOption(opt))) : undefined; if (opt$1 !== undefined) { return opt$1; } else { return 0; } return Primitive_option.some(f(x)); }}
Review
- What else changes? The generated JavaScript of the runtime and Belt stays the same. In the tests,
pick(x, y, true)now uses its known flag (inline_static_exit_cross_module_test). - Why does
addBumpedininline_assign_cross_module_testget alet tmp = r? ItsbumpTwiceupdates a local ref, so its stored body assignsr. Inlined late, the JavaScript generation saw that the second copy assigns onlyr$1. Inlined early, the first copy's result is bound to keep the order of evaluation, and the binding is substituted back only ifrcan't change: the Lambda passes only know thatris assigned somewhere, sotmpstays. The output is correct. The next PR in the stack stops storing bodies that update a local ref, sobumpTwiceis called and this case goes away. - Does
Primitive_option.some(f(x))return the right value? Yes: the payload is anint, so it is returned as it is. Not wrapping it at all needs the type of the call's result.