[Python-Dev] "Global freepool"

Antoine Pitrou solipsis at pitrou.net
Thu Jun 1 04:40:21 EDT 2017


On Thu, 1 Jun 2017 09:57:04 +0200
Victor Stinner <victor.stinner at gmail.com> wrote:
> 
> By the way, Naoki INADA also proposed a different idea:
> 
> "Global freepool: Many types has it’s own freepool. Sharing freepool
> can increase memory and cache efficiency. Add PyMem_FastFree(void*
> ptr, size_t size) to store memory block to freepool, and PyMem_Malloc
> can check global freepool first."

This is already exactly how PyObject_Malloc() works.  Really, the fast
path for PyObject_Malloc() is:

        size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
        pool = usedpools[size + size];
        if (pool != pool->nextpool) {
            /*
             * There is a used pool for this size class.
             * Pick up the head block of its free list.
             */
            ++pool->ref.count;
            bp = pool->freeblock;
            assert(bp != NULL);
            if ((pool->freeblock = *(block **)bp) != NULL) {
                UNLOCK();
                return (void *)bp;   // <- fast path!
            }


I don't think you can get much faster than that in a generic allocation
routine (unless you have a compacting GC where allocating memory is
basically bumping a single global pointer). IMHO the main thing the
private freelists have is that they're *private* precisely, so they can
avoid a couple of conditional branches.

Regards

Antoine.




More information about the Python-Dev mailing list