It’s very interesting to discover what makes V8 so fast and which solutions were used to achieve this goal.
OOP, Design Patterns and performance.
There is an odd conviction among some C/C++ programmers. They seem to think that using OOP and design patterns decrease their application performance. V8 is a good example to prove that it’s not true. V8 implements many design patterns and it’s well-optimized.
Here are as example two patterns used:
Here’s the list of all classes inheriting from JSObject:
V8 implements a factoy class to create the objects needed and the method Factory::NewJsObject is used for this purpose.
Here are all the methods using it:
The factory is not used directly by the V8 classes, but it’s invoked from the Heap class which add another indirection to the implementation.
As well explained in the visitor wiki page:
The visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to follow the open/closed principle.
Like the factory pattern, this pattern add also some indirections to the implementation. But it makes the code more readable and maintainable.
V8 source code contains many classes implementing the visitor pattern.
Even if the V8 developers have to optimize its execution, they don’t care if some indirections are added to the code. It’s true that using design patterns and some C++ mechanisms could have an optimization impact due to these added indirections generated by their implementations. But it’s more related to a micro optimization. Significant macro optimization depends more on your design choices specific to your application needs.
V8 design choices to optimize its execution
1- Hidden Class and Fast Property Access:
2- Dynamic machine code generation
3- Efficient garbage collector
V8 reclaims memory used by objects that are no longer required in a process known as garbage collection. To ensure fast object allocation, short garbage collection pauses, and no memory fragmentation V8 employs a stop-the-world, generational, accurate, garbage collector. This means that V8:
- stops program execution when performing a garbage collection cycle.
- processes only part of the object heap in most garbage collection cycles. This minimizes the impact of stopping the application.
- always knows exactly where all objects and pointers are in memory. This avoids falsely identifying objects as pointers which can result in memory leaks.
Decide to not use the OOP and the design patterns because the optimization is your first priority, is maybe a bad idea. You could just gain some micro seconds and lose the readability and maintainability of your code.