Freelist in PyPy? Reuse short lived objects?

PIERRE AUGIER <[email protected]>
Newsgroups gmane.comp.python.pypy
Message-ID <1919451462.10447507.1610638452697.JavaMail.zimbra@univ-grenoble-alpes.fr>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.