Rule-based program specialization to optimize gradually typed code

Abstract Both static and dynamic typing provide different benefits to the programmer. Statically typed languages support earlier type error detection and more opportunities for compiler optimizations. Dynamically typed languages facilitate the development of runtime adaptable applications and rapid prototyping. Since both approaches provide benefits, gradually typed languages support both typing approaches in the very same programming language. Gradual typing has been an active research field in the last years, turning out to be a strong influence on commercial languages. However, one important drawback of gradual typing is the runtime performance cost of the additional type checks performed at runtime. In this article, we propose a rule-based program specialization mechanism to provide significant performance optimizations of gradually typed code. Our system gathers dynamic type information of the application by simulating its execution. That type information is used to optimize the generated code, reducing the number of type checks performed at runtime. Moreover, program specialization allows the early detection of compile-time type errors, providing static type safety. To ensure the correctness of the proposed approach, we prove its soundness and efficiency properties. The specialization system has been implemented as part of a full-fledged programming language, measuring the runtime performance gain. The generated code performs significantly better than the state-of-the-art techniques to optimize dynamically typed code. Unlike the existing approaches, our system does not consume additional memory resources at runtime, because program specialization is performed statically. Program specialization involves an average compilation time increase from 2% to 11.75%.

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

[2]  Satish R. Thatte Quasi-static typing , 1989, POPL '90.

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

[4]  Sven Apel,et al.  Feature featherweight java: a calculus for feature-oriented programming and stepwise refinement , 2008, GPCE '08.

[5]  Atsushi Igarashi,et al.  Union types for object-oriented programming , 2006, SAC.

[6]  Davide Ancona,et al.  RPython: a step towards reconciling dynamically and statically typed OO languages , 2007, DLS '07.

[7]  Laurence Tratt,et al.  Dynamically Typed Languages , 2009, Adv. Comput..

[8]  J. Baltasar García Pérez-Schofield,et al.  Including both static and dynamic typing in the same programming language , 2010, IET Softw..

[9]  Walid Taha,et al.  Gradual Typing for Objects , 2007, ECOOP.

[10]  Yoshihiko Futamura,et al.  Partial Evaluation of Computation Process--An Approach to a Compiler-Compiler , 1999, High. Order Symb. Comput..

[11]  Sam Tobin-Hochstadt,et al.  Pycket: a tracing JIT for a functional language , 2015, ICFP.

[12]  Jose Manuel Redondo,et al.  Attaining multiple dispatch in widespread object-oriented languages , 2014 .

[13]  Giuseppe Castagna,et al.  Gradual typing with union and intersection types , 2017, Proc. ACM Program. Lang..

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

[15]  L. Peter Deutsch,et al.  Efficient implementation of the smalltalk-80 system , 1984, POPL.

[16]  Francisco Ortin,et al.  Towards a Static Type Checker for Python , 2015 .

[17]  David J. Pearce Sound and Complete Flow Typing with Unions, Intersections and Negations , 2013, VMCAI.

[18]  Martín Abadi,et al.  Dynamic typing in polymorphic languages , 1995, Journal of Functional Programming.

[19]  Francisco Ortin,et al.  Design and implementation of an efficient hybrid dynamic and static typing language , 2016, Softw. Pract. Exp..

[20]  Benjamin C. Pierce,et al.  Types and programming languages: the next generation , 2003, 18th Annual IEEE Symposium of Logic in Computer Science, 2003. Proceedings..

[21]  Gavin M. Bierman,et al.  Adding Dynamic Types to C# , 2010, ECOOP.

[22]  Matteo Cimini,et al.  Principal Type Schemes for Gradual Programs , 2015, POPL.

[23]  Dave Clarke,et al.  Gradual Ownership Types , 2012, ESOP.

[24]  Robert Cartwright,et al.  Soft typing , 1991, PLDI '91.

[25]  Francisco Ortin Type Inference to Optimize a Hybrid Statically and Dynamically Typed Language , 2011, Comput. J..

[26]  Juan Manuel Cueva Lovelle,et al.  Optimizing Reflective Primitives of Dynamic Languages , 2008, Int. J. Softw. Eng. Knowl. Eng..

[27]  Lieven Eeckhout,et al.  Statistically rigorous java performance evaluation , 2007, OOPSLA.

[28]  Cormac Flanagan,et al.  Space-efficient gradual typing , 2010, High. Order Symb. Comput..

[29]  Peter Sestoft,et al.  Partial evaluation and automatic program generation , 1993, Prentice Hall international series in computer science.

[30]  Sophia Drossopoulou,et al.  Session Types for Object-Oriented Languages , 2006, ECOOP.

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

[32]  Ulrik Pagh Schultz Partial Evaluation for Class-Based Object-Oriented Languages , 2000 .

[33]  Dan Grossman,et al.  EnerJ: approximate data types for safe and general low-power computation , 2011, PLDI '11.

[34]  Joshua Dunfield,et al.  Sums of uncertainty: refinements go gradual , 2017, POPL.

[35]  Alexander Aiken,et al.  Type inclusion constraints and type inference , 1993, FPCA '93.

[36]  Ravi Chugh,et al.  Nested refinements: a logic for duck typing , 2012, POPL '12.

[37]  淳 五十嵐 Formalizing Advanced Class Mechanisms , 2000 .

[38]  Mariangiola Dezani-Ciancaglini,et al.  Intersection and Union Types: Syntax and Semantics , 1995, Inf. Comput..

[39]  Sam Tobin-Hochstadt,et al.  Towards Practical Gradual Typing , 2015, ECOOP.

[40]  Francisco Ortin,et al.  SSA Transformations to Facilitate Type Inference in Dynamically Typed Code , 2017, Comput. J..

[41]  David Llewellyn-Jones,et al.  Optimizing runtime performance of hybrid dynamically and statically typed languages for the .Net platform , 2016, J. Syst. Softw..

[42]  Armin Rigo,et al.  Representation-based just-in-time specialization and the psyco prototype for python , 2004, PEPM '04.

[43]  David J. Lilja,et al.  Measuring computer performance : A practitioner's guide , 2000 .

[44]  Matt Zandstra,et al.  PHP 5 Objects, Patterns, and Practice , 2006, Apress.

[45]  Andreas Krause,et al.  Predicting Program Properties from "Big Code" , 2015, POPL.

[46]  Raghu V. Hudli,et al.  CORBA fundamentals and programming , 1996 .

[47]  Reinhold Weicker,et al.  Dhrystone: a synthetic systems programming benchmark , 1984, CACM.

[48]  Philip Wadler,et al.  Featherweight Java: a minimal core calculus for Java and GJ , 2001, TOPL.

[49]  Jens Palsberg,et al.  Lightweight confinement for featherweight java , 2003, OOPSLA 2003.

[50]  Martin Erwig,et al.  Migrating gradual types , 2018, Proc. ACM Program. Lang..

[51]  Atsushi Igarashi,et al.  Gradual session types , 2017, Proc. ACM Program. Lang..

[52]  Sam Tobin-Hochstadt,et al.  Sound gradual typing: only mostly dead , 2017, Proc. ACM Program. Lang..

[53]  Francisco Ortin,et al.  Union and intersection types to support both dynamic and static typing , 2011, Inf. Process. Lett..