Snapshots in Dart
Written by Siva Annamalai
This article talks about snapshots in Dart—both what they are and how they can make Dart apps start up faster. If you write command-line apps, you should be able to improve their startup time by generating your own snapshots, following the instructions in this article.
If you write web apps, you don’t need to do anything: browsers with the Dart VM can snapshot your app automatically. The next time your web app runs, the browser can retrieve the snapshot from the browser cache and use it to start your app faster.
What is a snapshot?
A snapshot is a sequence of bytes that represents a serialized form of one or more Dart objects. This representation closely corresponds to the way these Dart objects are represented in an isolate’s heap in memory.
The Dart VM uses snapshots for two main reasons:
Speeding up initial startup time for an application. A snapshot of the core libraries and application script typically contains preparsed data for the core libraries and the application script. This means it is not necessary to parse and tokenize the libraries and script during startup.
Passing objects from one isolate to another isolate.
The Dart VM uses the following kinds of snapshots:
A full snapshot, which is a complete representation of an isolate’s heap after it is initialized. This is used by the Dart VM for fast startup and initialization of the entire Dart core library and other libraries such as dart:uri, dart:io, dart:utf, dart:json, dart:isolate, and so on.
A script snapshot, which is a complete representation of an application script in an isolate’s heap after the script is loaded into the isolate, but before the script starts executing. This is used by the Dart VM for fast startup and initialization of applications. For example, the script that starts dart2js uses a pre-created script snapshot of the dart2js application.
An object snapshot. Messaging from one isolate to another is implemented in the Dart VM by creating a snapshot of the Dart object that needs to be sent to the other isolate.
How the browser can use snapshots
A browser that contains the Dart VM uses a full snapshot for fast startup and initialization of the main Dart isolate. This snapshot contains the entire Dart core library and other libraries such as dart:uri, dart:utf, dart:json, dart:isolate, and dart:html.
In addition, the browser could potentially generate a script snapshot of an application that has been loaded and cache it in the browser cache. This cached script snapshot could then be used for subsequent reloads of the application for faster application startup.
How to generate and use script snapshots
You can generate and use script snapshots using the Dart VM (dart).
To generate a script snapshot,
use dart with the
You can use the
to specify the location of packages used in imports
dart [--package_root=path] --snapshot=out-file dart-script-file
--snapshot option writes
a script snapshot of dart-script-file to out-file.
For example, the following command creates
a snapshot of the Dart script
putting it into a file called
dart --snapshot=dart2js.snapshot \ dart-sdk/lib/dart2js/lib/_internal/compiler/implementation/dart2js.dart
To execute a script from its snapshot, specify the snapshot file on the command line:
dart snapshot-file args
Any args you specify are passed to the script.
For example, you can run dart2js like this,
myscript.dart -oout.js as command-line arguments to dart2js:
dart dart2js.snapshot myscript.dart -oout.js
How to generate full snapshots
Read this section if you’re working on one of the rare projects that embed the Dart VM (for example, Dartium). The gen_snapshot tool writes a full snapshot (corelibs, dart:uri, dart:io, dart:utf, dart:json, dart:isolate, …) to out-file:
gen_snapshot [options] --snapshot=out-file
You can use the following options:
Specifies the location of packages used in imports
|--url_mapping=mapping||Provides a URL mapping on the command line for URI resolution during library imports.|
You can find more information about snapshots and how they are implemented by browsing the files in the dart/runtime/vm directory. Start by looking for “Snapshot” in snapshot.h. Note that the code might move as the implementation changes.