It's very interesting to discover how this engine is implemented and what lessons we can take when analyzing it.
here's some remarks about this engine:
Optimization is also a design issue
The goal of this engine is to be very fast and the decision of V8 team to choose C++ proof that performance depends also on design choices more than language choice.
There’s some V8 Design Choices:
To answer to this question let’s search in V8 source code all objects kind treated, for that we can execute the following request:
SELECT TYPES WHERE DeriveFrom "v8.internal.JSObject"
Dynamic machine code generation:
let's discover the dependency graph for MakeCode function.
V8 use an efficient garbage collector to manage memory, and I was very interested to know if I can use it easily in other C++ projects, for that I have to look if the garbage collector classes are highly coupled with other V8 classes or not.
An interesting question is to know if we can use MarkCompactCollector in other C++ projects.
for that let’s search for indirect dependencies for this class:
SELECT TYPES WHERE IsUsedBy "v8.internal.MarkCompactCollector"
Unfortunately this class is highly coupled with a lot of other V8 classes.
V8 is optimized and use design patterns:
I dont know why many developpers think that using OOP and design patterns can impact a lot the optimization.
V8 is an example of project that use OOP and patterns and in the same time very optimized.
here's for example two patterns used:
The factoy class create many objects needed and do abstraction of garbage collector invocation, for example let's search for methods invoked by Factory::NewJsObject
SELECT METHODS WHERE IsDirectlyUsedBy "v8.internal.Factory.NewJSObject(Handle,PretenureFlag)"
as we can observe this method do some abstraction of memory allocation.
V8 contains many classes implementing visitor pattern.
Make C++ easy to use:
Memory allocation simplified
Managing memory and allocating pointers is complicated in C++ Using garbage collector can simplify this task.
Avoid multiple inheritance
Let's search for classes with multiple base classes:
SELECT TYPES WHERE NbBaseClass >1
and the result of this query is empty so V8 dont use multiple inheritance, it simplify a lot the developement and evolution of application.
Use RTTI carefully
Using RTTI resolve some problems but can be an indicator of a bad design, be sure to no overuse this technique.
Let's search if V8 use dynamic_cast:
SELECT METHODS WHERE IsDirectlyUsing "Keywords.dynamic_cast"
the result is empty and it's an indicator from others that V8 is well designed.
Using exceptions is interesting but is not easy that a code be exception safe.
V8 team choose to not use exceptions to simplify the implementation.
we can execute the following CQL request to know if exception is used:
SELECT METHODS WHERE IsDirectlyUsing "Keywords.throw"