[New-bugs-announce] [issue22222] dtoa.c: remove custom memory allocator

STINNER Victor report at bugs.python.org
Sun Aug 17 21:55:29 CEST 2014

New submission from STINNER Victor:

dtoa.c has an optimized memory allocator for performances: it uses a short pool of 2304 doubles (18 KB) to avoid calling malloc/free. Comment in dtoa.c:

/* Memory management: memory is allocated from, and returned to, Kmax+1 pools
   of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
   1 << k.  These pools are maintained as linked lists, with freelist[k]
   pointing to the head of the list for pool k.

   On allocation, if there's no free slot in the appropriate pool, MALLOC is
   called to get more memory.  This memory is not returned to the system until
   Python quits.  There's also a private memory pool that's allocated from
   in preference to using MALLOC.

   For Bigints with more than (1 << Kmax) digits (which implies at least 1233
   decimal digits), memory is directly allocated using MALLOC, and freed using

   XXX: it would be easy to bypass this memory-management system and
   translate each call to Balloc into a call to PyMem_Malloc, and each
   Bfree to PyMem_Free.  Investigate whether this has any significant
   performance on impact. */

Python already has such memory pool: PyObject_Malloc(). I propose to reuse it. It avoids wasting memory "until Python quits" just to optimize dtoa.c.

dtoa.c memory pool is only used for allocations smaller than 68 bytes (on 64 bits system, Kmax=7). PyObject_Malloc() is optimized for allocations smaller than 513 bytes, so it's ok.

See also the issue #7632.

files: dtoa_remove_custom_alloc.patch
keywords: patch
messages: 225465
nosy: haypo, mark.dickinson, skrah
priority: normal
severity: normal
status: open
title: dtoa.c: remove custom memory allocator
versions: Python 3.5
Added file: http://bugs.python.org/file36400/dtoa_remove_custom_alloc.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list