Debian Codegraph @

rpython/doc/jit/overview.rst

raw
1 Motivating JIT Compiler Generation
2 ==================================
3
4 .. contents::
5
6 This is a non-technical introduction and motivation for RPython's approach
7 to Just-In-Time compiler generation.
8
9
10 Motivation
11 ----------
12
13 Overview
14 ~~~~~~~~
15
16 Writing an interpreter for a complex dynamic language like Python is not
17 a small task, especially if, for performance goals, we want to write a
18 Just-in-Time (JIT) compiler too.
19
20 The good news is that it's not what we did. We indeed wrote an
21 interpreter for Python, but we never wrote any JIT compiler for Python
22 in PyPy. Instead, we use the fact that our interpreter for Python is
23 written in RPython, which is a nice, high-level language -- and we turn
24 it *automatically* into a JIT compiler for Python.
25
26 This transformation is of course completely transparent to the user,
27 i.e. the programmer writing Python programs. The goal (which we
28 achieved) is to support *all* Python features -- including, for example,
29 random frame access and debuggers. But it is also mostly transparent to
30 the language implementer, i.e. to the source code of the Python
31 interpreter. It only needs a bit of guidance: we had to put a small
32 number of hints in the source code of our interpreter. Based on these
33 hints, the *JIT compiler generator* produces a JIT compiler which has
34 the same language semantics as the original interpreter by construction.
35 This JIT compiler itself generates machine code at runtime, aggressively
36 optimizing the user's program and leading to a big performance boost,
37 while keeping the semantics unmodified. Of course, the interesting bit
38 is that our Python language interpreter can evolve over time without
39 getting out of sync with the JIT compiler.
40
41
42 The path we followed
43 ~~~~~~~~~~~~~~~~~~~~
44
45 Our previous incarnations of PyPy's JIT generator were based on partial
46 evaluation. This is a well-known and much-researched topic, considered
47 to be very promising. There have been many attempts to use it to
48 automatically transform an interpreter into a compiler. However, none of
49 them have lead to substantial speedups for real-world languages. We
50 believe that the missing key insight is to use partial evaluation to
51 produce just-in-time compilers, rather than classical ahead-of-time
52 compilers. If this turns out to be correct, the practical speed of
53 dynamic languages could be vastly improved.
54
55 All these previous JIT compiler generators were producing JIT compilers
56 similar to the hand-written Psyco. But today, starting from 2009, our
57 prototype is no longer using partial evaluation -- at least not in a way
58 that would convince paper reviewers. It is instead based on the notion
59 of *tracing JIT,* recently studied for Java and JavaScript. When
60 compared to all existing tracing JITs so far, however, partial
61 evaluation gives us some extra techniques that we already had in our
62 previous JIT generators, notably how to optimize structures by removing
63 allocations.
64
65 The closest comparison to our current JIT is Tamarin's TraceMonkey.
66 However, this JIT compiler is written manually, which is quite some
67 effort. Instead, we write a JIT generator at the level of RPython,
68 which means that our final JIT does not have to -- indeed, cannot -- be
69 written to encode all the details of the full Python language. These
70 details are automatically supplied by the fact that we have an
71 interpreter for full Python.
72
73
74 Practical results
75 ~~~~~~~~~~~~~~~~~
76
77 The JIT compilers that we generate use some techniques that are not in
78 widespread use so far, but they are not exactly new either. The point
79 we want to make here is not that we are pushing the theoretical limits
80 of how fast a given dynamic language can be run. Our point is: we are
81 making it **practical** to have reasonably good Just-In-Time compilers
82 for all dynamic languages, no matter how complicated or non-widespread
83 (e.g. Open Source dynamic languages without large industry or academic
84 support, or internal domain-specific languages). By practical we mean
85 that this should be:
86
87 * Easy: requires little more efforts than writing the interpreter in the
88 first place.
89
90 * Maintainable: our generated JIT compilers are not separate projects
91 (we do not generate separate source code, but only throw-away C code
92 that is compiled into the generated VM). In other words, the whole
93 JIT compiler is regenerated anew every time the high-level interpreter
94 is modified, so that they cannot get out of sync no matter how fast
95 the language evolves.
96
97 * Fast enough: we can get some rather good performance out of the
98 generated JIT compilers. That's the whole point, of course.
99
100
101 Alternative approaches to improve speed
102 ---------------------------------------
103
104 +----------------------------------------------------------------------+
105 | :NOTE: |
106 | |
107 | Please take the following section as just a statement of opinion. |
108 | In order to be debated over, the summaries should first be |
109 | expanded into full arguments. We include them here as links; |
110 | we are aware of them, even if sometimes pessimistic about them |
111 | ``:-)`` |
112 +----------------------------------------------------------------------+
113
114 There are a large number of approaches to improving the execution speed of
115 dynamic programming languages, most of which only produce small improvements
116 and none offer the flexibility and customisability provided by our approach.
117 Over the last 6 years of tweaking, the speed of CPython has only improved by a
118 factor of 1.3 or 1.4 (depending on benchmarks). Many tweaks are applicable to
119 PyPy as well. Indeed, some of the CPython tweaks originated as tweaks for PyPy.
120
121 IronPython initially achieved a speed of about 1.8 times that of CPython by
122 leaving out some details of the language and by leveraging the large investment
123 that Microsoft has put into making the .NET platform fast; the current, more
124 complete implementation has roughly the same speed as CPython. In general, the
125 existing approaches have reached the end of the road, speed-wise. Microsoft's
126 Dynamic Language Runtime (DLR), often cited in this context, is essentially
127 only an API to make the techniques pioneered in IronPython official. At best,
128 it will give another small improvement.
129
130 Another technique regularly mentioned is adding types to the language in order
131 to speed it up: either explicit optional typing or soft typing (i.e., inferred
132 "likely" types). For Python, all projects in this area have started with a
133 simplified subset of the language; no project has scaled up to anything close
134 to the complete language. This would be a major effort and be platform- and
135 language-specific. Moreover maintenance would be a headache: we believe that
136 many changes that are trivial to implement in CPython, are likely to invalidate
137 previous carefully-tuned optimizations.
138
139 For major improvements in speed, JIT techniques are necessary. For Python,
140 Psyco gives typical speedups of 2 to 4 times - up to 100 times in algorithmic
141 examples. It has come to a dead end because of the difficulty and huge costs
142 associated with developing and maintaining it. It has a relatively poor
143 encoding of language semantics - knowledge about Python behavior needs to be
144 encoded by hand and kept up-to-date. At least, Psyco works correctly even when
145 encountering one of the numerous Python constructs it does not support, by
146 falling back to CPython. The PyPy JIT started out as a metaprogrammatic,
147 non-language-specific equivalent of Psyco.
148
149 A different kind of prior art are self-hosting JIT compilers such as Jikes.
150 Jikes is a JIT compiler for Java written in Java. It has a poor encoding of
151 language semantics; it would take an enormous amount of work to encode all the
152 details of a Python-like language directly into a JIT compiler. It also has
153 limited portability, which is an issue for Python; it is likely that large
154 parts of the JIT compiler would need retargetting in order to run in a
155 different environment than the intended low-level one.
156
157 Simply reusing an existing well-tuned JIT like that of the JVM does not
158 really work, because of concept mismatches between the implementor's
159 language and the host VM language: the former needs to be compiled to
160 the target environment in such a way that the JIT is able to speed it up
161 significantly - an approach which essentially has failed in Python so
162 far: even though CPython is a simple interpreter, its Java and .NET
163 re-implementations are not significantly faster.
164
165 More recently, several larger projects have started in the JIT area. For
166 instance, Sun Microsystems is investing in JRuby, which aims to use the Java
167 Hotspot JIT to improve the performance of Ruby. However, this requires a lot of
168 hand crafting and will only provide speedups for one language on one platform.
169 Some issues are delicate, e.g., how to remove the overhead of constantly boxing
170 and unboxing, typical in dynamic languages. An advantage compared to PyPy is
171 that there are some hand optimizations that can be performed, that do not fit
172 in the metaprogramming approach. But metaprogramming makes the PyPy JIT
173 reusable for many different languages on many different execution platforms.
174 It is also possible to combine the approaches - we can get substantial speedups
175 using our JIT and then feed the result to Java's Hotspot JIT for further
176 improvement. One of us is even a member of the `JSR 292`_ Expert Group
177 to define additions to the JVM to better support dynamic languages, and
178 is contributing insights from our JIT research, in ways that will also
179 benefit PyPy.
180
181 Finally, tracing JITs are now emerging for dynamic languages like
182 JavaScript with TraceMonkey. The code generated by PyPy is very similar
183 (but not hand-written) to the concepts of tracing JITs.
184
185 .. _JSR 292: http://jcp.org/en/jsr/detail?id=292
186
187
188 Further reading
189 ---------------
190
191 The description of the current RPython JIT generator is given in :doc:`PyJitPl5 <pyjitpl5>`
192 (draft).