From 6624e6854694315c25981f6bb6cfd360798169c5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 21 May 2003 05:58:46 +0000 Subject: [PATCH] SF bug #604716: faster [None]*n or []*n Fulfilled request to special case repetitions of lists of length 0 or 1. --- Objects/listobject.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Objects/listobject.c b/Objects/listobject.c index 48f3d7d6ea0..7d035064122 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -421,14 +421,26 @@ list_repeat(PyListObject *a, int n) int size; PyListObject *np; PyObject **p; + PyObject *elem; if (n < 0) n = 0; size = a->ob_size * n; + if (size == 0) + return PyList_New(0); if (n && size/n != a->ob_size) return PyErr_NoMemory(); np = (PyListObject *) PyList_New(size); if (np == NULL) return NULL; + + if (a->ob_size == 1) { + elem = a->ob_item[0]; + for (i = 0; i < n; i++) { + np->ob_item[i] = elem; + Py_INCREF(elem); + } + return (PyObject *) np; + } p = np->ob_item; for (i = 0; i < n; i++) { for (j = 0; j < a->ob_size; j++) {