{"id":644,"date":"2018-03-15T15:17:01","date_gmt":"2018-03-15T15:17:01","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=644"},"modified":"2023-05-31T16:00:57","modified_gmt":"2023-05-31T16:00:57","slug":"boost-performance-with-v8-engine-design-choices-study","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/boost-performance-with-v8-engine-design-choices-study\/","title":{"rendered":"Boost Performance with V8 Engine: A Design Choices Study"},"content":{"rendered":"<p>The V8 engine is Google&#8217;s open source, high-performance JavaScript engine written in C++. Alongside Google Chrome, it can also be found in MongoDb\u00a0, Node.js, and many other popular applications.<\/p>\n<p>It\u2019s very interesting to discover what makes V8 so fast and which solutions were used to achieve this goal.<!--more--><br \/>\n<span id=\"more-898\"><\/span><\/p>\n<p><strong>OOP, Design Patterns, and performance.<\/strong><\/p>\n<p>There is an odd conviction among some C\/C++ programmers. They seem to think that\u00a0using OOP and design patterns decrease\u00a0their application performance.\u00a0V8 is a good example to prove that it\u2019s not true. V8 implements many design patterns\u00a0and it\u2019s well-optimized.<\/p>\n<p>Here are some\u00a0 patterns used:<\/p>\n<p><em><strong>Factory<\/strong><\/em><\/p>\n<p>When the javascript engine executes a script, it creates the instances of each variable, function or array encountered.\u00a0JSObject is the parent class of all these kind of objects.<\/p>\n<p>Here\u2019s the list of all classes inheriting from JSObject:<\/p>\n<p><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/v11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-939\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/v11.png\" alt=\"v1\" width=\"348\" height=\"449\" \/><\/a><\/p>\n<p>V8 implements a factory class to\u00a0create the objects needed and the method Factory::NewJsObject is used for this purpose.<\/p>\n<p>Here are all the methods using it:<\/p>\n<div><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/v4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-938\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/v4.png\" alt=\"v4\" width=\"414\" height=\"401\" \/><\/a><\/div>\n<p>The factory is not used directly by the V8 classes, but it\u2019s invoked from the Heap class which adds another indirection to the implementation.<\/p>\n<p><em><strong>Visitor:<\/strong><\/em><\/p>\n<p>As well explained in the visitor wiki page:<\/p>\n<pre>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.<\/pre>\n<p>Like the factory pattern, this pattern adds also some indirections to the implementation. But it makes the code more readable and maintainable.<\/p>\n<p>V8 source code contains many classes implementing the visitor pattern.<\/p>\n<div><a href=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/v21.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-937\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/v21.png\" alt=\"v2\" width=\"414\" height=\"587\" \/><\/a><\/div>\n<p>Even if the V8 developers have\u00a0to optimize its execution, they don\u2019t care if some indirections are added to the code. \u00a0It\u2019s 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\u2019s more related to a micro-optimization. Significant macro\u00a0optimization depends more on your design choices specific to your application needs.<\/p>\n<p><strong>V8 design choices to optimize its execution<\/strong><\/p>\n<p><em>1- Hidden Class and Fast Property Access:<\/em><\/p>\n<p>JavaScript is a dynamic programming language: properties can be added to, and deleted from, objects on the fly. This means an object\u2019s properties are likely to change.<\/p>\n<p>As specified before the JSObject is the parent class of JSFunction which represents a javascript function, or JSValue which represents a javascript value. However, There\u2019s no class inheriting from JSObject representing a Class like Function or Value. 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\u2019s location in memory.<\/p>\n<p>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\u2019s class. Access is simply a matter of a memory load or store, often requiring only a single instruction.<\/p>\n<p>V8 use hidden class concept 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.<\/p>\n<p><em>2- Dynamic machine code generation<\/em><\/p>\n<p>V8 compiles JavaScript source code directly into machine code when it is first executed. There are no intermediate byte codes, no interpreter. Property access is handled by inline cache code that may be patched with other machine instructions as V8 executes.<\/p>\n<p>3- Efficient garbage collector<\/p>\n<p>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:<\/p>\n<ul>\n<li>stops program execution when performing a garbage collection cycle.<\/li>\n<li>processes only part of the object heap in most garbage collection cycles. This minimizes the impact of stopping the application.<\/li>\n<li>always knows exactly where all objects and pointers are in memory. This avoids falsely identifying objects as pointers which can result in memory leaks.<\/li>\n<\/ul>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>Decide to not use the OOP and the\u00a0design patterns because the optimization is your first priority, is maybe a bad idea. You could\u00a0just gain some microseconds and lose the readability and maintainability of your code.<\/p>\n<p>Significant macro\u00a0optimization depends more on your design choices specific to your application needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The V8 engine is Google&#8217;s open source, high-performance JavaScript engine written in C++. Alongside Google Chrome, it can also be found in MongoDb\u00a0, Node.js, and many other popular applications. It\u2019s very interesting to discover what makes V8 so fast and which solutions were used to achieve this goal.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13,5,190,53,189],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cpp","tag-design","tag-google","tag-javascript","tag-v8-engine"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":5,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":1483,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions\/1483"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}