STINNER Victor added the comment:

Patch version 5. This patch is ready for a review.

Summary of calloc-5.patch:

- add the following functions:

  * void* PyMem_RawCalloc(size_t nelem, size_t elsize)
  * void* PyMem_Calloc(size_t nelem, size_t elsize)
  * void* PyObject_Calloc(size_t nelem, size_t elsize)
  * PyObject* _PyObject_GC_Calloc(size_t basicsize)

- add "void* calloc(void *ctx, size_t nelem, size_t elsize)" field to the 
PyMemAllocator structure
- optimize bytes(n) and bytearray(n) to allocate objects using calloc() instead 
of malloc()
- update tracemalloc to trace also calloc()
- document new functions and add unit tests for the calloc "hook" (in _testcapi)


Changes between versions 4 and 5:

- revert all changes except bytes(n) and bytearray(n) of use_calloc.patch: they 
were useless according to benchmarks
- _PyObject_GC_Calloc() now takes a single parameter
- add versionadded and versionchanged fields in the documentation


According to benchmarks, calloc() is only useful for large allocation (1 MB?) 
if only a part of the memory block is modified (to non-zero bytes) just after 
the allocation. Untouched memory pages don't use physical memory and don't use 
RSS memory pages, but it is possible to read their content (null bytes). Using 
calloc() instead of malloc()+memset(0) doens't look to be faster (it may be a 
little bit slower) if all bytes are set just after the allocation.

I chose to only use one parameter for _PyObject_GC_Calloc() because this 
function is used to allocate Python objects. A structure of a Python object 
must start with PyObject_HEAD or PyObject_VAR_HEAD and so the total size of an 
object cannot be expressed as NELEM * ELEMSIZE.

I have no use case for _PyObject_GC_Calloc(), but it makes sense to use it to 
allocate a large Python object tracked by the GC and using a single memory 
block for the Python header + data.

PyObject_Calloc() simply use memset(0) for small objects (<= 512 bytes). It 
delegates the allocation to PyMem_RawCalloc(), and so indirectly to calloc(), 
for larger objects.

Note: use_calloc.patch is no more needed, I merged the two patches since only 
bytes(n) and bytearray(n) now use calloc().

----------
Added file: http://bugs.python.org/file35072/calloc-5.patch

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

Reply via email to