Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Dart's snapshots and Java bytecode?

Tags:

dart

I've been reading up on Dart snapshots, and they're frequently compared to Smalltalk images. But to me, they sound alot like Java bytecode.

For example:

"A Dart snapshot is just a binary serialization of the token stream, generated from parsing the code. A snapshot is not a "snapshot of a running program", it's generated before the tokens are turned into machine code. So, no program state is captured in a snapshot."

Plus they're cross-platform:

"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."

Am I getting it wrong somewhere?

Sources:
What is the snapshot concept in dart?
http://www.infoq.com/articles/google-dart

like image 999
khoomeister Avatar asked Jan 27 '13 12:01

khoomeister


People also ask

What is meant by Java bytecode?

In computing, Java bytecode is the bytecode-structured instruction set of the Java virtual machine (JVM), a virtual machine that enables a computer to run programs written in the Java programming language and several other programming languages, see List of JVM languages.

What is the difference between binary code and bytecode?

Byte code is referred to as a Portable code. Machine Code: Machine code is a set of instructions that is directly machine-understandable and it is processed by the Central Processing Unit (CPU). Machine code is in binary (0's and 1's) format which is completely different from the byte code and source code.

Why Java bytecode is needed?

Bytecode in Java is the reason java is platform-independent, as soon as a Java program is compiled bytecode is generated. To be more precise a Java bytecode is the machine code in the form of a . class file. A bytecode in Java is the instruction set for Java Virtual Machine and acts similar to an assembler.

Is Java bytecode the same on all computers?

Of course, a different Java bytecode interpreter is needed for each type of computer, but once a computer has a Java bytecode interpreter, it can run any Java bytecode program. And the same Java bytecode program can be run on any computer that has such an interpreter.


2 Answers

Snapshots contain the VM data structures representing the loaded script in a serialized form similar to Smalltalk images. To get a better understanding of what is contained in the snapshot, we should take a look at what the Dart VM creates as it reads the script:

  • Library objects, referring to all top-level structures such as classes or top-level methods and variables.
  • Class objects, containing all objects describing all methods and fields.
  • Script and Tokenstream objects representing all loaded source code.
  • String objects for all used identifiers and string constants in the source code.

This object graph is serialized into a file when generating a snapshot using a format that is architecture agnostic. This allows the Dart VM to deserialize this snapshot file on 32-bit or 64-bit machines and recreate all of the necessary internal VM data structures much quicker than reading the original scripts from a set of files (see John's answer).

To clarify John's answer a bit. The Dart VM does not parse ALL of the source code when generating the snapshot. It only needs to parse the top level of the sources to be able to extract class, method and field definitions as these are represented in the serialized graph. In particular method bodies are not parsed and as it is customary for a scripting language errors will be only reported once control reaches the particular method.

The purpose of Java bytecodes is entirely different as Ladicek points out. You could create a snapshot of the VM data structures in a JVM once the bytecodes are loaded to get a similar effect.

In short: The snapshot contains an efficient representation of all the data structures allocated on the Dart VM heap which are needed to start executing the script.

-Ivan

like image 55
Ivan Posva Avatar answered Sep 21 '22 20:09

Ivan Posva


A Dart snapshot is just a roll up of all source files that has been parsed ahead of time. A Dart snapshot is not similar to a Java bytecode file. A Java bytecode file consists of JVM machine code and is the product of a compile, link, and assembly (into JVM machine code) phase.

A Dart snapshot is a binary file of a Dart program and it's import/part source file dependencies that has been parsed into an abstract syntax tree and rolled into a single file. Executing a Dart snapshot allows for faster startup times because:

  1. Only 1 file must be loaded from disk or off network. In contrast, a non-snapshot Dart program must be fetched, then any imported files must be fetched, and so on. Before each subsequent source file request can be made the previously fetched source file must be parsed to find out if it's referencing more source files. Imagine if your Dart program imported 10 libraries which consisted of 10 source files each. That means 110 I/O requests and parses that are done one after another.
  2. The parsing has been done ahead of time. It's already known to be syntactically correct and ready to be compiled by the Dart VM.

HTH, John

like image 23
Cutch Avatar answered Sep 17 '22 20:09

Cutch