Run-time of Node.js: V8 JavaScript Engine

30
Run-time of Node.js V8 JavaScript Engine Gary 2014/2/26

Transcript of Run-time of Node.js: V8 JavaScript Engine

Page 1: Run-time of Node.js: V8 JavaScript Engine

Run-time of Node.jsV8 JavaScript Engine

Gary

2014/2/26

Page 2: Run-time of Node.js: V8 JavaScript Engine

Outline

• Overview of Node.js

• V8 JavaScript Engine• Fast property access

• Dynamic machine code generation

• Efficient Garbage Collection

• Hello World Demo

Page 3: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• Node.js• Node.js was created by Ryan Dahl starting in 2009.

• Dahl was inspired to create Node.js after seeing a file upload progress bar on Flickr.

• The browser did not know how much of the file had been uploaded and had to query the web server.

• Dahl wanted an easier way.

Page 4: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• Old School JavaScript

JavaScript Engine

Web Browser

Operation System

Hardware

Page 5: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• Without Browser

JavaScript Engine

Operation System

Hardware

Page 6: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• With Node.js

JavaScript Engine

Node.js API

Operation System

Hardware

Page 7: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• More third Party Modules

JavaScript Engine

Node.js API

Operation System

Hardware

Mo

du

leD

Mo

du

leC

Mo

du

leA

Mo

du

leB

Page 8: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• More Supports

JavaScript Engine

Node.js API

Operation System

Hardware

Mo

du

leD

Mo

du

leC

Mo

du

leA

Mo

du

leB

Cloud Service

Database(MySQL/PostgreSQL/

MongoDB…etc)

Libraries

JavaC/C++Objective C

Page 9: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• Node.js• A platform built on Chrome’s JavaScript runtime for easily building fast,

scalable network applications.

• Uses an event-driven, non-blocking I/O model that makes it lightweight and efficient

• Perfect for data-intensive real-time applications that run across distributed devices

• Contains a built-in HTTP server library

• Making it possible to run a web server without the use of external software, such as Apache or Lighttpd

• Allowing more control of how the web server works

Page 10: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• There are four building blocks that constitute Node. • Node encapsulates libuv to handle asynchronous events

• Libuv is what abstracts away all of the underlying network and file system functionality on both Windows and POSIX-based systems like Linux, Mac OSX and Unix.

• Google’s V8 to provide a run-time for JavaScript.

• The core functionality of Node, modules like Assert, HTTP, Crypto etc, reside in a core library written in JavaScript.

• The Node bindings, written in C++, provide the glue connecting these technologies to each other and to the operating system.

Page 11: Run-time of Node.js: V8 JavaScript Engine

Overview of Node.js

• Node.js architecture

Node bindings

V8

Node Standard library

libuv

JavaScript

C/C++

Page 12: Run-time of Node.js: V8 JavaScript Engine

V8 JavaScript Engine

• The V8 JavaScript Engine is an open source JavaScript engine developed by Google for Google Chrome web browser.

• The first version of the V8 engine was released at the same time as the first version of Chrome, September 2, 2008

• There are three key areas to V8’s performance• Fast property access

• Dynamic machine code generation

• Efficient Garbage Collection

Page 13: Run-time of Node.js: V8 JavaScript Engine

V8 JavaScript Engine

Note: Google is forking webkit to create a new rendering engine, called Blink, for chrome and opera, at April 2013

Page 14: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• JavaScript is a dynamic programming language

• Properties can be added to, and deleted from, objects on the fly.

• This means an object's properties are likely to change.

• Most JavaScript engines use a dictionary-like data structure as storage for object properties - each property access requires a dynamic lookup to resolve the property's location in memory.

• This approach makes accessing properties in JavaScript typically much slower than accessing instance variables in programming languages like Java and Smalltalk.

• In these languages, instance variables are located at fixed offsets determined by the compiler due to the fixed object layout defined by the object's class.

• Access is simply a matter of a memory load or store, often requiring only a single instruction.

