From: Victor Stinner Date: Mon, 2 Jun 2014 20:22:42 +0000 (+0200) Subject: Issue #21233: Revert bytearray(int) optimization using calloc() X-Git-Tag: v3.5.0a1~1535 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2bc4d95bb67a0bcddec5b76e7f7b5d10b098aa49;p=python Issue #21233: Revert bytearray(int) optimization using calloc() --- diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst index b879edece4..5c4e0b5619 100644 --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -196,9 +196,8 @@ Optimizations The following performance enhancements have been added: -* 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 +* Construction of ``bytes(int)`` (filled by zero bytes) is faster and use less + memory for large objects. ``calloc()`` is used instead of ``malloc()`` to allocate memory for these objects. * Some operations on :class:`~ipaddress.IPv4Network` and diff --git a/Misc/NEWS b/Misc/NEWS index be02bba58a..af084b86e0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,10 +26,9 @@ Core and Builtins internal iteration logic. - 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). + PyObject_Calloc(), _PyObject_GC_Calloc(). bytes(int) is now using + ``calloc()`` instead of ``malloc()`` for large objects which is faster and + use less memory. - Issue #21377: PyBytes_Concat() now tries to concatenate in-place when the first argument has a reference count of 1. Patch by Nikolaus Rath. diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 68b9c4a22f..5b75705abf 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -813,21 +813,9 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) } else { if (count > 0) { - void *sval; - Py_ssize_t alloc; - - assert (Py_SIZE(self) == 0); - - alloc = count + 1; - sval = PyObject_Calloc(1, alloc); - if (sval == NULL) + if (PyByteArray_Resize((PyObject *)self, count)) return -1; - - PyObject_Free(self->ob_bytes); - - self->ob_bytes = self->ob_start = sval; - Py_SIZE(self) = count; - self->ob_alloc = alloc; + memset(PyByteArray_AS_STRING(self), 0, count); } return 0; }