Running dart run <dart-file> is much slower than running dart <dart-file>. This is inconsistent with the dart tool doc and the issue was raised on reddit. It appears the dart run version is doing much more work before starting the app. See comparisons below:
example/hello.dart
void main() {
print('hello');
}
$ time dart run example/hello.dart
hello
real 0m0.630s
user 0m0.432s
sys 0m0.134s
$ time dart example/hello.dart
hello
real 0m0.190s
user 0m0.188s
sys 0m0.073s
$ time pub run example/hello.dart
hello
real 0m0.360s
user 0m0.417s
sys 0m0.127s
Dart SDK version: 2.11.0-240.0.dev (dev) (Tue Oct 20 16:27:23 2020 -0700) on "macos_x64"
cc @bkonyi
(Ben, if you're not subscribed to area-dart-cli you might want to)
This isn't terribly surprising as dart foo.dart takes a different startup path than dart run foo.dart and even dart --observe foo.dart.
dart foo.dart basically acts as a fast path that skips starting the CLI isolate as the VM knows how to handle all the options without deferring to the CLI isolate and doesn't require that isolate to startup any debugging services like DDS. If dart run foo.dart is run instead, the VM sees run, realizes it's a CLI command, and then starts the CLI isolate from a kernel snapshot. Running straight from a kernel snapshot isn't particularly performant when it comes to startup time, especially compared to a pre-trained app-jit snapshot, but we chose to take the performance hit for now due to the extra complexity involved with being able to fall back from an app-jit snapshot to a kernel snapshot when incompatible flags were provided to the VM.
TL;DR: this is a known limitation due to an implementation decision that I'll try to address in the coming quarter.
Thanks @bkonyi. Makes sense and aligns with observation, but we should at least make sure our docs correctly reflect this to avoid developer confusion. Currently the docs at dart: The Dart command-line tool imply the two are identical.
dart <DART_FILE>: Runs a Dart program, just likedart run.
Most helpful comment
Thanks @bkonyi. Makes sense and aligns with observation, but we should at least make sure our docs correctly reflect this to avoid developer confusion. Currently the docs at dart: The Dart command-line tool imply the two are identical.