Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the snapshot concept in dart?

Tags:

dart

snapshot

I have read that with dart your application can start up to 10x faster because of snapshots. Can anyone explain what it really is and how it works? In what kind of application would i be using snapshots?

like image 836
Gero Avatar asked Oct 13 '12 08:10

Gero


People also ask

What is snapshot in flutter?

A Snapshot simplifies accessing and converting properties in a JSON-like object, for example a JSON object returned from a REST-api service.

What does Snapshot return in flutter?

Snapshot is the result of the Future or Stream you are listening to in your FutureBuilder . Before interacting with the data being returned and using it in your builder, you have to access it first.

What is async snapshot?

AsyncSnapshot<T> class Null safety. Immutable representation of the most recent interaction with an asynchronous computation. See also: StreamBuilder, which builds itself based on a snapshot from interacting with a Stream. FutureBuilder, which builds itself based on a snapshot from interacting with a Future.


1 Answers

Dart's Snapshots are like Smalltalk images in the sense that they allow nearly instant application startup. However, unlike Smalltalk images, Snapshots don't store the program state.

This is especially helpful in slower mobile devices because they are inherently slower and also restricted by memory much more than a desktop system. That reason and the fact that battery usage begs us to close unnecessary programs makes startup speed important.

Dart addresses this issue of slow startup with the heap snapshot feature, which is similar to Smalltalk's image system. The heap of an application is traversed and all objects are written to a simple file. Note: at the moment, the Dart distribution ships with a tool that fires up a Dart VM, loads an application's code, and just before calling main, it takes a snapshot of the heap. The Dart VM can use such a snapshot file to quickly load an application.

The snapshot feature is also used to serialize object graphs that are being sent between Dart Isolates (serialized with SnapshotWriter).

Currently I do not know of any way to initiating a snapshot or dealing with them. In the future, I would expect it to be possible to serve a snapshot file from the web server and having that processed by the browser Dart VM instantaneously.

The snapshot format itself is cross-platform meaning that it works between 32-bit, 64-bit machines and so forth. The format has been made so that it's quick to read into memory with a emphasis on minimizing extra work like pointer fixups.

Here's the source code for snapshot.cc: http://code.google.com/p/dart/source/browse/trunk/dart/runtime/vm/snapshot.cc

and the tests: http://code.google.com/p/dart/source/browse/trunk/dart/runtime/vm/snapshot_test.cc

So the reason why it can speed up an application startup by a factor of 10 is because it's not a bunch of source code like JavaScript that is send as-is and slowly processed afterwards.

And where would you like to use it? Anywhere you possibly can. On the server side, it's basically already happening for you (and doesn't matter really). but on the client-side, that's not possible yet. As I understand it, it will be possible to serve these snapshots to the browser for instant startup, but you really have to wait since it's not available as of now.

like image 61
Kai Sellgren Avatar answered Oct 24 '22 01:10

Kai Sellgren