| 1 |
rstrategies |
| 2 |
=========== |
| 3 |
|
| 4 |
A library to implement storage strategies in VMs based on the RPython |
| 5 |
toolchain. rstrategies can be used in VMs for any language or language |
| 6 |
family. |
| 7 |
|
| 8 |
This library has been developed as part of a Masters Thesis by `Anton |
| 9 |
Gulenko <https://github.com/antongulenko>`__. |
| 10 |
|
| 11 |
The original paper describing the optimization "Storage Strategies for |
| 12 |
collections in dynamically typed languages" by C.F. Bolz, L. Diekmann |
| 13 |
and L. Tratt can be found |
| 14 |
`here <http://stups.hhu.de/mediawiki/images/3/3b/Pub-BoDiTr13_246.pdf>`__. |
| 15 |
|
| 16 |
So far, this library has been adpoted by 3 VMs: |
| 17 |
`RSqueak <https://github.com/HPI-SWA-Lab/RSqueak>`__, |
| 18 |
`Topaz <https://github.com/topazproject/topaz>`__ (`Forked |
| 19 |
here <https://github.com/antongulenko/topaz/tree/rstrategies>`__) and |
| 20 |
`Pycket <https://github.com/samth/pycket>`__ (`Forked |
| 21 |
here <https://github.com/antongulenko/pycket/tree/rstrategies>`__). |
| 22 |
|
| 23 |
Concept |
| 24 |
------- |
| 25 |
|
| 26 |
Collections are often used homogeneously, i.e. they contain only objects |
| 27 |
of the same type. Primitive numeric types like ints or floats are |
| 28 |
especially interesting for optimization. These cases can be optimized by |
| 29 |
storing the unboxed data of these objects in consecutive memory. This is |
| 30 |
done by letting a special "strategy" object handle the entire storage of |
| 31 |
a collection. The collection object holds two separate references: one |
| 32 |
to its strategy and one to its storage. Every operation on the |
| 33 |
collection is delegated to the strategy, which accesses the storage when |
| 34 |
needed. The strategy can be switched to a more suitable one, which might |
| 35 |
require converting the storage array. |
| 36 |
|
| 37 |
Usage |
| 38 |
~~~~~ |
| 39 |
|
| 40 |
The following are the steps needed to integrated rstrategies in an |
| 41 |
RPython VM. Because of the special nature of this library it is not |
| 42 |
enough to simply call some API methods; the library must be integrated |
| 43 |
within existing VM classes using a metaclass, mixins and other |
| 44 |
meta-programming techniques. |
| 45 |
|
| 46 |
The sequence of steps described here is something like a "setup |
| 47 |
walkthrough", and might be a bit abstract. To see a concrete example, |
| 48 |
look at |
| 49 |
`SingletonStorageStrategy <https://github.com/HPI-SWA-Lab/RSqueak/blob/d048f713002c01c9b121c80e8eb9bea33ed742d6/spyvm/storage.py#L73>`__, |
| 50 |
`StrategyFactory <https://github.com/HPI-SWA-Lab/RSqueak/blob/d048f713002c01c9b121c80e8eb9bea33ed742d6/spyvm/storage.py#L126>`__ |
| 51 |
and |
| 52 |
`W\_PointersObject <https://github.com/HPI-SWA-Lab/RSqueak/blob/d048f713002c01c9b121c80e8eb9bea33ed742d6/spyvm/model.py#L616>`__ |
| 53 |
from the `RSqueak VM <https://github.com/HPI-SWA-Lab/RSqueak>`__. The |
| 54 |
code is also well commented. |
| 55 |
|
| 56 |
Basics |
| 57 |
------- |
| 58 |
|
| 59 |
Currently the rstrategies library supports fixed sized and variable |
| 60 |
sized collections. This can be used to optimize a wide range of |
| 61 |
primitive data structures like arrays, lists or regular objects. Any of |
| 62 |
these are called 'collections' in this context. The VM should have a |
| 63 |
central class or class hierarchy for collections. In order to extend |
| 64 |
these classes and use strategies, the library needs accessor methods for |
| 65 |
two attributes of collection objects: strategy and storage. The easiest |
| 66 |
way is adding the following line to the body of the root collection |
| 67 |
class: |
| 68 |
|
| 69 |
:: |
| 70 |
|
| 71 |
rstrategies.make_accessors(strategy='strategy', storage='storage') |
| 72 |
|
| 73 |
This will generate the 4 accessor methods |
| 74 |
``_[get/set]_[storage/strategy]()`` for the respective attributes. |
| 75 |
Alternatively, implement these methods manually or overwrite the |
| 76 |
getters/setters in ``StrategyFactory``. |
| 77 |
|
| 78 |
Next, the strategy classes must be defined. This requires a small class |
| 79 |
hierarchy with a dedicated root class. In the definition of this root |
| 80 |
class, include the following lines: |
| 81 |
|
| 82 |
:: |
| 83 |
|
| 84 |
__metaclass__ = rstrategies.StrategyMetaclass |
| 85 |
import_from_mixin(rstrategies.AbstractStrategy) |
| 86 |
import_from_mixin(rstrategies.SafeIndexingMixin) |
| 87 |
|
| 88 |
``import_from_mixin`` can be found in ``rpython.rlib.objectmodel``. If |
| 89 |
index-checking is performed safely at other places in the VM, you can |
| 90 |
use ``rstrategies.UnsafeIndexingMixin`` instead. If you need your own |
| 91 |
metaclass, you can combine yours with the rstrategies one using multiple |
| 92 |
inheritance `like |
| 93 |
here <https://github.com/HPI-SWA-Lab/RSqueak/blob/d5ff2572106d23a5246884de6f8b86f46d85f4f7/spyvm/storage_contexts.py#L24>`__. |
| 94 |
Also implement a ``storage_factory()`` method, which returns an instance |
| 95 |
of ``rstrategies.StorageFactory``, which is described below. |
| 96 |
|
| 97 |
An example ``AbstractStrategy`` class, which also stores an additional ``space`` parameter could looks like this: |
| 98 |
|
| 99 |
:: |
| 100 |
|
| 101 |
class AbstractStrategy(AbstractStrategy): |
| 102 |
_attrs_ = ['space'] |
| 103 |
_immutable_fields_ = ['space'] |
| 104 |
__metaclass__ = rstrat.StrategyMetaclass |
| 105 |
import_from_mixin(rstrat.AbstractStrategy) |
| 106 |
import_from_mixin(rstrategies.SafeIndexingMixin) |
| 107 |
|
| 108 |
def __init__(self, space): |
| 109 |
self.space = space |
| 110 |
|
| 111 |
def strategy_factory(self): |
| 112 |
return self.space.strategy_factory |
| 113 |
|
| 114 |
|
| 115 |
Strategy classes |
| 116 |
---------------- |
| 117 |
|
| 118 |
Now you can create the actual strategy classes, subclassing them from |
| 119 |
the single root class. The following list summarizes the basic |
| 120 |
strategies available. |
| 121 |
|
| 122 |
- ``EmptyStrategy`` A strategy for empty collections; very efficient, but limited. Does not allocate anything. |
| 123 |
- ``SingleValueStrategy`` A strategy for collections containing the same object ``n`` times. Only allocates memory to store the size of the collection. |
| 124 |
- ``GenericStrategy`` A non-optimized strategy backed by a generic python list. This is the fallback strategy, since it can store everything, but is not optimized. |
| 125 |
- ``WeakGenericStrategy`` Like ``GenericStrategy``, but uses ``weakref`` to hold on weakly to its elements. |
| 126 |
- ``SingleTypeStrategy`` Can store a single unboxed type like int or float. This is the main optimizing strategy |
| 127 |
- ``TaggingStrategy`` Extension of SingleTypeStrategy. Uses a specific value in the value range of the unboxed type to represent one additional, arbitrary object. For example, one of ``float``'s ``NaN`` representations can be used to represent special value like ``nil``. |
| 128 |
|
| 129 |
There are also intermediate classes, which allow creating new, more |
| 130 |
customized strategies. For this, you should get familiar with the code. |
| 131 |
|
| 132 |
Include one of these mixin classes using ``import_from_mixin``. The |
| 133 |
mixin classes contain comments describing methods or fields which are |
| 134 |
also required in the strategy class in order to use them. Additionally, |
| 135 |
add the ``@rstrategies.strategy(generalize=alist)`` decorator to all |
| 136 |
strategy classes. The ``alist`` parameter must contain all strategies, |
| 137 |
which the decorated strategy can switch to, if it can not represent a |
| 138 |
new element anymore. |
| 139 |
`Example <https://github.com/HPI-SWA-Lab/RSqueak/blob/d5ff2572106d23a5246884de6f8b86f46d85f4f7/spyvm/storage.py#L87>`__ |
| 140 |
for an implemented strategy. See the other strategy classes behind this |
| 141 |
link for more examples. |
| 142 |
|
| 143 |
An example strategy class for optimized ``int`` storage could look like this: |
| 144 |
|
| 145 |
:: |
| 146 |
|
| 147 |
@rstrat.strategy(generalize=[GenericStrategy]) |
| 148 |
class IntegerOrNilStrategy(AbstractStrategy): |
| 149 |
import_from_mixin(rstrat.TaggingStrategy) |
| 150 |
contained_type = model.W_Integer |
| 151 |
def wrap(self, val): return self.space.wrap_int(val) |
| 152 |
def unwrap(self, w_val): return self.space.unwrap_int(w_val) |
| 153 |
def wrapped_tagged_value(self): return self.space.w_nil |
| 154 |
def unwrapped_tagged_value(self): return constants.MAXINT |
| 155 |
|
| 156 |
Strategy Factory |
| 157 |
---------------- |
| 158 |
|
| 159 |
The last part is subclassing ``rstrategies.StrategyFactory``, |
| 160 |
overwriting the method ``instantiate_strategy`` if necessary and passing |
| 161 |
the strategies root class to the constructor. The factory provides the |
| 162 |
methods ``switch_strategy``, ``set_initial_strategy``, |
| 163 |
``strategy_type_for`` which can be used by the VM code to use the |
| 164 |
mechanism behind strategies. See the comments in the source code. |
| 165 |
|
| 166 |
The strategy mixins offer the following methods to manipulate the |
| 167 |
contents of the collection: |
| 168 |
|
| 169 |
- basic API |
| 170 |
|
| 171 |
- ``size`` |
| 172 |
|
| 173 |
- fixed size API |
| 174 |
|
| 175 |
- ``store``, ``fetch``, ``slice``, ``store_all``, ``fetch_all`` |
| 176 |
|
| 177 |
- variable size API |
| 178 |
|
| 179 |
- ``insert``, ``delete``, ``append``, ``pop`` |
| 180 |
|
| 181 |
If the collection has a fixed size, simply never use any of the variable |
| 182 |
size methods in the VM code. Since the strategies are singletons, these |
| 183 |
methods need the collection object as first parameter. For convenience, |
| 184 |
more fitting accessor methods should be implemented on the collection |
| 185 |
class itself. |
| 186 |
|
| 187 |
An example strategy factory for the ``AbstractStrategy`` class above could look like this: |
| 188 |
|
| 189 |
:: |
| 190 |
|
| 191 |
class StrategyFactory(rstrategies.StrategyFactory): |
| 192 |
_attrs_ = ['space'] |
| 193 |
_immutable_fields_ = ['space'] |
| 194 |
|
| 195 |
def __init__(self, space): |
| 196 |
self.space = space |
| 197 |
rstrat.StrategyFactory.__init__(self, AbstractStrategy) |
| 198 |
|
| 199 |
def instantiate_strategy(self, strategy_type): |
| 200 |
return strategy_type(self.space) |
| 201 |
|
| 202 |
def strategy_type_for(self, list_w, weak=False): |
| 203 |
""" |
| 204 |
Helper method for handling weak objects specially |
| 205 |
""" |
| 206 |
if weak: |
| 207 |
return WeakListStrategy |
| 208 |
return rstrategies.StrategyFactory.strategy_type_for(self, list_w) |
| 209 |
|