mirror of https://github.com/python/cpython
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:
parent
4618cc09ec
commit
b86269db45
|
@ -1448,15 +1448,11 @@ PySequence_List(PyObject *v)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Guess a result list size. */
|
/* Guess a result list size. */
|
||||||
n = -1; /* unknown */
|
n = PyObject_Size(v);
|
||||||
if (PySequence_Check(v) &&
|
if (n < 0) {
|
||||||
v->ob_type->tp_as_sequence->sq_length) {
|
PyErr_Clear();
|
||||||
n = PySequence_Size(v);
|
|
||||||
if (n < 0)
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
|
||||||
if (n < 0)
|
|
||||||
n = 8; /* arbitrary */
|
n = 8; /* arbitrary */
|
||||||
|
}
|
||||||
result = PyList_New(n);
|
result = PyList_New(n);
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
Py_DECREF(it);
|
Py_DECREF(it);
|
||||||
|
|
|
@ -153,15 +153,11 @@ builtin_filter(PyObject *self, PyObject *args)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* Guess a result list size. */
|
/* Guess a result list size. */
|
||||||
len = -1; /* unknown */
|
len = PyObject_Size(seq);
|
||||||
if (PySequence_Check(seq) &&
|
if (len < 0) {
|
||||||
seq->ob_type->tp_as_sequence->sq_length) {
|
PyErr_Clear();
|
||||||
len = PySequence_Size(seq);
|
len = 8; /* arbitrary */
|
||||||
if (len < 0)
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
}
|
||||||
if (len < 0)
|
|
||||||
len = 8; /* arbitrary */
|
|
||||||
|
|
||||||
/* Pre-allocate argument list tuple. */
|
/* Pre-allocate argument list tuple. */
|
||||||
arg = PyTuple_New(1);
|
arg = PyTuple_New(1);
|
||||||
|
|
Loading…
Reference in New Issue