The VM already knew that: leveraging compile-time knowledge to optimize gradual typing

Programmers in dynamic languages wishing to constrain and understand the behavior of their programs may turn to gradually-typed languages, which allow types to be specified optionally and check values at the boundary between dynamic and static code. Unfortunately, the performance cost of these run-time checks can be severe, slowing down execution by at least 10x when checks are present. Modern virtual machines (VMs) for dynamic languages use speculative techniques to improve performance: If a particular value was seen once, it is likely that similar values will be seen in the future. They combine optimization-relevant properties of values into cacheable “shapes”, then use a single shape check to subsume checks for each property. Values with the same memory layout or the same field types have the same shape. This greatly reduces the amount of type checking that needs to be performed at run-time to execute dynamic code. While very valuable to the VM’s optimization, these checks do little to benefit the programmer aside from improving performance. We present in this paper a design for intrinsic object contracts, which makes the obligations of gradually-typed languages’ type checks an intrinsic part of object shapes, and thus can subsume run-time type checks into existing shape checks, eliminating redundant checks entirely. With an implementation on a VM for JavaScript used as a target for SafeTypeScript’s soundness guarantees, we demonstrate slowdown averaging 7% in fully-typed code relative to unchecked code, and no more than 45% in pessimal configurations.

[1]  Philip Wadler,et al.  Threesomes, with and without blame , 2010, POPL '10.

[2]  Martín Abadi,et al.  Understanding TypeScript , 2014, ECOOP.

[3]  Matthias Felleisen,et al.  Contracts for higher-order functions , 2002, ICFP '02.

[4]  Jan Vitek,et al.  Thorn: robust concurrent scripting on the JVM , 2009, OOPSLA Companion.

[5]  Philip Wadler,et al.  Well-Typed Programs Can't Be Blamed , 2009, ESOP.

[6]  Carl Friedrich Bolz,et al.  Tracing the meta-level: PyPy's tracing JIT compiler , 2009, ICOOOLPS@ECOOP.

[7]  Hanspeter Mössenböck,et al.  An object storage model for the truffle language implementation framework , 2014, PPPJ '14.

[8]  Marc Feeley,et al.  Interprocedural Type Specialization of JavaScript Programs Without Type Analysis , 2016, ECOOP.

[9]  Jan Vitek,et al.  Concrete Types for TypeScript , 2015, ECOOP.

[10]  Sam Tobin-Hochstadt,et al.  The design and implementation of typed scheme , 2008, POPL '08.

[11]  Gavin M. Bierman,et al.  Safe & Efficient Gradual Typing for TypeScript , 2015, POPL.

[12]  Christian Wimmer,et al.  One VM to rule them all , 2013, Onward!.

[13]  John Tang Boyland,et al.  Refined Criteria for Gradual Typing , 2015, SNAPL.

[14]  Jan Vitek,et al.  Is sound gradual typing dead? , 2016, POPL.

[15]  Marc Feeley,et al.  Simple and Effective Type Check Removal through Lazy Basic Block Versioning , 2015, ECOOP.

[16]  Craig Chambers,et al.  An efficient implementation of SELF, a dynamically-typed object-oriented language based on prototypes , 1989, OOPSLA '89.

[17]  Sam Tobin-Hochstadt,et al.  Monotonic References for Efficient Gradual Typing , 2015, ESOP.

[18]  Jan Vitek,et al.  Integrating typed and untyped code in a scripting language , 2010, POPL '10.

[19]  Matthias Felleisen,et al.  Contract Soundness for object-oriented languages , 2001, OOPSLA '01.

[20]  Mason Chang,et al.  Trace-based just-in-time type specialization for dynamic languages , 2009, PLDI '09.

[21]  Jim Baker,et al.  Design and evaluation of gradual typing for python , 2014, DLS.

[22]  Robert Bruce Findler,et al.  Relationally-parametric polymorphic contracts , 2007, DLS '07.