Since lists are specialized for storing primative ints, would it be possible to do something similar with user defined types? Assuming that most instances of the same type will either store or not store an int in the same attribute, you could optimistically specialize it initially and fallback to the normal code if this invariant is broken.
On Sun, Mar 22, 2015 at 8:21 AM, Carl Friedrich Bolz <cfb...@gmx.de> wrote: > Hey Timothy, > > PyPy has no great solution for this yet. We have played with various > approaches, Armin has described some of them. The simplest one is one that > I've been trying recently on the typed-cells branch: > > The basic idea is that for an attribute that stores an int, the attribute > stores a special MutableIntCell instead. When that value is overwritten > with another int, the value in the int cell is changed instead, thus there > won't be an allocation. If another kind of object is stored in the > attribute, the cell is replaced by a reference to that object. > > On reading an attribute it is then necessary to check whether the > attribute value is such a cell. If it is, the value of the cell needs to be > boxed into a regular W_IntObject. > > Here's the relevant code on the branch (with some extra complication): > > > https://bitbucket.org/pypy/pypy/src/b193ed03acc0c93af48e70f77c6660a4c79aa3ae/pypy/objspace/std/mapdict.py?at=typed-cells#cl-305 > > This approach has the advantage that it's really simple to implement. It > won't make instances that store integers smaller in memory, since the > MutableIntCell needs as much memory as a regular W_IntObject. However, the > GC pressure is reduced a lot when the field is written to often. > > Cheers, > > Carl Friedrich > > > On March 21, 2015 1:43:21 AM GMT+01:00, Timothy Baldridge < > tbaldri...@gmail.com> wrote: > >> I'd like to add some optimization to app level types in Pixie. What I'm >> thinking of is something like this (in app level PyPy code): >> >> >> class Foo(object): >> def __init__(self, some_val): >> self._some_val = some_val >> def set_value(self, val): >> self._some_val = val >> >> In a perfect world the JIT should be able to recognize that ._some_val is >> only ever an int, and therefore store it unboxed in the instance of the >> type, hopefully this would decrease pressure on the GC if ._some_val is >> modified often. Also in a perfect world, the value of _some_val should be >> auto promoted to an object if someone ever decides to set it to something >> besides an int. >> >> How would I go about coding this up in RPython? I can't seem to figure >> out a way to do this without either bloating each instance of the type with >> an array of object, an array of ints and an array of floats. >> >> Currently app level objects in Pixie are just a wrapper around a object >> array. The type then holds the lookups for name->slot_idx. >> >> Thanks in advance. >> >> Timothy Baldridge >> >> >> ------------------------------ >> >> pypy-dev mailing list >> pypy-dev@python.org >> https://mail.python.org/mailman/listinfo/pypy-dev >> >> > _______________________________________________ > pypy-dev mailing list > pypy-dev@python.org > https://mail.python.org/mailman/listinfo/pypy-dev > >
_______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev