diff options
author | Raymond Hettinger <python@rcn.com> | 2003-05-21 05:58:46 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-05-21 05:58:46 (GMT) |
commit | 6624e6854694315c25981f6bb6cfd360798169c5 (patch) | |
tree | f20023508dbc079ec0af23584fc97aa6108b6e31 /Objects | |
parent | 28137a09d69fd098b82b3386c92949ad60aebef4 (diff) | |
download | cpython-6624e6854694315c25981f6bb6cfd360798169c5.zip cpython-6624e6854694315c25981f6bb6cfd360798169c5.tar.gz cpython-6624e6854694315c25981f6bb6cfd360798169c5.tar.bz2 |
SF bug #604716: faster [None]*n or []*n
Fulfilled request to special case repetitions of lists of length 0 or 1.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/listobject.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 48f3d7d..7d03506 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++) { |