From 516009a2f3eccac7d6e4fc497cfd8e71871e6bd2 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 3 Dec 2026 19:19:15 +0000 Subject: [PATCH] Put arguments read once into inlined bodies directly Why: Calls to other modules' functions are inlined in two places. When an argument is a parameter of the caller, inlining waits until the JavaScript is generated, and a pure argument the body reads once takes the parameter's place. Otherwise `lam_pass_remove_alias` inlines the call early and bound every argument to a variable first; it did the same when applying a function directly. Both now substitute like the late path. JS modified (literal and function arguments): Example: let called = [] Ok(3)->Result.forEach(i => called->Array.push(i)) let merged = Array.concatAll([[1], arr]) Before (js): let called = []; let f = i => { called.push(i); }; f(3); function merge(arr) { let arrays = [ [1], arr ]; return [].concat(...arrays); } After (js): let called = []; called.push(3); function merge(arr) { return [].concat([1], arr); } Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- changes/inline-literal-args.polish.md | 1 + compiler/backend/lam_pass_remove_alias.ml | 17 +++++++++---- .../runtime/lib/es6/Stdlib_Option.mjs | 3 +-- .../runtime/lib/es6/Stdlib_Result.mjs | 3 +-- .../runtime/lib/js/Stdlib_Option.cjs | 3 +-- .../runtime/lib/js/Stdlib_Result.cjs | 3 +-- .../inline_arg_order_cross_module_test.mjs | 7 ++---- tests/compiler_tests/src/splice_test.mjs | 24 +++++-------------- .../src/stdlib/Stdlib_ArrayTests.mjs | 4 +--- .../src/stdlib/Stdlib_ObjectTests.mjs | 12 ++++------ .../src/stdlib/Stdlib_ResultTests.mjs | 5 +--- tests/runtime_tests/docstrings/DocTest.res.js | 3 +-- 12 files changed, 34 insertions(+), 51 deletions(-) create mode 100644 changes/inline-literal-args.polish.md diff --git a/changes/inline-literal-args.polish.md b/changes/inline-literal-args.polish.md new file mode 100644 index 0000000000..2e754f864f --- /dev/null +++ b/changes/inline-literal-args.polish.md @@ -0,0 +1 @@ +- Put a literal or a function passed to an inlined function from another module straight into its body when the body uses it once, instead of binding it to a variable first. diff --git a/compiler/backend/lam_pass_remove_alias.ml b/compiler/backend/lam_pass_remove_alias.ml index 655994382d..5c779b0539 100644 --- a/compiler/backend/lam_pass_remove_alias.ml +++ b/compiler/backend/lam_pass_remove_alias.ml @@ -174,10 +174,16 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = | _ -> true) args && Lambda.lfunction_can_be_inlined lfunction -> + (* Like the inlining in [Lam_compile]: an argument without side + effects that the body reads once takes the parameter's place, + instead of a binding of its own *) + let _, param_map = + Lam_closure.is_closed_with_map Set_ident.empty params body + in simpl (record_assigned - (Lam_beta_reduce.propagate_beta_reduce (current_meta ()) params - body args)) + (Lam_beta_reduce.propagate_beta_reduce_with_map (current_meta ()) + param_map params body args)) | _ -> let fn = simpl l1 in let args' = Ext_list.map_sharing args simpl in @@ -259,10 +265,13 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = {ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _} when List.compare_lengths params args = 0 && Lambda.lfunction_can_be_inlined lfunction -> + let _, param_map = + Lam_closure.is_closed_with_map Set_ident.empty params body + in simpl (record_assigned - (Lam_beta_reduce.propagate_beta_reduce (current_meta ()) params body - args)) + (Lam_beta_reduce.propagate_beta_reduce_with_map (current_meta ()) + param_map params body args)) | Lstringswitch (l, sw, d) -> ( let known_constant = match l with diff --git a/packages/@rescript/runtime/lib/es6/Stdlib_Option.mjs b/packages/@rescript/runtime/lib/es6/Stdlib_Option.mjs index 25164dbf2d..51639a4447 100644 --- a/packages/@rescript/runtime/lib/es6/Stdlib_Option.mjs +++ b/packages/@rescript/runtime/lib/es6/Stdlib_Option.mjs @@ -18,8 +18,7 @@ function getOrThrow(x, message) { if (x !== undefined) { return Primitive_option.valFromOption(x); } - let msg = message !== undefined ? message : "Option.getOrThrow called for None value"; - throw new Error(`Panic! ${msg}`); + throw new Error(`Panic! ${message !== undefined ? message : "Option.getOrThrow called for None value"}`); } function mapOr(opt, $$default, f) { diff --git a/packages/@rescript/runtime/lib/es6/Stdlib_Result.mjs b/packages/@rescript/runtime/lib/es6/Stdlib_Result.mjs index 19b29026f6..fa6d577db9 100644 --- a/packages/@rescript/runtime/lib/es6/Stdlib_Result.mjs +++ b/packages/@rescript/runtime/lib/es6/Stdlib_Result.mjs @@ -5,8 +5,7 @@ function getOrThrow(x, message) { if (x.TAG === "Ok") { return x._0; } - let msg = message !== undefined ? message : "Result.getOrThrow called for Error value"; - throw new Error(`Panic! ${msg}`); + throw new Error(`Panic! ${message !== undefined ? message : "Result.getOrThrow called for Error value"}`); } function mapOr(opt, $$default, f) { diff --git a/packages/@rescript/runtime/lib/js/Stdlib_Option.cjs b/packages/@rescript/runtime/lib/js/Stdlib_Option.cjs index ab9beb65f5..279969c61a 100644 --- a/packages/@rescript/runtime/lib/js/Stdlib_Option.cjs +++ b/packages/@rescript/runtime/lib/js/Stdlib_Option.cjs @@ -18,8 +18,7 @@ function getOrThrow(x, message) { if (x !== undefined) { return Primitive_option.valFromOption(x); } - let msg = message !== undefined ? message : "Option.getOrThrow called for None value"; - throw new Error(`Panic! ${msg}`); + throw new Error(`Panic! ${message !== undefined ? message : "Option.getOrThrow called for None value"}`); } function mapOr(opt, $$default, f) { diff --git a/packages/@rescript/runtime/lib/js/Stdlib_Result.cjs b/packages/@rescript/runtime/lib/js/Stdlib_Result.cjs index 50819650e9..f4612e9125 100644 --- a/packages/@rescript/runtime/lib/js/Stdlib_Result.cjs +++ b/packages/@rescript/runtime/lib/js/Stdlib_Result.cjs @@ -5,8 +5,7 @@ function getOrThrow(x, message) { if (x.TAG === "Ok") { return x._0; } - let msg = message !== undefined ? message : "Result.getOrThrow called for Error value"; - throw new Error(`Panic! ${msg}`); + throw new Error(`Panic! ${message !== undefined ? message : "Result.getOrThrow called for Error value"}`); } function mapOr(opt, $$default, f) { diff --git a/tests/compiler_tests/src/inline_arg_order_cross_module_test.mjs b/tests/compiler_tests/src/inline_arg_order_cross_module_test.mjs index 36db81d981..f049c3f3ad 100644 --- a/tests/compiler_tests/src/inline_arg_order_cross_module_test.mjs +++ b/tests/compiler_tests/src/inline_arg_order_cross_module_test.mjs @@ -26,12 +26,9 @@ function bumpThenRead(x) { function readThenCall() { let s = 0; - let next = () => { - s = s + 1 | 0; - return y => y; - }; let x = s; - return next()(x); + s = s + 1 | 0; + return x; } Mocha.describe("Inline_arg_order_cross_module_test", () => { diff --git a/tests/compiler_tests/src/splice_test.mjs b/tests/compiler_tests/src/splice_test.mjs index e658ef1a61..14dcb1bd65 100644 --- a/tests/compiler_tests/src/splice_test.mjs +++ b/tests/compiler_tests/src/splice_test.mjs @@ -25,11 +25,7 @@ Mocha.test("splice_test_static", () => { function dynamic(arr) { let a = []; a.push(1, ...arr); - let arrays = [ - [1], - arr - ]; - Test_utils.eq("File \"splice_test.res\", line 28, characters 5-12", a, [].concat(...arrays)); + Test_utils.eq("File \"splice_test.res\", line 28, characters 5-12", a, [].concat([1], arr)); } dynamic([ @@ -58,14 +54,10 @@ Mocha.test("splice_test_newArr_static", () => { function dynamicNew(arr) { let a = new Array(1, 2, ...arr); - let arrays = [ - [ - 1, - 2 - ], - arr - ]; - Test_utils.eq("File \"splice_test.res\", line 48, characters 5-12", a, [].concat(...arrays)); + Test_utils.eq("File \"splice_test.res\", line 48, characters 5-12", a, [].concat([ + 1, + 2 + ], arr)); } dynamicNew([ @@ -125,11 +117,7 @@ Test_utils.eq("File \"splice_test.res\", line 92, characters 7-14", a, [ function dynamic$1(arr) { let a = []; a.push(1, ...arr); - let arrays = [ - [1], - arr - ]; - Test_utils.eq("File \"splice_test.res\", line 98, characters 7-14", a, [].concat(...arrays)); + Test_utils.eq("File \"splice_test.res\", line 98, characters 7-14", a, [].concat([1], arr)); } dynamic$1([ diff --git a/tests/compiler_tests/src/stdlib/Stdlib_ArrayTests.mjs b/tests/compiler_tests/src/stdlib/Stdlib_ArrayTests.mjs index 605afd1dda..3d2035c4cc 100644 --- a/tests/compiler_tests/src/stdlib/Stdlib_ArrayTests.mjs +++ b/tests/compiler_tests/src/stdlib/Stdlib_ArrayTests.mjs @@ -445,8 +445,6 @@ Test.run([ ] ]); -let arrays$1 = []; - Test.run([ [ "Stdlib_ArrayTests.res", @@ -455,7 +453,7 @@ Test.run([ 39 ], "concatAll - empty" -], [].concat(...arrays$1), eq, []); +], [].concat(), eq, []); Test.run([ [ diff --git a/tests/compiler_tests/src/stdlib/Stdlib_ObjectTests.mjs b/tests/compiler_tests/src/stdlib/Stdlib_ObjectTests.mjs index 82a12bde35..9005222a3e 100644 --- a/tests/compiler_tests/src/stdlib/Stdlib_ObjectTests.mjs +++ b/tests/compiler_tests/src/stdlib/Stdlib_ObjectTests.mjs @@ -478,16 +478,14 @@ runGetTest({ }), get: i => { let opt = i["a"]; - let f = i => i.concat([ - 4, - 5 - ]); - let opt$1 = opt !== undefined ? Primitive_option.some(f(Primitive_option.valFromOption(opt))) : undefined; - let $$default = []; + let opt$1 = opt !== undefined ? Primitive_option.some(Primitive_option.valFromOption(opt).concat([ + 4, + 5 + ])) : undefined; if (opt$1 !== undefined) { return Primitive_option.valFromOption(opt$1); } else { - return $$default; + return []; } }, expected: [ diff --git a/tests/compiler_tests/src/stdlib/Stdlib_ResultTests.mjs b/tests/compiler_tests/src/stdlib/Stdlib_ResultTests.mjs index 7fb6513ce4..a60207f18d 100644 --- a/tests/compiler_tests/src/stdlib/Stdlib_ResultTests.mjs +++ b/tests/compiler_tests/src/stdlib/Stdlib_ResultTests.mjs @@ -8,10 +8,7 @@ let eq = Primitive_object.equal; function forEachIfOkCallFunction() { let called = []; - let f = i => { - called.push(i); - }; - f(3); + called.push(3); Test.run([ [ "Stdlib_ResultTests.res", diff --git a/tests/runtime_tests/docstrings/DocTest.res.js b/tests/runtime_tests/docstrings/DocTest.res.js index ca8f4b9c8e..330ad6cf05 100644 --- a/tests/runtime_tests/docstrings/DocTest.res.js +++ b/tests/runtime_tests/docstrings/DocTest.res.js @@ -56,8 +56,7 @@ async function extractDocFromFile(file) { let e = Primitive_exceptions.internalToException(raw_e); if (e.RE_EXN_ID === "JsExn") { console.error(e._1); - let msg = `Failed to extract code blocks from ${file}`; - throw new Error(`Panic! ${msg}`); + throw new Error(`Panic! ${`Failed to extract code blocks from ${file}`}`); } throw e; }