dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.2k stars 1.57k forks source link

[dart2js] Evaluate more initializers eagerly #45681

Open fishythefish opened 3 years ago

fishythefish commented 3 years ago

The scope visitor currently yields an EvaluationComplexity of lazy for many non-constant AST nodes, regardless of their internal details. In some cases, this pessimizes code because the overhead of the lazy evaluation mechanism dwarfs the cost of eagerly evaluating the initializer. We should investigate where improvements can be made.

/cc @rakudrama @johnniwinther

rakudrama commented 2 years ago

See #48442 for an example where we can't evaluate map and set literals eagerly.

It might be useful to decouple the lazy/eager analysis from the scope visitor. The scope visitor is too early to make a good decision. Probably the best point is in SSA just before generating JavaScript for the initializer, as the execution cost and implicit side-effects will then be most apparent. This deferred decision would need support at link-time, with the lazy static load being generated as a deferred JavaScript expression that is then finalized as a property or variable access for the eager case, or a call for the true lazy case.