<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><generator uri="http://jekyllrb.com" version="3.0.3">Jekyll</generator><link href="http://abchatra.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="http://abchatra.github.io/" rel="alternate" type="text/html" /><updated>2016-02-29T19:04:16+00:00</updated><id>http://abchatra.github.io/</id><title>Abhijith Chatra</title><subtitle>Chakra dev</subtitle><entry><title>Inline Cache</title><link href="http://abchatra.github.io/InlineCache/" rel="alternate" type="text/html" title="Inline Cache" /><published>2015-12-29T00:00:00+00:00</published><updated>2015-12-29T00:00:00+00:00</updated><id>http://abchatra.github.io/InlineCache</id><content type="html" xml:base="http://abchatra.github.io/InlineCache/">&lt;p&gt;In this post, we are going to understand the inline cache with respect to Chakra. We will briefly look at the need for inline cache as well. If you haven’t heard of inline cache at all, this &lt;a href=&quot;https://en.wikipedia.org/wiki/Inline_caching&quot;&gt;wiki&lt;/a&gt; page and the &lt;a href=&quot;http://abchatra.github.com/Type&quot;&gt;type&lt;/a&gt; blog are necessary to read before we deep dive. To quote from wiki:
&lt;em&gt;The concept of inline caching is based on the empirical observation that the objects that occur at a particular call site are often of the same type. In those cases, performance can be increased greatly by storing the result of a method lookup “inline”, i.e. directly at the call site.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Note, in this context of blog call site means any property access location (line of code), not just a location where a function is called. First let us understand the cost of property lookup in Chakra.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;&lt;strong&gt;Blog in progress…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;###Cost of property lookup&lt;/p&gt;

&lt;p&gt;Example is the best way to begin:
```js
function Car(make, model) {
  this.make = make;
  this.model = model;
}&lt;/p&gt;

&lt;p&gt;var mycar = new Car(“Honda”, “Accord”);&lt;/p&gt;

&lt;p&gt;function GetModel(machine)
{
  return machine.model;
}
print(GetModel(mycar));
```&lt;/p&gt;

&lt;p&gt;Function &lt;em&gt;GetModel&lt;/em&gt; returns &lt;em&gt;model&lt;/em&gt; property from &lt;em&gt;machine&lt;/em&gt; object. Typically &lt;em&gt;machine&lt;/em&gt; is instance of &lt;em&gt;Car&lt;/em&gt;. Though &lt;em&gt;GetModel&lt;/em&gt; can be invoked from a different type of object such as &lt;em&gt;SewingMachine&lt;/em&gt; and still the contract of code is valid. Sewing machines also have model property.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;SewingMachine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;brand&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mymachine&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;SewingMachine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Brother&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;CS6000i&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;GetModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;mymachine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When the &lt;em&gt;model&lt;/em&gt; property is accessed in script following steps are taken. (Read about &lt;a href=&quot;http://abchatra.github.io/Type&quot;&gt;type&lt;/a&gt; here)&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Fetch the type (machine-&amp;gt;type) of the object.&lt;/li&gt;
  &lt;li&gt;Invoke dictionary lookup in property map ((machine-&amp;gt;type-&amp;gt;typeHandler) to get the slot number corresponding to property &lt;em&gt;model&lt;/em&gt;. Lets say property model is at slot 1;machine-&amp;gt;slots[1]&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 2 is super expensive for runtimes.&lt;/p&gt;

&lt;p&gt;###Dumping bytecode&lt;/p&gt;

&lt;p&gt;Let us dump the bytecode using debug version of ch.exe to see inline caches. Ch.exe is a lightweight console host for hosting ChakraCore. See &lt;em&gt;using ChakraCore&lt;/em&gt; section &lt;a href=&quot;https://github.com/microsoft/chakracore&quot;&gt;here&lt;/a&gt; for how to build ch.exe .&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ch.exe test.js -dump:bytecode

Function GetModel ( (#1.2), #3) (In0, In1) (size: 11 [10])
      5 locals (1 temps from R4), 2 inline cache
  Line  12: return myCar.model;
  Col    6: ^
    0012   ProfiledLdFld        R0 = R3.model #0 &amp;lt;0&amp;gt;
    0016   Br                   x:0021 (   8)
    

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</content><category term="invisible" /><summary>In this post, we are going to understand the inline cache with respect to Chakra. We will briefly look at the need for inline cache as well. If you haven’t heard of inline cache at all, this wiki page and the type blog are necessary to read before we deep dive. To quote from wiki:
The concept of inline caching is based on the empirical observation that the objects that occur at a particular call site are often of the same type. In those cases, performance can be increased greatly by storing the result of a method lookup “inline”, i.e. directly at the call site.

Note, in this context of blog call site means any property access location (line of code), not just a location where a function is called. First let us understand the cost of property lookup in Chakra.</summary></entry><entry><title>Type</title><link href="http://abchatra.github.io/Type/" rel="alternate" type="text/html" title="Type" /><published>2015-12-22T00:00:00+00:00</published><updated>2015-12-22T00:00:00+00:00</updated><id>http://abchatra.github.io/Type</id><content type="html" xml:base="http://abchatra.github.io/Type/">&lt;p&gt;Javascript is an untyped language. &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/Type.h#L22&quot;&gt;Type&lt;/a&gt; in the context of this blog refers to a data structure in the Chakra runtime, which holds additional information about an object. Type is also popularly known as hidden class. Type shares data about a class of objects. The sole purpose of a type in the runtime is to enhance the performance of the user script code. If you are building your own javascript runtime and didn’t care about performance or the memory, you don’t need type.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;Chakra has two kinds of type, static type and dynamic type. The &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/StaticType.h&quot;&gt;static type&lt;/a&gt; is for the primitive object which can’t store properties in it. For example: String (“hello”), Boolean (true or false), Number (10.4) etc. The [Dynamic object] (https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/DynamicObject.h#L46)  which can store properties have &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/DynamicType.h&quot;&gt;dynamic type&lt;/a&gt;. Example: &lt;code class=&quot;highlighter-rouge&quot;&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;or&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;Point()&lt;/span&gt;&lt;/code&gt;. Let us go a bit deeper into static type and dynamic type and see how they share.&lt;/p&gt;

&lt;p&gt;###Static Type
Every &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/RecyclableObject.h#L191&quot;&gt;RecyclableObject&lt;/a&gt; holds a pointer to a type. Every object class in Chakra inherits from RecyclableObject except &lt;a href=&quot;http://abchatra.github.io/TaggedFloat/&quot;&gt;tagged floats&lt;/a&gt;. Let’s take an example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;greeting&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;open source&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Here we have two literal strings &lt;code class=&quot;highlighter-rouge&quot;&gt;greeting&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;message&lt;/code&gt; which Chakra tracks as a &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Library/JavascriptString.h#L50&quot;&gt;JavascriptString&lt;/a&gt;. JavascriptString class inherits from RecyclableObject. Both &lt;code class=&quot;highlighter-rouge&quot;&gt;greeting&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;message&lt;/code&gt; point to a shared type.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2015-12-22-Type-StaticTypesharing.png&quot; alt=&quot;Object Layout&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This type has the &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/EdgeJavascriptTypeId.h#L23&quot;&gt;typeId&lt;/a&gt; set to TypeIds_String and shared between two literal string objects. It may seem an overkill to share an integer typeId when you have to add a pointer to that type in the literal string objects. In reality, type contains a lot of additional data, not just the typeId. Following is the field dump of the type. Prototype and entrypoint are shared as well.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;C++
Js::Type
    typeId    TypeIds_String (7)    Js::TypeId
    flags        
    javascriptLibrary*
    prototype*
    entryPoint*
    propertyCache*
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;JavscriptString type is a static type. Dynamic objects provide a lot more opportunity to share information. Again dynamic objects have a dynamic type.&lt;/p&gt;

&lt;p&gt;###Dynamic Type
Let us take an example first:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Above script creates two &lt;em&gt;Point&lt;/em&gt; objects which have properties x and y. Chakra needs to store following information in the runtime:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Objects one and two have properties x and y&lt;/li&gt;
  &lt;li&gt;Object one has property x whose value is 10 and property y whose value is 20&lt;/li&gt;
  &lt;li&gt;Object two has property x whose value is 40 and property y whose value is 50&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Above information needs to be retrieved when the script wants to get or set the property. Note (1) is shareable among multiple objects. (2) &amp;amp; (3) are specific to individual dynamic objects. A typical way to store this is by building a property map and a slot array.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Property map, maps between a property and a slot number. Example: Property x is present at slot 0.&lt;/li&gt;
  &lt;li&gt;Slot array stores the values. Example slots[0] contains the value of property x.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Property map is stored in a &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/DynamicType.h&quot;&gt;dynamic type&lt;/a&gt; (to be specific, in a type handler which is part of dynamic type). When script does &lt;em&gt;one.x&lt;/em&gt;, Chakra fetches property map in the &lt;em&gt;one-&amp;gt;type&lt;/em&gt; to figure out the slot number corresponding to x which is 0 here. Then it fetches &lt;em&gt;one-&amp;gt;slots[0]&lt;/em&gt; to get the value 10.&lt;/p&gt;

&lt;p&gt;The following diagram illustrates the same.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2015-12-22-Type-ObjectLayout-sharing.png&quot; alt=&quot;Object Layout&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A single type can be shared by all the objects created from the constructor &lt;em&gt;Point&lt;/em&gt;. Even if you create a million &lt;em&gt;Points&lt;/em&gt;, you just need a type to represent them all. Memory saving by type is just a tip of the iceberg. Tons of optimization are possible by the concept of type. I will note a bunch here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Inline Cache&lt;/li&gt;
  &lt;li&gt;Object type specialization in JIT&lt;/li&gt;
  &lt;li&gt;Function specialization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Going deeper into these optimizations necessitates a separate post. First we need to understand how this type is built. &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/DynamicType.h#L28&quot;&gt;Typehanlder&lt;/a&gt; which is part of a dynamic type handles how the property map is stored and how a brand vanilla dynamic object gets its type.&lt;/p&gt;

&lt;p&gt;###Typehandler&lt;/p&gt;

&lt;p&gt;Typehandler in a dynamic type serves two important goals:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Maintains a property map which maps between a property and a slot number.&lt;/li&gt;
  &lt;li&gt;Maintain successor types.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have already seen that property map is to share between objects. But what is a successor type? 
Let us go back to an example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;       
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;       
&lt;span class=&quot;c1&quot;&gt;//Objects one and two points to Type1&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     
&lt;span class=&quot;c1&quot;&gt;//Object one points to Type2 and object two points to Type1&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;starwars1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;     
&lt;span class=&quot;c1&quot;&gt;//Object one points to Type3 and object two points to Type1&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;starwars2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;    
&lt;span class=&quot;c1&quot;&gt;//Object one points to Type3 and object two points to Type2&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;starwars3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  
&lt;span class=&quot;c1&quot;&gt;//Object one and two points to Type3&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;starwars4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The assumption here is functions &lt;em&gt;starwars&lt;/em&gt; doesn’t mutate objects, it just accesses properties off them. It is optimal for &lt;em&gt;one&lt;/em&gt; and &lt;em&gt;two&lt;/em&gt; to share the exact same type when they are constructed. Both these objects end up with property x and y as well. So they can share the same type when the function &lt;em&gt;starwars4&lt;/em&gt; is invoked. In between these two states, we can see that objects &lt;em&gt;one&lt;/em&gt; and &lt;em&gt;two&lt;/em&gt; differ in what they contain. At the call site of &lt;em&gt;starwars2&lt;/em&gt; object &lt;em&gt;one&lt;/em&gt; has x &amp;amp; y but object &lt;em&gt;two&lt;/em&gt; has none at all. Obviously, they can’t have the same type. How do we make sure all the objects with same properties end up with the exact type if they are constructed at different point of time?&lt;/p&gt;

&lt;p&gt;Successor type in typehanlder comes to the rescue. Each typehandler holds a successor type which tracks what new type to assign to an object if it gets a new property. This process is known as type promotion or type transition. Following diagram tries to illustrate the same.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/2015-12-22-Type-ObjectLayout-TypePath.png&quot; alt=&quot;Object Layout&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When a property x is added to object with Type1, next type of the object is going to be Type2. Objects with just property x will share Type1.&lt;/p&gt;

&lt;p&gt;As script adds properties to objects, type promotion occurs quickly as typehandler holds a pointer to the next type. The successor type is present to improve the speed of type promotion. One can build a type system without the successor type. Though for objects to share types, you will have to search through an entire set of types to figure out what is the next type your object needs to get when you set a new property.&lt;/p&gt;

&lt;p&gt;To understand this in Chakra codebase take a look at  &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/PathTypeHandler.h#L207&quot;&gt;SimpleTypeHandler&lt;/a&gt;. SimpleTypeHanlder has a property map named &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/PathTypeHandler.h#L15&quot;&gt;typePath&lt;/a&gt; in the base class which has a [tiny dictionary] (https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/TypePath.h#L73) to map a propertyId (synonymous to property name) to a slot number. It also consists of successorTypeWeakRef which holds the pointer to next type. A variant of SimpleTypeHandler known as  [PathTypeHandler] (https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/PathTypeHandler.h#L243) holds map known as PropertySuccessorsMap to multiple successors.&lt;/p&gt;

&lt;p&gt;###Summary&lt;/p&gt;

&lt;p&gt;We looked at why objects have a type and how it shares data. We briefly looked at why typehandler stores a pointer to successor type. Note there are various other typehandlers which share data differently or not share at all. All have their own use cases for performance or memory.&lt;/p&gt;

&lt;p&gt;In my opinion biggest advantage of type is inline cache. We will look into the inline cache in the next post.&lt;/p&gt;

&lt;p&gt;Hope you liked this post and please post any feedback in the comments.&lt;/p&gt;</content><summary>Javascript is an untyped language. Type in the context of this blog refers to a data structure in the Chakra runtime, which holds additional information about an object. Type is also popularly known as hidden class. Type shares data about a class of objects. The sole purpose of a type in the runtime is to enhance the performance of the user script code. If you are building your own javascript runtime and didn’t care about performance or the memory, you don’t need type.</summary></entry><entry><title>Tagged Float</title><link href="http://abchatra.github.io/TaggedFloat/" rel="alternate" type="text/html" title="Tagged Float" /><published>2015-12-15T00:00:00+00:00</published><updated>2015-12-15T00:00:00+00:00</updated><id>http://abchatra.github.io/TaggedFloat</id><content type="html" xml:base="http://abchatra.github.io/TaggedFloat/">&lt;p&gt;A [tagged pointer] (https://en.wikipedia.org/wiki/Tagged_pointer) is a well know concept which every virtual machine (VM) tries to exploit. Unlike some other VM’s Chakra doesn’t tag a pointer. Instead, Chakra tag’s the non-pointer a.k.a a float or an int. For the purpose of this blog, I will illustrate the implementation of tagged float in 64 bit. In the context of this blog float means double-precision 64-bit format IEEE 754-2008 as specified by &lt;a href=&quot;http://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type&quot;&gt;ECMA262&lt;/a&gt;. Chakra doesn’t tag floats in 32 bit but tags integers. On 64 bit Chakra tags both floats and integers. First let us see our object representation, it’s size and the need for tagged floats.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;###Object representation
Javascript is a Garbage Collected (GC) language. Any object or primitive (which represents javascript var) is accessed as a void pointer named Var in Chakra runtime.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;C++
typedef void * Var;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Var typically points to a &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/RecyclableObject.h#L191&quot;&gt;RecyclableObject&lt;/a&gt;. RecyclableObject is the root of the object hierarchy which all other objects inherit. This necessitates a vTable pointer which consumes 8 bytes for each object. It holds an additional pointer to &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Types/Type.h#L22&quot;&gt;Type&lt;/a&gt; which accounts for another 8 bytes. Type structure disambiguates between various kinds of RecyclableObjects such as strings, numbers, dynamic objects  and can be shared between multiple objects. Every RecyclableObject has following two fields:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;C++
__vfptr*   // 8 bytes
type*      // 8 bytes
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In a nutshell, 16 bytes are required to represent a simple object (again in x64).  Now let us take an example. Following &lt;code class=&quot;highlighter-rouge&quot;&gt;speed&lt;/code&gt; javascript variable holds a float.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To represent this Var in the engine, Chakra need to create an object named &lt;a href=&quot;https://github.com/Microsoft/ChakraCore/blob/master/lib/Runtime/Library/JavascriptNumber.h&quot;&gt;JavascriptNumber&lt;/a&gt; which inherits from RecyclableObject and can store a float value (10.4). Total bytes required is 24 (&lt;code class=&quot;highlighter-rouge&quot;&gt;sizeof(Js::JavascriptNumber) == sizeof(Js::RecyclableObject) + sizeof(double))&lt;/code&gt;. Turns out our GC allocates at 16 byte boundary. 24 bytes is rounded off to 32 bytes. Chakra needs 32 bytes to represent a JavascriptNumber. In addition, to this 8 byte Var pointer is necessary for the runtime to point to this object. Can we avoid this overhead for every var pointing to a float? If we could encode a float in a pointer and mark it as a non-pointer, the runtime can recognize it and we save on bytes.&lt;/p&gt;

&lt;p&gt;###Extra bits in a pointer
Let us look at memory address allocated by the GC carefully.
Couple of characteristics of pointers:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Bottom 4 bits are always going to be zero. (Remember our GC allocates at 16 byte boundary)&lt;/li&gt;
  &lt;li&gt;Top 16 bits are going to zero as the operating system only uses bottom 48 bits to represent virtual memory address (256TB is good enough).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can party with these extra bits. One can use these always zero bits to encode a float. But how will the runtime differentiate between a valid pointer or a float encoded in a pointer? Can we encode the entire float which is 64 bit inside a pointer which is also 64 bit and don’t lose precision? First it is important to understand the floating point representation.&lt;/p&gt;

&lt;p&gt;###IEEE 754 floating point representation
Now let us look at the 64-bit double precision floating point value:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Sign&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Exponent&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;Fraction&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1 [63]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;11 [62-52]&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;52 [51-00]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;See [this] (http://steve.hollasch.net/cgindex/coding/ieeefloat.html) blog for more information on floating-point format. The interesting part is the exponent. If all the 11 bits in the exponent are 1 it can represent 3 values:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Positive infinity.&lt;/li&gt;
  &lt;li&gt;Negative infinity.&lt;/li&gt;
  &lt;li&gt;NaN&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;NaN’s are represented by a bit pattern with an exponent of all 1’s and a non-zero fraction. If the fraction is all zero it can be either +Infinity or -Infinity depending on sign bit. This lets tons of ways of expressing NaN. Ecma262 specifies that all NaN’s are treated equally. So we just need one. Chakra canonicalize’s all the NaN’s to just the one shown below which is, in fact, a &lt;a href=&quot;https://en.wikipedia.org/wiki/NaN&quot;&gt;QNaN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;C++
static const uint64 k_Nan    = 0xFFF8000000000000ull;
static const uint64 k_PosInf = 0x7FF0000000000000ull;
static const uint64 k_NegInf = 0xFFF0000000000000ull;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now let us dig deeper into our tagging scheme.&lt;/p&gt;

&lt;p&gt;###Tagging scheme
Remember our goal here is to pick a tagging scheme in a pointer so as to:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Easily differentiate between a pointer and a float.&lt;/li&gt;
  &lt;li&gt;Not lose any data while encoding a float.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From the above IEEE 754 floating representation we know that all float values (except NaN, +Infinity, -Infinity) are guaranteed to have at least one of the exponent bits &lt;strong&gt;not set&lt;/strong&gt;. So we xor all floats with &lt;strong&gt;0xFFFC0000 00000000 or 0xFFFC«48&lt;/strong&gt; and store them in the memory as pointers. This magic xor constant guarantees that all floats will have at least one bit set in the exponent part (bits 62-52). This magic constant also ensures that NaN, Infinity &amp;amp; -Infinity have 50th-bit set. To generalize all floats will have one of the top 16 bit set. Pointers won’t have any of the top 16 bit set.&lt;/p&gt;

&lt;p&gt;A simple table to illustrate.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;float value or pointer&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Bit pattern in hex&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Bit pattern after xor&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0000000000000000&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;fffc000000000000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;c02599999999999a&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3fd999999999999a&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;Infinity&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;7ff0000000000000&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;800c000000000000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;-Infinity&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;fff0000000000000&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;000c000000000000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;NaN&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;fff8000000000000&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0004000000000000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;RecyclableObject*&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;00000209512b4e20&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;00000209512b4e20&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Note: Chakra keeps RecyclableObject pointer values as is.&lt;/p&gt;

&lt;p&gt;See links for &lt;a href=&quot;http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html&quot;&gt;floating point conversion&lt;/a&gt; calculator.&lt;/p&gt;

&lt;p&gt;Runtime simply looks at top 16 bits (&lt;code class=&quot;highlighter-rouge&quot;&gt;x &amp;gt;&amp;gt; 48 != 0&lt;/code&gt;). If any of top 16 bit is set, it is a float or else it is a valid pointer to a RecyclableObject. If it is a float, runtime gets the original float by xoring with &lt;strong&gt;0xFFFC«48&lt;/strong&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;x ===  (x^(0xFFFC&amp;lt;&amp;lt;48)^(0xFFFC&amp;lt;&amp;lt;48)&lt;/code&gt;). Total memory spent on a float in the VM is just 8 bytes as Chakra directly stores the float inside a pointer.&lt;/p&gt;

&lt;p&gt;To close this post Chakra does bit twiddling magic to save 32 bytes of memory for each var pointing to a float. Hope this helps. Please let me know the feedback either through email or leaving a comment here.&lt;/p&gt;</content><summary>A [tagged pointer] (https://en.wikipedia.org/wiki/Tagged_pointer) is a well know concept which every virtual machine (VM) tries to exploit. Unlike some other VM’s Chakra doesn’t tag a pointer. Instead, Chakra tag’s the non-pointer a.k.a a float or an int. For the purpose of this blog, I will illustrate the implementation of tagged float in 64 bit. In the context of this blog float means double-precision 64-bit format IEEE 754-2008 as specified by ECMA262. Chakra doesn’t tag floats in 32 bit but tags integers. On 64 bit Chakra tags both floats and integers. First let us see our object representation, it’s size and the need for tagged floats.</summary></entry><entry><title>Hello open source</title><link href="http://abchatra.github.io/Introduction/" rel="alternate" type="text/html" title="Hello open source" /><published>2015-12-14T00:00:00+00:00</published><updated>2015-12-14T00:00:00+00:00</updated><id>http://abchatra.github.io/Introduction</id><content type="html" xml:base="http://abchatra.github.io/Introduction/">&lt;p&gt;I joined Chakra team back in 2009. I have hacked a bunch of code in Chakra engine. 
Eventhough I have spent a great deal of time in this codebase, I still have lot to learn. 
(I am not an expert in any sense).&lt;/p&gt;

&lt;p&gt;Intention of this blog is to help new developers to make sense of basic building blocks in Chakra. 
Over the course I intend to cover all parts of the engine such as type system, jit, 
runtime etc. If you see any inaccuracy in my blog please point out.&lt;/p&gt;

&lt;p&gt;Following is the list of articles. I will populate the list as I have more content.
If you are particularly interested in a topic do let me know.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;[Tagged float] (http://abchatra.github.io/TaggedFloat)&lt;/li&gt;
  &lt;li&gt;[Type] (http://abchatra.github.io/Type)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you!&lt;/p&gt;</content><summary>I joined Chakra team back in 2009. I have hacked a bunch of code in Chakra engine. 
Eventhough I have spent a great deal of time in this codebase, I still have lot to learn. 
(I am not an expert in any sense).

Intention of this blog is to help new developers to make sense of basic building blocks in Chakra. 
Over the course I intend to cover all parts of the engine such as type system, jit, 
runtime etc. If you see any inaccuracy in my blog please point out.

Following is the list of articles. I will populate the list as I have more content.
If you are particularly interested in a topic do let me know.


  [Tagged float] (http://abchatra.github.io/TaggedFloat)
  [Type] (http://abchatra.github.io/Type)


Thank you!</summary></entry></feed>
