2.2 Optimization Concepts Hidden Class Inlining Cache
Hidden Class
Hidden Class Javascript Prototype based o0.__proto__.__proto__ Change type at runtime => Hard to track object & variables Hidden class Internal representation of the type system => Fast property access
function Point (x, y) { this .x = x; this .y = y; } const p1 = new Point( 12 , 3 ); const p2 = new Point( 5 , 9 ); const p3 = new Point( 12 ); const hero = { name : 'Superman' }; Javascript Code
Hidden Class function Point (x, y) { this .x = x; this .y = y; }
Hidden Class const p1 = new Point( 13 , 37 ); => this.x = 13
Hidden Class const p1 = new Point( 13 , 37 ); => this.y = 37
Hidden Class const p5 = new Point( 13 , 37 ); p5.a = 'a' ; const p6 = new Point( 13 , 37 ); p6.b = 'b' ;
Hidden Class /* C2 */ const p1 = new Point( 13 , 37 ); /* C1 */ const p2 = new Point( 2 ); /* C0 */ const p3 = new Point(); /* C2 */ const p4 = new Point( 4 , 2 );
Optimization tips Avoid using delete Won’t be backed by hidden class Init object in the same order Same hidden classes, same transitions Avoid adding properties after the object creation If need: Adding in order of props
Inline Cache Monomorphic single shape fastest possible IC Polymorphic more than one shape linear search among cached entries Megamorphic too many shapes slowest ICs, hitting global cache
Optimization tips Prefer Monomorphism than Polymorphism Same method repeatedly over many different method only once
Conclusion
Optimization tips Avoid using delete Init object in the same order Avoid adding properties after the object creation Prefer Monomorphism than Polymorphism Same method repeatedly over many different method only once
Digging Deeper Deeper in Inline Caching Type Feedback, Type Guard Intermediate Representation, Deoptimization Garbage Collection
References http://mrale.ph/blog/2015/01/11/whats-up-with-monomorphism.html http://mrale.ph/blog/2012/06/03/explaining-js-vms-in-js-inline-caches.html https://draft.li/blog/2016/12/22/javascript-engines-hidden-classes/ https://github.com/v8/v8/wiki/Design%20Elements Google I/O 2016, 2017 https://v8project.blogspot.com/