Profile-guided static typing for dynamic scripting languages
Authors:
Open Access
- 25 October 2009
- conference paper
- Published by Association for Computing Machinery (ACM)
- Vol. 44 (10) , 283-300
Abstract
Many popular scripting languages such as Ruby, Python, and Perl include highly dynamic language constructs, such as an eval method that evaluates a string as program text. While these constructs allow terse and expressive code, they have traditionally obstructed static analysis. In this paper we present PRuby, an extension to Diamondback Ruby (DRuby), a static type inference system for Ruby. PRuby augments DRuby with a novel dynamic analysis and transformation that allows us to precisely type uses of highly dynamic constructs. PRuby's analysis proceeds in three steps. First, we use run-time instrumentation to gather per-application profiles of dynamic feature usage. Next, we replace dynamic features with statically analyzable alternatives based on the profile. We also add instrumentation to safely handle cases when subsequent runs do not match the profile. Finally, we run DRuby's static type inference on the transformed code to enforce type safety. We used PRuby to gather profiles for a benchmark suite of sample Ruby programs. We found that dynamic features are pervasive throughout the benchmarks and the libraries they include, but that most uses of these features are highly constrained and hence can be effectively profiled. Using the profiles to guide type inference, we found that DRuby can generally statically type our benchmarks modulo some refactoring, and we discovered several previously unknown type errors. These results suggest that profiling and transformation is a lightweight but highly effective approach to bring static typing to highly dynamic languages.Keywords
This publication has 19 references indexed in Scilit:
- The design and implementation of typed scheme2008
- RPython2007
- Gradual Typing for Objects2007
- Towards Type Inference for JavaScript2005
- Towards a Type System for Analyzing JavaScript ProgramsLecture Notes in Computer Science, 2005
- Pointer Analysis in the Presence of Dynamic Class LoadingLecture Notes in Computer Science, 2004
- A framework for interprocedural optimization in the presence of dynamic class loading2000
- Towards partially evaluating reflection in Java1999
- Practical experience with an application extractor for Java1999
- Dynamic typing in a statically typed languageACM Transactions on Programming Languages and Systems, 1991