RATA: Rapid Atomic Type Analysis by Abstract Interpretation – Application to JavaScript Optimization [chapter]

Francesco Logozzo, Herman Venter
2010 Lecture Notes in Computer Science  
We introduce RATA, a static analysis based on abstract interpretation for the rapid inference of atomic types in JavaScript programs. RATA enables aggressive type specialization optimizations in dynamic languages. RATA is a combination of an interval analysis (to determine the range of variables), a kind analysis (to determine if a variable may assume fractional values, or NaN), and a variation analysis (to relate the values of variables). The combination of those three analyses allows our
more » ... ler to specialize Float64 variables (the only numerical type in JavaScript) to Int32 variables, providing large performance improvements (up to 7.7×) in some of our benchmarks. . Let us consider the nestedLoops function in Fig. 1 . Without any knowledge of the concrete types of i and j, the JIT should generate a value wrapper containing: (i) a tag with the dynamic type of the value, and (ii) the value. Value wrappers are disastrous for performance. For instance, the execution of nestedLoops takes 310ms on our laptop. 1 In fact, the dynamic execution of the statement i++ involves: (i) an "unbox" operation to fetch the old value of i and check that it is a numerical type; (ii) incrementing i; (iii) a "box" operation to update the wrapper with the new value. The JIT can specialize the function if it knows that i and j are numerical values. In JavaScript, the only numerical type is a 64 bits floating point (Float64) which follows the IEEE754 standard [16, 19] . In our case, a simple type inference can determine that i and j are Float64: they are initialized to zero and only incremented by one. The execution time then goes down to 180ms. The JIT may do a better job if it knows that i and j are Int32: floating point comparisons are quite inefficient and they usually requires twice or more instructions to perform than integer comparisons on a x86 architecture. A simple type inference does not help, as it cannot infer that i and j are bounded by 10000. In fact, it is safe to specialize a numerical variable x with type Int32 when one can prove that for all possible executions: (i) x never assumes values outside of the range [−2 31 , 2 31 − 1]; and (ii) x is never assigned a fractional value (e.g., 0.5). Contribution. We introduce RATA, Rapid Atomic Type Analysis, a new static analysis based on abstract interpretation, to quickly and precisely infer the numerical types of variables. RATA is based on a combination of an interval analysis (to determine the range of variables), a kind analysis (to determine if a variable may assume fractional values, or NaN) and a variation analysis (to relate the values of variables). In our example, the first analysis discovers that i ∈ [0, 10000], j ∈ [0, 10000] and the second that i, j ∈ Z. Using this information, the JIT can further specialize the code so that i and j are allocated in integer registers, and as a matter of fact the execution time (inclusive of the analysis time) drops to 31ms! The function bitsinbyte in Fig. 1 (extracted from the SunSpider benchmarks [31]) illustrates the need for the variation analysis. The interval analysis determines that m ∈ [1, 256], c ∈ [0, +∞]. The kind analysis determines that m, c ∈ Z. If we infer that c ≤ m then we can conclude that c is an Int32. In general, we can solve this problem using a relational, or weakly relational abstract domain, such as Polyhedra [12] , Subpolyhedra [23], Octagons [26], or Pentagons [24] . However, all those abstract domains have a cost which is quadratic (Pentagons), cubic (Octagons), polynomial (Subpolyhedra) or exponential (Polyhedra) and hence we rejected their use, as non-linear costs are simply not tolerable at runtime. Our variation analysis infers that: (i) m and c differ by one 1 The data we report is based on the experience with our own implementation of a JavaScript interpreter for .Net. More details will be given in Sect. 6.
doi:10.1007/978-3-642-11970-5_5 fatcat:np5rwetui5dwtg3wjbjtiiesym