Skip to content

Commit d16a2ea

Browse files
committed
Use simpler solution to preallocate PushNotification list
1 parent 943611c commit d16a2ea

File tree

1 file changed

+2
-23
lines changed

1 file changed

+2
-23
lines changed

src/reader.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ static int PushNotificationType_init(PushNotificationObject *self, PyObject *arg
221221
return PyList_Type.tp_init((PyObject *)self, args, kwds);
222222
}
223223

224-
/* Create a new instance of PushNotificationType with preallocated number of elements */
225224
static PyObject* PushNotificationType_New(Py_ssize_t size) {
226225
/* Check for negative size */
227226
if (size < 0) {
@@ -240,28 +239,8 @@ static PyObject* PushNotificationType_New(Py_ssize_t size) {
240239
return NULL;
241240
}
242241

243-
/* Cast to PyListObject to access its fields */
244-
PyListObject* op = (PyListObject*)obj;
245-
246-
/* Allocate memory for the list items if size > 0 */
247-
if (size > 0) {
248-
size_t nbytes = (size_t)size * sizeof(PyObject*);
249-
op->ob_item = (PyObject**)PyMem_Malloc(nbytes);
250-
if (op->ob_item == NULL) {
251-
Py_DECREF(obj);
252-
return PyErr_NoMemory();
253-
}
254-
/* Initialize memory to zeros */
255-
memset(op->ob_item, 0, nbytes);
256-
}
257-
258-
/* Set the size and allocated fields */
259-
#if PY_VERSION_HEX >= 0x03090000
260-
Py_SET_SIZE(op, size);
261-
#else
262-
Py_SIZE(op) = size;
263-
#endif
264-
op->allocated = size;
242+
// Simple solution to preallocate the list that works for both CPython and PyPy
243+
PyList_SetSlice(obj, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, PyList_New(size));
265244

266245
return obj;
267246
}

0 commit comments

Comments
 (0)