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.19k stars 1.57k forks source link

Specify Dart liveness guarantees #45914

Open jacob314 opened 3 years ago

jacob314 commented 3 years ago

Currently Dart2JS and the Dart VM diverge in what variables are captured by a closure. The Dart2JS behavior better matches user intuition on what a closure should capture and is less likely to introduce surprising memory leaks. The DartVM behavior leads to fewer context allocations and may have significant runtime performance benefits. Very few Dart users understand what closures capture as shown by https://github.com/flutter/flutter/issues/79605. If the DartVM behavior can be made to match the Dart2JS behavior without a regression of Flutter app performance, that would greatly reduce hard to debug Flutter memory leaks. If not, it might be beneficial to align Dart2JS with DartVM to improve consistency of Flutter Web and Flutter Native.

As mentioned by @rmacnak-google in https://github.com/dart-lang/sdk/issues/45455#issuecomment-831474391_, debuggers would prefer a solution that kept even more variables alive than the Dart VM. Such a solution would introduce significant memory leaks in Flutter apps and would only be interesting for users debugging logic not performance issues. An intermediate solution for debuggers would be to to make debuggers more clearly surface which variables are live.

The lack of specification is more of an issue now that Flutter code can run on the VM and Dart2JS and Dart is looking to add WeakRef support which makes finalization behavior more visible to user code . Flutter code that leaks badly on the VM may not leak on Dart2JS.

mit-mit commented 3 years ago

cc @mraleph @sigmundch