mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
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:
parent
d50c3f3f3a
commit
db067af12a
11 changed files with 366 additions and 71 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue