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.
This commit is contained in:
Raymond Hettinger 2004-01-04 11:00:08 +00:00
parent 4618cc09ec
commit b86269db45
2 changed files with 8 additions and 16 deletions

View file

@ -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);