[pypy-dev] Freelist in PyPy? Reuse short lived objects?

Timothy Baldridge tbaldridge at gmail.com
Thu Jan 14 11:05:17 EST 2021

Well if you write this in pure Python and run it via PyPy I imagine
that most of the time the Point objects won't be created at all, as
the JIT will detect that the are created and don't escape the scope of
the JIT loop, so they can be ripped apart and stored in locals. But
also these sort of optimizations make less sense in a GC environment
where allocation is (almost) free, and the cost of freeing objects is
lowered due to bulk reclaiming of objects.

I'd try writing some tests in pure Python, running PyPy with jit
tracing and see what it spits out in the log.

On Thu, Jan 14, 2021 at 8:34 AM PIERRE AUGIER
<pierre.augier at univ-grenoble-alpes.fr> wrote:
> Hello,
> I was still playing with the idea to speedup codes using small numerical objects.
> I wrote a Cython extension which defines a Point (3d) cdef class and a Points cdef class (a vector of points). Both classes contain a pointer towards a point_ C struct:
> ctypedef struct point_:
>     float x, y, z
> Of course, any computation with Point objects with involved several very short lived objects and we really want to avoid all the associated malloc/free calls.
> In Cython, one can decorate a cdef class with `@cython.freelist(8)` to reused objects: https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#fast-instantiation
> I try to add a bit of logic to avoid freeing and allocating the memory for the struct (https://github.com/paugier/nbabel/blob/master/py/microbench/util_cython.pyx). If I understand correctly, doing such things is possible in CPython because the method __dealloc__ is called as soon as the objects is not accessible from Python. Or we can use the fact that it's very fast to get the reference count of an instance. But I think it is not the case for PyPy.
> Is there an alternative strategy efficient with PyPy?
> Pierre
> _______________________________________________
> pypy-dev mailing list
> pypy-dev at python.org
> https://mail.python.org/mailman/listinfo/pypy-dev

“One of the main causes of the fall of the Roman Empire was
that–lacking zero–they had no way to indicate successful termination
of their C programs.”
(Robert Firth)

More information about the pypy-dev mailing list