One VM to Rule Them All
Total Page:16
File Type:pdf, Size:1020Kb
One VM to Rule Them All ∗ ∗ † † Thomas Wurthinger¨ Christian Wimmer Andreas Woߨ Lukas Stadler † † § ∗ ∗ Gilles Duboscq Christian Humer Gregor Richards Doug Simon Mario Wolczko ∗ † § Oracle Labs Institute for System Software, Johannes Kepler University Linz, Austria S3 Lab, Purdue University {thomas.wuerthinger, christian.wimmer, doug.simon, mario.wolczko}@oracle.com {woess, stadler, duboscq, christian.humer}@ssw.jku.at [email protected] Abstract These implementations can be characterized in the fol- Building high-performance virtual machines is a complex lowing way: and expensive undertaking; many popular languages still • Their performance on typical applications is within a have low-performance implementations. We describe a new small multiple (1-3x) of the best statically compiled code approach to virtual machine (VM) construction that amor- for most equivalent programs written in an unsafe lan- tizes much of the effort in initial construction by allowing guage such as C. new languages to be implemented with modest additional • effort. The approach relies on abstract syntax tree (AST) in- They are usually written in an unsafe, systems program- terpretation where a node can rewrite itself to a more special- ming language (C or C++). ized or more general node, together with an optimizing com- • Their implementation is highly complex. piler that exploits the structure of the interpreter. The com- • They implement a single language, or provide a bytecode piler uses speculative assumptions and deoptimization in or- interface that advantages a narrow set of languages to the der to produce efficient machine code. Our initial experience detriment of other languages. suggests that high performance is attainable while preserv- ing a modular and layered architecture, and that new high- In contrast, there are numerous languages that are popu- performance language implementations can be obtained by lar, have been around for about 20 years, and yet still have writing little more than a stylized interpreter. no implementations that even approach this level of perfor- mance. Examples are PHP, Python, Ruby, R, Perl, MAT- Categories and Subject Descriptors D.3.4 [Programming LAB, Smalltalk, and APL. The computer language bench- Languages]: Processors—Run-time environments marks game [20] provides some insights into the perfor- Keywords Java; JavaScript; dynamic languages; virtual mance of various languages. machine; language implementation; optimization JavaScript is in an intermediate state. Performance was lackluster until 2008, but significant effort has since been 1. Introduction invested in several competing implementations to make per- formance respectable. We believe that the recent history of High-performance VM implementations for object-oriented JavaScript explains why the other languages have lesser per- languages have existed for about twenty years, as pioneered formance: until industrial-scale investment becomes avail- by the Self VM [25], and have been in widespread use for fif- able, the complexity of a traditional high-performance VM teen years. Examples are high-performance Java VMs such is too high for small-scale efforts to build and maintain as Oracle’s Java HotSpot VM [28] and IBM’s Java VM [30], a high-performance implementation for multiple hardware as well as Microsoft’s Common Language Runtime (the VM platforms and operating systems. of the .NET framework [42]). We present a new approach and architecture, which en- ables implementing a wide range of languages within a com- Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed mon framework, reusing many components (especially the for profit or commercial advantage and that copies bear this notice and the full citation optimizing compiler). This brings excellent performance for on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or all languages using our framework. Our approach relies on a republish, to post on servers or to redistribute to lists, requires prior specific permission framework that allows nodes to rewrite1 themselves during and/or a fee. Request permissions from [email protected]. interpretation, a speculatively optimizing compiler that can Onward! 2013, October 29–31, 2013, Indianapolis, Indiana, USA. Copyright is held by the owner/author(s). Publication rights licensed to ACM. ACM 978-1-4503-2472-4/13/10/13/10. $15.00. 1 We use “node rewriting” in a sense that is distinct from rewriting in the http://dx.doi.org/10.1145/2509578.2509581 context of the lambda calculus, formal semantics, or term rewriting systems. exploit the structure of interpreters written using this frame- Written by: Written in: Application work, and deoptimization to revert back to interpreted exe- Guest Language Application Guest Language cution on speculation failures. Developer Language In this paper, we describe the overall approach and the Guest Language Implementation Managed Host Language structure of our implementation, with particular attention to Developer Managed Host Language the interaction between the interpreter, the compiler, and de- VM Expert Host Services or Unmanaged Language optimization. Additionally, we describe the variety of lan- Unmanaged Language OS Expert OS guage implementations we are undertaking, with specific (typically C or C++) reference to their unique attributes and the challenges posed. Our focus is on dynamically typed, imperative programming Figure 1. System structure of a guest language implementa- languages such as JavaScript, Ruby, and Python; as well as tion utilizing host services to build a high-performance VM languages for technical computing such as R and J. Section 6 for the guest language. presents details on the languages. This paper presents a forward-looking viewpoint on work offer many benefits for applications running on top of them, in progress. We have a working prototype implementation but they mostly do not utilize these benefits for themselves. of the interpreter framework and the compilation infrastruc- In contrast, we want the VMs for many different guest lan- ture. A detailed description of the node rewriting appears guages written in a managed host language. Only the guest- elsewhere [72]; a brief summary of that paper appears at language specific part is implemented anew by the language the beginning of Section 3. The AST and machine code of developer. A core of reusable host services are provided the example presented in the subsequent sections are pro- by the framework, such as dynamic compilation, automatic duced by our actual system. We are therefore convinced that memory management, threads, synchronization primitives, the approach can be successful. However, more implementa- and a well-defined memory model. Figure 1 summarizes our tion work is necessary to get larger industry-standard bench- system structure. marks running for multiple languages. The host services can either be written in the managed Our language implementation framework, called Truffle, host language or in an unmanaged language; our approach as well as our compilation infrastructure, called Graal, are does not impose restrictions on that. Section 5 presents dif- available as open source in an OpenJDK project [45]. We ferent approaches for providing the host services. encourage academic and open source community contribu- For the concrete examples in this paper, the guest lan- tions, especially in the area of new innovative language fea- guage is JavaScript and the host language is a subset of Java, tures and language implementations. Language implemen- i.e., we have a guest JavaScript VM as well as the host ser- tations that are started now, using our framework, will au- vices implemented in Java. However, we want to stress that tomatically profit from our compilation infrastructure when the idea is language independent. Section 6 shows how to it is finished, so we envision multiple language implementa- map key features of many guest languages to our system. tions being developed by third parties simultaneously to our We have designed our system in the expectation that the host efforts. In summary, this paper contributes the following: language is statically typed. This allows us to express type • We present a new VM architecture and our implemen- specializations, i.e., to explicitly express the semantics of a tation, which can be used to construct high-performance guest language operation on a specific type, as well as to implementations of a wide range of languages at modest explicitly express type conversions in the (possibly dynami- incremental cost. cally typed) guest language. • Our layered approach simplifies guest language imple- We show how the combination of node rewriting during mentation. Host services factor out common parts found in interpretation, optimizing compilation, and deoptimiza- every high-performance VM, allowing guest language de- tion delivers high performance from an interpreter with- velopers to focus on required execution semantics. However, out requiring a language-specific compiler. the benefits of the layering must not sacrifice peak perfor- • We show how features of a variety of programming lan- mance of the guest language. Dynamic compilation of guest guages map on our framework. language code to machine code is therefore essential. Fig- • We describe how our system supports alternative de- ure