V8: an open source JavaScript engine




Uploaded by Google on 15.09.2008

Transcript:
>> Hi. I'm Lars, and I'm the tech lead for the V8
project, and the V8 project is a new, exciting, high-performance JavaScript engine we built
here at Google. The V8 team is located in Aarhus, Denmark,
but we also have a few people working on the project here from
JavaScript was written and designed to be a way of customizing conversation inside the
browser, so if you press the button, you could do different things.
So it allowed the programmer to change the behavior of the browser.
In the beginning, it was only meant to be a small piece of code you added to your Web
application. One of the interesting properties of JavaScript
and the reason why it's being used today is that it's platform-independent.
So you can run on Mac OS or Windows and use the same application inside the browser.
Let me talk about the three key design decisions we did in V8.
The first one is hidden classes. The second one is a generated code.
And the third one is the efficient memory management system we have.
So first I want to show you how we have introduced hidden classes in our system.
As you all know, JavaScript is prototype-based, and if you have a function here called Point,
where we create a new optic and assign X and Y to the parameters, and here, the last two
lines, we'll create two different Points, where X and Y is zero and one, and two and
three. In most JavaScript implementation, you will
have two independent optics that will not share any common structure.
That's different in V8. So here's a drawing of what we do in V8.
You have the two different optics, A and B, and instead of having them being completely
independent, like in most JavaScript implementation, we actually make them share a hidden class.
So instead of creating two optics, we create three: A and B, and then the hidden class
for these two Optics. The hidden class will actually explain that
both these two optics have the same number of properties-- namely, two, and then X and
Y.
Now, how are we going to use this? Well, let me show some generated machine code
we do in V8. So another idea in V8 is that we translate
JavaScript code into machine code, instead of just using interpreter to obtain speed.
And let's take an example. If you actually have some application that
uses a Point-- here, we symbolize with point.x-- it reads the X property out of a point.
And here you have the code we actually generate for it.
So this is X86 code, and instead of having a complicated look-up of a property, we can
actually do much better if we have a hidden class structure.
So, as you see in this case, reading an X property of an optic will translate into three
machine instructions. The first instruction will actually check
that the optic has a certain hidden class. If it has that, it will continue to the third
instruction, and then just read the X property out of the optic.
Otherwise, we call a run-time routine. Now, when you're dealing with the code, we
do not know about the hidden classes, so this code here is classical, and we capitalize
on the hidden classes to make it run fast. In essence, instead of having a generic look-up
for a property, we can actually generate efficient machine code to fetch the property out of
the optic, like any other class-based system.
The third thing we wanted to solve in V8 was, we wanted to eliminate garbage collection
process. And so, we designed this efficient memory
management system that allowed fast allocation and also minimal process when running JavaScript.
And that actually means when you, as a user, use JavaScript inside the browser, you will
not get any hiccups with your having tagged your applications.
In particular, if your application is big, then you tend to have lots of process.
We eliminated that by implementing a two-generation-based garbage collector, so that when we reclaim
storage for updates, we do it in small pieces. It sounds like a simple problem, but in JavaScript,
you are always reading in JavaScript, compiling code, creating hidden classes, and so on.
In V8, all these kinds of optics that are created at one time, they reside inside the
optic key, and they are reclaimed incrementally as you execute.
So V8 is an open source project, and Chrome underdog is also including it as part of the
Google Chrome browser. You can also take it as a separate stand-alone
project and embed a new application. So the main purpose of V8 is to raise the
performance bar of JavaScript out there in the marketplace.