Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions from other modules are never inlined #1065

Open
rniii opened this issue Feb 14, 2024 · 2 comments
Open

Functions from other modules are never inlined #1065

rniii opened this issue Feb 14, 2024 · 2 comments

Comments

@rniii
Copy link

rniii commented Feb 14, 2024

As an example, here's a function composition operator:

let (>>) f g x = f (g x)

Using it from the same file as it is defined always gets the function inlined --

let logPlusTwo = Js.log >> ((+) 2)
function logPlusTwo(param) {
  console.log(2 + param | 0);
}

But when moving the operator to something like an Utils module, the inlining never happens, which makes the output unnecessarily large. Explicitly annotating it with [@inline] also doesn't seem to do anything.

Is this a limitation with the build system working file-by-file? If so, is there any workaround?

@anmonteiro
Copy link
Member

Is this a limitation with the build system working file-by-file? If so, is there any workaround?

Yeah, effectively. We have limited, experimental support for cross-module optimizations. Try adding this in your dune file:

({melange.,}compile_flags :standard --mel-cross-module-opt)

@anmonteiro
Copy link
Member

anmonteiro commented Jan 13, 2025

I added a fairly comprehensive test for this in #1291. We could make a few improvements:

  • perform compiler optimizations after inlining the cross-module function (would likely eliminate some of the calls to Curry
  • store the relevant information in the .cmj even if --mel-cross-module-opt isn't passed in, such that libraries don't need to explicitly pass that flag (and optimizations can still be had in melange.emit without changing upstream libraries)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants