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

[js_interop/dart2js] Constant fold string literals .toJS in dart2js #56045

Open
mkustermann opened this issue Jun 19, 2024 · 3 comments
Open
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js web-js-interop Issues that impact all js interop

Comments

@mkustermann
Copy link
Member

The following example

import 'dart:js_interop';
import 'dart:js_interop_unsafe';

@JS()
external JSObject eval(JSString expression);

JSString lookupKey(JSObject jsObject) => jsObject.getProperty(kKey);

final JSString kKey = 'key'.toJS;

main() {
  final jsMap = eval('{key: "value"}'.toJS);
  print(lookupKey(jsMap).toDart);
}

compiles (with dart compile js --no-minify -O4 -o repro.js repro.dart) to:

...
  (function lazyInitializers() {
    var _lazyFinal = hunkHelpers.lazyFinal;
    ...
    _lazyFinal($, "kKey", "$get$kKey", () => "key");
  })();
...
  main() {
    A.printString(self.eval('{key: "value"}')[$.$get$kKey()]);
  }
...

When instead manually inlining this in dart to

import 'dart:js_interop';
import 'dart:js_interop_unsafe';

@JS()
external JSObject eval(JSString expression);

JSString lookupKey(JSObject jsObject) => jsObject.getProperty('key'.toJS);

main() {
  final jsMap = eval('{key: "value"}'.toJS);
  print(lookupKey(jsMap).toDart);
}

then this becomes

    main() {
      A.printString(self.eval('{key: "value"}').key);
    }

/cc @srujzs @rakudrama

@dart-github-bot
Copy link
Collaborator

Labels: area-web, type-bug
Summary: The toJS method on string literals is not being constant folded in dart2js, resulting in unnecessary JavaScript code generation. This leads to larger and less efficient code compared to manually inlining the string literal.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jun 19, 2024
@rakudrama
Copy link
Member

Constant-folding of final top-level/static variables is mostly done by CFE.
final foo = 123; is handled somewhat like const foo = 123; because dart2js can see the RHS is a constant in the Kernel IR.
Unfortunately, dart2js does not compile using a full callgraph-bottom-up schedule, so the non-const initializer expression that reduces to a constant it not seen by the users of the variable.

We could do something more limited if this is a popular code pattern./

@mkustermann
Copy link
Member Author

We could do something more limited if this is a popular code pattern./

The reason I used that pattern in a benchmark is because on dart2wasm this is not a NOP and we'd do the key conversion every single time we do a map lookup. So it makes sense to hoist it there to top-level which then regresses dart2js.

If we recognize this specially in dart2wasm compiler (see #56046) then using the pattern inline in code (rather than top-levels) would not be needed.

Though any 3rd party user may also think it's more efficient to cache in a top level instead of executing every time - and it would be worse performance. So I think maybe it's a good idea to do this optimization regardless.

@fishythefish fishythefish added web-dart2js web-js-interop Issues that impact all js interop labels Jun 20, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js web-js-interop Issues that impact all js interop
Projects
Status: No status
Development

No branches or pull requests

5 participants