Issue #21233: Add new C functions: PyMem_RawCalloc(), PyMem_Calloc(),

PyObject_Calloc(), _PyObject_GC_Calloc(). bytes(int) and bytearray(int) are now
using ``calloc()`` instead of ``malloc()`` for large objects which is faster
and use less memory (until the bytearray buffer is filled with data).
This commit is contained in:
Victor Stinner 2014-05-02 22:31:14 +02:00
parent d50c3f3f3a
commit db067af12a
11 changed files with 366 additions and 71 deletions

View file

@ -92,8 +92,8 @@ functions are thread-safe, the :term:`GIL <global interpreter lock>` does not
need to be held.
The default raw memory block allocator uses the following functions:
:c:func:`malloc`, :c:func:`realloc` and :c:func:`free`; call ``malloc(1)`` when
requesting zero bytes.
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`; call
``malloc(1)`` (or ``calloc(1, 1)``) when requesting zero bytes.
.. versionadded:: 3.4
@ -106,6 +106,17 @@ requesting zero bytes.
been initialized in any way.
.. c:function:: void* PyMem_RawCalloc(size_t nelem, size_t elsize)
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
request fails. The memory is initialized to zeros. Requesting zero elements
or elements of size zero bytes returns a distinct non-*NULL* pointer if
possible, as if ``PyMem_RawCalloc(1, 1)`` had been called instead.
.. versionadded:: 3.5
.. c:function:: void* PyMem_RawRealloc(void *p, size_t n)
Resizes the memory block pointed to by *p* to *n* bytes. The contents will
@ -136,8 +147,8 @@ behavior when requesting zero bytes, are available for allocating and releasing
memory from the Python heap.
The default memory block allocator uses the following functions:
:c:func:`malloc`, :c:func:`realloc` and :c:func:`free`; call ``malloc(1)`` when
requesting zero bytes.
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`; call
``malloc(1)`` (or ``calloc(1, 1)``) when requesting zero bytes.
.. warning::
@ -152,6 +163,17 @@ requesting zero bytes.
been called instead. The memory will not have been initialized in any way.
.. c:function:: void* PyMem_Calloc(size_t nelem, size_t elsize)
Allocates *nelem* elements each whose size in bytes is *elsize* and returns
a pointer of type :c:type:`void\*` to the allocated memory, or *NULL* if the
request fails. The memory is initialized to zeros. Requesting zero elements
or elements of size zero bytes returns a distinct non-*NULL* pointer if
possible, as if ``PyMem_Calloc(1, 1)`` had been called instead.
.. versionadded:: 3.5
.. c:function:: void* PyMem_Realloc(void *p, size_t n)
Resizes the memory block pointed to by *p* to *n* bytes. The contents will be
@ -222,11 +244,17 @@ Customize Memory Allocators
+----------------------------------------------------------+---------------------------------------+
| ``void* malloc(void *ctx, size_t size)`` | allocate a memory block |
+----------------------------------------------------------+---------------------------------------+
| ``void* calloc(void *ctx, size_t nelem, size_t elsize)`` | allocate a memory block initialized |
| | with zeros |
+----------------------------------------------------------+---------------------------------------+
| ``void* realloc(void *ctx, void *ptr, size_t new_size)`` | allocate or resize a memory block |
+----------------------------------------------------------+---------------------------------------+
| ``void free(void *ctx, void *ptr)`` | free a memory block |
+----------------------------------------------------------+---------------------------------------+
.. versionchanged:: 3.5
Add a new field ``calloc``.
.. c:type:: PyMemAllocatorDomain
Enum used to identify an allocator domain. Domains:

View file

@ -164,7 +164,10 @@ Optimizations
Major performance enhancements have been added:
* None yet.
* Construction of ``bytes(int)`` and ``bytearray(int)`` (filled by zero bytes)
is faster and use less memory (until the bytearray buffer is filled with
data) for large objects. ``calloc()`` is used instead of ``malloc()`` to
allocate memory for these objects.
Build and C API Changes
@ -172,7 +175,12 @@ Build and C API Changes
Changes to Python's build process and to the C API include:
* None yet.
* New ``calloc`` functions:
* :c:func:`PyMem_RawCalloc`
* :c:func:`PyMem_Calloc`
* :c:func:`PyObject_Calloc`
* :c:func:`_PyObject_GC_Calloc`
Deprecated
@ -209,6 +217,9 @@ Porting to Python 3.5
This section lists previously described changes and other bugfixes
that may require changes to your code.
Changes in the Python API
-------------------------
* Before Python 3.5, a :class:`datetime.time` object was considered to be false
if it represented midnight in UTC. This behavior was considered obscure and
error-prone and has been removed in Python 3.5. See :issue:`13936` for full
@ -217,3 +228,8 @@ that may require changes to your code.
* :meth:`ssl.SSLSocket.send()` now raises either :exc:`ssl.SSLWantReadError`
or :exc:`ssl.SSLWantWriteError` on a non-blocking socket if the operation
would block. Previously, it would return 0. See :issue:`20951`.
Changes in the C API
--------------------
* The :c:type:`PyMemAllocator` structure has a new ``calloc`` field.