diff options
author | Raymond Hettinger <python@rcn.com> | 2004-01-04 08:54:44 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-01-04 08:54:44 (GMT) |
commit | 77f3c8711337c887c072c43be2aec7a3079819f3 (patch) | |
tree | 7326bdf06fc7e8c53c668e3e23b4bf764cba3194 | |
parent | 7832cd6141116cc64f6304f1107631e28cd7ee08 (diff) | |
download | cpython-77f3c8711337c887c072c43be2aec7a3079819f3.zip cpython-77f3c8711337c887c072c43be2aec7a3079819f3.tar.gz cpython-77f3c8711337c887c072c43be2aec7a3079819f3.tar.bz2 |
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.
-rw-r--r-- | Python/bltinmodule.c | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 314fb4e..7fc6f57 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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; |