STINNER Victor <victor.stin...@gmail.com> added the comment:

I added _PyTraceMalloc_Track() and _PyTraceMalloc_Untrack() private functions 
to the C API in Python 3.6. These functions were made public in Python 3.7: 
renamed to PyTraceMalloc_Track() and PyTraceMalloc_Untrack(). I made this 
change to allow numpy to trace memory allocations, to debug memory leaks.

numpy cannot use Python memory allocators because numpy requires aligned memory 
blocks which are required to use CPU SIMD instructions.


Stefan Krah:
> I think many people would welcome this in scientific computing: The Arrow 
> memory format for example recommends 64 bit alignment.

Ah, that's an interesting use case.

I created attached PR 4089 to implement PyMem_AlignedAlloc():

   void* PyMem_AlignedAlloc(size_t alignment, size_t size);
   void PyMem_AlignedFree(void *ptr);

Memory allocated by PyMem_AlignedAlloc() must be freed with PyMem_AlignedFree().

We cannot reuse PyMem_Free(). On Windows, PyMem_AlignedAlloc() is implemented 
with _aligned_malloc() which requires to release the memory with 
_aligned_free().


Raymond Hettinger:
>> Adding yet another API to allocate memory has a cost
> Please don't FUD this one to death.

Statistics (size) on my PR:

 Doc/c-api/memory.rst                              |  43 +-
 Doc/whatsnew/3.7.rst                              |   4 +
 Include/internal/mem.h                            |   6 +-
 Include/objimpl.h                                 |   2 +
 Include/pymem.h                                   |  16 +-
 .../2017-10-23-19-03-38.bpo-18835.8XEjtG.rst      |   9 +
 Modules/_testcapimodule.c                         |  83 ++-
 Modules/_tracemalloc.c                            | 131 +++-
 Objects/obmalloc.c                                | 616 +++++++++++++------
 9 files changed, 655 insertions(+), 255 deletions(-)

That's quite a big change "just to add PyMem_AlignedAlloc()" :-)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue18835>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to