mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
Apply map/zip pre-sizing optimization to a broader class of objects.
Formerly, the length was only fetched from sequence objects. Now, any object that reports its length can benefit from pre-sizing.
This commit is contained in:
parent
7832cd6141
commit
77f3c87113
1 changed files with 7 additions and 11 deletions
|
@ -791,17 +791,13 @@ builtin_map(PyObject *self, PyObject *args)
|
|||
}
|
||||
|
||||
/* Update len. */
|
||||
curlen = -1; /* unknown */
|
||||
if (PySequence_Check(curseq) &&
|
||||
curseq->ob_type->tp_as_sequence->sq_length) {
|
||||
curlen = PySequence_Size(curseq);
|
||||
if (curlen < 0)
|
||||
PyErr_Clear();
|
||||
}
|
||||
if (curlen < 0)
|
||||
curlen = PyObject_Size(curseq);
|
||||
if (curlen < 0) {
|
||||
PyErr_Clear();
|
||||
curlen = 8; /* arbitrary */
|
||||
if (curlen > len)
|
||||
len = curlen;
|
||||
}
|
||||
if (curlen > len)
|
||||
len = curlen;
|
||||
}
|
||||
|
||||
/* Get space for the result list. */
|
||||
|
@ -1968,7 +1964,7 @@ builtin_zip(PyObject *self, PyObject *args)
|
|||
len = -1; /* unknown */
|
||||
for (i = 0; i < itemsize; ++i) {
|
||||
PyObject *item = PyTuple_GET_ITEM(args, i);
|
||||
int thislen = PySequence_Length(item);
|
||||
int thislen = PyObject_Size(item);
|
||||
if (thislen < 0) {
|
||||
PyErr_Clear();
|
||||
len = -1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue