Skip to content

[js_interop/dart2wasm] Canonicalize and move <constant-or-literal>.toJS at compile-time to mjs file #56046

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

Open
mkustermann opened this issue Jun 19, 2024 · 2 comments
Labels
area-dart2wasm Issues for the dart2wasm compiler. type-performance Issue relates to performance or code size web-js-interop Issues that impact all js interop

Comments

@mkustermann
Copy link
Member

We should evaluate whether it makes sense for dart2wasm recognize <constant-or-literal>.toJS expressions and canonicalize & move those objects at compile-time to JS (by emitting them into the ...mjs file and importing those external references) instead of lazily at runtime.

It may be very common to have code such as

@JS()
external JSObject foo(JSString value);

main() {
  foo('foo'.toJS);
}

Currently one may be tempted to cache such manually at the top-level via

@JS()
external JSObject foo(JSString value);

final jsFooArg = 'foo'.toJS;
main() {
  foo(jsFooArg);
}

but this compiles to worse code in dart2js (see #56045)

/cc @srujzs

@mkustermann mkustermann added the area-dart2wasm Issues for the dart2wasm compiler. label Jun 19, 2024
@srujzs srujzs added the web-js-interop Issues that impact all js interop label Jun 25, 2024
@mkustermann mkustermann added the type-performance Issue relates to performance or code size label Feb 11, 2025
@rakudrama
Copy link
Member

They don't necessarily need to be moved all the way to the mjs file.
'foo'.toJS could be hoisted to a static scope.
Most users are not going to write code like the jsFooArg example, but we could do that automatically.

It would be more powerful to treat this more like 'boxing' and permit CSE, DCE, LICM to effect hoisting

  • out of a loop
  • out of a closure into its context chain
  • to a static scope lazy initializer when the inputs are static scope lazy initialized.

@mkustermann
Copy link
Member Author

This issue isn't that relevant anymore, because we no longer have One/TwoByteStrings and don't have to copy across the boundary. Thay are also no longer in mjs file

The only thing to optimize would be boxes, but in many cases binaryen may already take care of this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart2wasm Issues for the dart2wasm compiler. type-performance Issue relates to performance or code size web-js-interop Issues that impact all js interop
Projects
Status: No status
Development

No branches or pull requests

3 participants