summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-01-04 11:00:08 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-01-04 11:00:08 (GMT)
commitb86269db458f63b95be8017e47671092be3b48d1 (patch)
tree7d923c10b04385faf611a9df19be81f504a3e5d9
parent4618cc09ec6b155c1570be07c7da37327f6e2293 (diff)
downloadcpython-b86269db458f63b95be8017e47671092be3b48d1.zip
cpython-b86269db458f63b95be8017e47671092be3b48d1.tar.gz
cpython-b86269db458f63b95be8017e47671092be3b48d1.tar.bz2
Apply 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--Objects/abstract.c12
-rw-r--r--Python/bltinmodule.c12
2 files changed, 8 insertions, 16 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 1259ad4..342d971 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1448,15 +1448,11 @@ PySequence_List(PyObject *v)
return NULL;
/* Guess a result list size. */
- n = -1; /* unknown */
- if (PySequence_Check(v) &&
- v->ob_type->tp_as_sequence->sq_length) {
- n = PySequence_Size(v);
- if (n < 0)
- PyErr_Clear();
- }
- if (n < 0)
+ n = PyObject_Size(v);
+ if (n < 0) {
+ PyErr_Clear();
n = 8; /* arbitrary */
+ }
result = PyList_New(n);
if (result == NULL) {
Py_DECREF(it);
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 7fc6f57..a17c6d9 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -153,15 +153,11 @@ builtin_filter(PyObject *self, PyObject *args)
return NULL;
/* Guess a result list size. */
- len = -1; /* unknown */
- if (PySequence_Check(seq) &&
- seq->ob_type->tp_as_sequence->sq_length) {
- len = PySequence_Size(seq);
- if (len < 0)
- PyErr_Clear();
+ len = PyObject_Size(seq);
+ if (len < 0) {
+ PyErr_Clear();
+ len = 8; /* arbitrary */
}
- if (len < 0)
- len = 8; /* arbitrary */
/* Pre-allocate argument list tuple. */
arg = PyTuple_New(1);