diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-05-02 20:31:14 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-05-02 20:31:14 (GMT) |
commit | db067af12a5ebb889874e47d8177f9c4a3dc9a68 (patch) | |
tree | 87cea7c1fb4a0905ac2632c03520c43f377b3584 /Objects/bytearrayobject.c | |
parent | d50c3f3f3af54f3be46d26d53a1c10b7c15a7b2d (diff) | |
download | cpython-db067af12a5ebb889874e47d8177f9c4a3dc9a68.zip cpython-db067af12a5ebb889874e47d8177f9c4a3dc9a68.tar.gz cpython-db067af12a5ebb889874e47d8177f9c4a3dc9a68.tar.bz2 |
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).
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 5b75705..68b9c4a 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -813,9 +813,21 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) } else { if (count > 0) { - if (PyByteArray_Resize((PyObject *)self, count)) + void *sval; + Py_ssize_t alloc; + + assert (Py_SIZE(self) == 0); + + alloc = count + 1; + sval = PyObject_Calloc(1, alloc); + if (sval == NULL) return -1; - memset(PyByteArray_AS_STRING(self), 0, count); + + PyObject_Free(self->ob_bytes); + + self->ob_bytes = self->ob_start = sval; + Py_SIZE(self) = count; + self->ob_alloc = alloc; } return 0; } |