Inline @inline functions in other modules without a flag
Why
@inline on a function inlined its calls only in its own module. Other modules inlined it only with -bs-cross-module-opt, which also inlined every small closed function, annotated or not. @inline now means the same in every module: an exported @inline function or functor stores its body in the .cmj, and other modules inline it, flag or not. The Option helpers that #8689 marked are @inline. This is PR 5 of the plan in #8733 (proposal 3) and replaces the compiler part of #8689, without the @inline(crossModule) payload.
modifiedJS another module, without -bs-cross-module-opt
// A.res@inline let g = x => x + 1let h = x => x * 2 // B.reslet a = A.g(3)let b = A.h(4)
js
Before B
After B
let a = A.g(3); let b = A.h(4);let b = A.h(4); let a = 4;
modifiedJS an Option helper
// tests/belt_tests/src/bs_stack_test.reswhile current->Option.isSome { … }
js
Before
After
while (Stdlib_Option.isSome(current)) {while (current !== undefined) {
modifiedRepresentation which bodies a .cmj stores
mapping
Before Lam_stats_export.values_of_export, Js_cmj_format.get_result
After Lam_stats_export.values_of_export, Js_cmj_format.get_result
true, false, null, undefined ↦ alwaystrue, false, null, undefined ↦ always@inline function or functor, closed ↦ with -bs-cross-module-opt@inline function or functor, closed, not async, no directive ↦ alwaysother closed function under size 5, closed functor ↦ with -bs-cross-module-optother closed function under size 5, closed functor ↦ with -bs-cross-module-opt
Review
- What keeps a body from being stored? It must be closed, since it is copied without its environment, not async and without a directive, and it must not call a relative-path external (#9475). A caller that can't load a module the body names calls the function instead (#9478).
- Which functions does this cover? Those defined at the top level of a module. A function in a nested module (
module M = { @inline let f = … }) and an@inline let recare still inlined only in their own module. - Does the caller rebuild when the body changes? Yes, through the changed .cmj (#9473); tests/build_tests/projects/inline_function_rebuild edits a body on the same line, which leaves the .cmi as it is.
- Can a JS global be captured by the import of a module whose uses were all inlined? Not any more. Such a module is still imported when it has side effects, so
Js_pass_external_shadownow counts the modules imported for their side effects as bindings in scope (Lam_compile_env.side_effect_modules, whichpopulate_required_modulesuses too). Before, it saw only the modules the generated code still named and printed the global bare, which then read the import (inline_shadowed_import_test.res). Found by marking every function in tests/tests@inline.