[Python-checkins] cpython: Issue #21233: Revert bytearray(int) optimization using calloc()

victor.stinner python-checkins at python.org
Mon Jun 2 22:23:26 CEST 2014


http://hg.python.org/cpython/rev/dff6b4b61cac
changeset:   90981:dff6b4b61cac
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Jun 02 22:22:42 2014 +0200
summary:
  Issue #21233: Revert bytearray(int) optimization using calloc()

files:
  Doc/whatsnew/3.5.rst      |   5 ++---
  Misc/NEWS                 |   7 +++----
  Objects/bytearrayobject.c |  16 ++--------------
  3 files changed, 7 insertions(+), 21 deletions(-)


diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -196,9 +196,8 @@
 
 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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,10 +26,9 @@
   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
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -813,21 +813,9 @@
     }
     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;
     }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list