| 1 |
Standard Interpreter Optimizations |
| 2 |
================================== |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
|
| 6 |
Introduction |
| 7 |
------------ |
| 8 |
|
| 9 |
One of the advantages -- indeed, one of the motivating goals -- of the PyPy |
| 10 |
standard interpreter (compared to CPython) is that of increased flexibility and |
| 11 |
configurability. |
| 12 |
|
| 13 |
One example of this is that we can provide several implementations of the same |
| 14 |
object (e.g. lists) without exposing any difference to application-level |
| 15 |
code. This makes it easy to provide a specialized implementation of a type that |
| 16 |
is optimized for a certain situation without disturbing the implementation for |
| 17 |
the regular case. |
| 18 |
|
| 19 |
This document describes several such optimizations. Most of them are not |
| 20 |
enabled by default. Also, for many of these optimizations it is not clear |
| 21 |
whether they are worth it in practice for a real-world application (they sure |
| 22 |
make some microbenchmarks a lot faster and use less memory, which is not saying |
| 23 |
too much). If you have any observation in that direction, please let us know! |
| 24 |
By the way: alternative object implementations are a great way to get into PyPy |
| 25 |
development since you have to know only a rather small part of PyPy to do |
| 26 |
them. And they are fun too! |
| 27 |
|
| 28 |
.. describe other optimizations! |
| 29 |
|
| 30 |
|
| 31 |
Object Optimizations |
| 32 |
-------------------- |
| 33 |
|
| 34 |
Integer Optimizations |
| 35 |
~~~~~~~~~~~~~~~~~~~~~ |
| 36 |
|
| 37 |
Caching Small Integers |
| 38 |
++++++++++++++++++++++ |
| 39 |
|
| 40 |
Similar to CPython, it is possible to enable caching of small integer objects to |
| 41 |
not have to allocate all the time when doing simple arithmetic. Every time a new |
| 42 |
integer object is created it is checked whether the integer is small enough to |
| 43 |
be retrieved from the cache. |
| 44 |
|
| 45 |
This option is disabled by default, you can enable this feature with the |
| 46 |
:config:`objspace.std.withprebuiltint` option. |
| 47 |
|
| 48 |
|
| 49 |
Integers as Tagged Pointers |
| 50 |
+++++++++++++++++++++++++++ |
| 51 |
|
| 52 |
An even more aggressive way to save memory when using integers is "small int" |
| 53 |
integer implementation. It is another integer implementation used for integers |
| 54 |
that only needs 31 bits (or 63 bits on a 64 bit machine). These integers |
| 55 |
are represented as tagged pointers by setting their lowest bits to distinguish |
| 56 |
them from normal pointers. This completely avoids the boxing step, saving |
| 57 |
time and memory. |
| 58 |
|
| 59 |
You can enable this feature with the :config:`objspace.std.withsmalllong` option. |
| 60 |
|
| 61 |
|
| 62 |
Dictionary Optimizations |
| 63 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 |
|
| 65 |
Dict Strategies |
| 66 |
++++++++++++++++ |
| 67 |
|
| 68 |
Dict strategies are an implementation approach for dictionaries (and lists) |
| 69 |
that make it possible to use a specialized representation of the dictionary's |
| 70 |
data, while still being able to switch back to a general representation should |
| 71 |
that become necessary later. |
| 72 |
|
| 73 |
Dict strategies are always enabled, by default there are special strategies for |
| 74 |
dicts with just string keys, just unicode keys and just integer keys. If one of |
| 75 |
those specialized strategies is used, then dict lookup can use much faster |
| 76 |
hashing and comparison for the dict keys. There is of course also a strategy |
| 77 |
for general keys. |
| 78 |
|
| 79 |
|
| 80 |
Identity Dicts |
| 81 |
+++++++++++++++ |
| 82 |
|
| 83 |
We also have a strategy specialized for keys that are instances of classes |
| 84 |
which compares "by identity", which is the default unless you override |
| 85 |
``__hash__``, ``__eq__`` or ``__cmp__``. This strategy will be used only with |
| 86 |
new-style classes. |
| 87 |
|
| 88 |
|
| 89 |
Map Dicts |
| 90 |
+++++++++++++ |
| 91 |
|
| 92 |
Map dictionaries are a special representation used together with dict strategies. |
| 93 |
This dict strategy is used only for instance dictionaries and tries to |
| 94 |
make instance dictionaries use less memory (in fact, usually memory behaviour |
| 95 |
should be mostly like that of using ``__slots__``). |
| 96 |
|
| 97 |
The idea is the following: Most instances of the same class have very similar |
| 98 |
attributes, and are even adding these keys to the dictionary in the same order |
| 99 |
while ``__init__()`` is being executed. That means that all the dictionaries of |
| 100 |
these instances look very similar: they have the same set of keys with different |
| 101 |
values per instance. What sharing dicts do is store these common keys into a |
| 102 |
common structure object and thus save the space in the individual instance |
| 103 |
dicts: |
| 104 |
the representation of the instance dict contains only a list of values. |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
User Class Optimizations |
| 109 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 110 |
|
| 111 |
Method Caching |
| 112 |
++++++++++++++ |
| 113 |
|
| 114 |
A method cache is introduced where the result of a method lookup |
| 115 |
is stored (which involves potentially many lookups in the base classes of a |
| 116 |
class). Entries in the method cache are stored using a hash computed from |
| 117 |
the name being looked up, the call site (i.e. the bytecode object and |
| 118 |
the current program counter), and a special "version" of the type where the |
| 119 |
lookup happens (this version is incremented every time the type or one of its |
| 120 |
base classes is changed). On subsequent lookups the cached version can be used, |
| 121 |
as long as the instance did not shadow any of its classes attributes. |
| 122 |
|
| 123 |
This feature is enabled by default. |
| 124 |
|
| 125 |
|
| 126 |
Interpreter Optimizations |
| 127 |
------------------------- |
| 128 |
|
| 129 |
Special Bytecodes |
| 130 |
~~~~~~~~~~~~~~~~~ |
| 131 |
|
| 132 |
.. _lookup method call method: |
| 133 |
|
| 134 |
LOOKUP_METHOD & CALL_METHOD |
| 135 |
+++++++++++++++++++++++++++ |
| 136 |
|
| 137 |
An unusual feature of Python's version of object oriented programming is the |
| 138 |
concept of a "bound method". While the concept is clean and powerful, the |
| 139 |
allocation and initialization of the object is not without its performance cost. |
| 140 |
We have implemented a pair of bytecodes that alleviate this cost. |
| 141 |
|
| 142 |
For a given method call ``obj.meth(x, y)``, the standard bytecode looks like |
| 143 |
this:: |
| 144 |
|
| 145 |
LOAD_GLOBAL obj # push 'obj' on the stack |
| 146 |
LOAD_ATTR meth # read the 'meth' attribute out of 'obj' |
| 147 |
LOAD_GLOBAL x # push 'x' on the stack |
| 148 |
LOAD_GLOBAL y # push 'y' on the stack |
| 149 |
CALL_FUNCTION 2 # call the 'obj.meth' object with arguments x, y |
| 150 |
|
| 151 |
We improved this by keeping method lookup separated from method call, unlike |
| 152 |
some other approaches, but using the value stack as a cache instead of building |
| 153 |
a temporary object. We extended the bytecode compiler to (optionally) generate |
| 154 |
the following code for ``obj.meth(x, y)``:: |
| 155 |
|
| 156 |
LOAD_GLOBAL obj |
| 157 |
LOOKUP_METHOD meth |
| 158 |
LOAD_GLOBAL x |
| 159 |
LOAD_GLOBAL y |
| 160 |
CALL_METHOD 2 |
| 161 |
|
| 162 |
``LOOKUP_METHOD`` contains exactly the same attribute lookup logic as |
| 163 |
``LOAD_ATTR`` - thus fully preserving semantics - but pushes two values onto the |
| 164 |
stack instead of one. These two values are an "inlined" version of the bound |
| 165 |
method object: the *im_func* and *im_self*, i.e. respectively the underlying |
| 166 |
Python function object and a reference to ``obj``. This is only possible when |
| 167 |
the attribute actually refers to a function object from the class; when this is |
| 168 |
not the case, ``LOOKUP_METHOD`` still pushes two values, but one *(im_func)* is |
| 169 |
simply the regular result that ``LOAD_ATTR`` would have returned, and the other |
| 170 |
*(im_self)* is an interpreter-level None placeholder. |
| 171 |
|
| 172 |
After pushing the arguments, the layout of the stack in the above |
| 173 |
example is as follows (the stack grows upwards): |
| 174 |
|
| 175 |
+---------------------------------+ |
| 176 |
| ``y`` *(2nd arg)* | |
| 177 |
+---------------------------------+ |
| 178 |
| ``x`` *(1st arg)* | |
| 179 |
+---------------------------------+ |
| 180 |
| ``obj`` *(im_self)* | |
| 181 |
+---------------------------------+ |
| 182 |
| ``function object`` *(im_func)* | |
| 183 |
+---------------------------------+ |
| 184 |
|
| 185 |
The ``CALL_METHOD N`` bytecode emulates a bound method call by |
| 186 |
inspecting the *im_self* entry in the stack below the ``N`` arguments: |
| 187 |
if it is not None, then it is considered to be an additional first |
| 188 |
argument in the call to the *im_func* object from the stack. |
| 189 |
|
| 190 |
.. more here? |
| 191 |
|
| 192 |
|
| 193 |
Overall Effects |
| 194 |
--------------- |
| 195 |
|
| 196 |
The impact these various optimizations have on performance unsurprisingly |
| 197 |
depends on the program being run. Using the default multi-dict implementation that |
| 198 |
simply special cases string-keyed dictionaries is a clear win on all benchmarks, |
| 199 |
improving results by anything from 15-40 per cent. |
| 200 |
|
| 201 |
Another optimization, or rather set of optimizations, that has a uniformly good |
| 202 |
effect are the two 'method optimizations', i.e. the |
| 203 |
method cache and the LOOKUP_METHOD and CALL_METHOD opcodes. On a heavily |
| 204 |
object-oriented benchmark (richards) they combine to give a speed-up of nearly |
| 205 |
50%, and even on the extremely un-object-oriented pystone benchmark, the |
| 206 |
improvement is over 20%. |
| 207 |
|
| 208 |
When building pypy, all generally useful optimizations are turned on by default |
| 209 |
unless you explicitly lower the translation optimization level with the |
| 210 |
``--opt`` option. |