Page 15: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• Difference between dynamic programming language and static programming language

Page 16: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

Page 17: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• To reduce the time required to access JavaScript properties, V8 does not use dynamic lookup to access properties.

• Instead, V8 dynamically creates hidden classes behind the scenes.

• In V8, an object changes its hidden class when a new property is added.

Page 18: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• When new Point(x, y) is executed a new Point object is created.

• When V8 does this for the first time, V8 creates an initial hidden class of Point, called C0 in this example.

• As the object does not yet have any properties defined the initial class is empty.

• At this stage the Point object's hidden class is C0.

function Point(x, y){this.x = x;this.y = y;

}

Page 19: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• Executing the first statement in Point (this.x = x;) creates a new property, x, in the Point object.

• In this case, V8:

Page 20: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• Executing the second statement in Point (this.y = y;) creates a new property, y, in the Point object.

• In this case, V8:

Page 21: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• It might seem inefficient to create a new hidden class whenever a property is added.

• However, because of the class transitions the hidden classes can be reused.

• The next time a new Point is created no new hidden classes are created, instead the new Point object shares the classes with the first Point object.

Page 22: Run-time of Node.js: V8 JavaScript Engine

V8 – Fast Property Access

• For example, if another Point object is created:• initially the Point object has no properties so the newly created object refers

to the intial class C0.

• when property x is added, V8 follows the hidden class transition from C0 to C1 and writes the value of x at the offset specified by C1.

• when property y is added, V8 follows the hidden class transition from C1 to C2 and writes the value of y at the offset specified by C2.

Page 23: Run-time of Node.js: V8 JavaScript Engine

V8 – Dynamic Machine Code Generation

• V8 compiles JavaScript source code directly into machine code when it is first executed.

• There are no intermediate byte codes, no interpreter.

• For example, the JavaScript code to access property x from a Point object is:

• In V8, the machine code generated for accessing x is:

point.x

# ebx = the point objectcmp [ebx,<hidden class offset>],<cached hidden class>jne <inline cache miss>mov eax,[ebx, <cached x offset>]

Page 24: Run-time of Node.js: V8 JavaScript Engine

V8 – Dynamic Machine Code Generation

Page 25: Run-time of Node.js: V8 JavaScript Engine

V8 – Dynamic Machine Code Generation

Page 26: Run-time of Node.js: V8 JavaScript Engine

V8 – Dynamic Machine Code Generation

Page 27: Run-time of Node.js: V8 JavaScript Engine

V8 – Efficient Garbage Collection

• Stop-the-world • Garbage collectors completely halt execution of the program to run a

collection cycle, thus guaranteeing that new objects are not allocated and objects do not suddenly become unreachable while the collector is running.

• Accurate• Always knows exactly where all objects and pointers are in memory. This

avoids falsely identifying objects as pointers which can result in memory leaks.

Page 28: Run-time of Node.js: V8 JavaScript Engine

V8 – Efficient Garbage Collection

• Generational• Each objects is divided into generations (based on the hypothesis about the

object) and places them into a memory heap for a particular generation. When that memory heap is filled up, the GC cycle begins, and those objects that still references are moved to another memory heap and fresh objects are added.

Page 29: Run-time of Node.js: V8 JavaScript Engine

Hello World Demo

var ip = "127.0.0.1";var port = 1337;var http = require('http');

http.createServer(function (req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Hello World\n');

}).listen(port, ip);

console.log("Server running at http://" + ip + ":" + port);

Page 30: Run-time of Node.js: V8 JavaScript Engine

Conclusion

• More recently, following the arrival of AJAX, JavaScript has become a central technology for implementing web-based applications such as GMail.

• JavaScript programs have grown from a few lines to several hundred kilobytes of source code. While JavaScript is very efficient in doing the things it was designed to do, performance has become a limiting factor to further development of web-based JavaScript applications.

• V8 is a new JavaScript engine specifically designed for fast execution of large JavaScript applications.