diff options
author | Raymond Hettinger <python@rcn.com> | 2004-01-04 06:08:16 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-01-04 06:08:16 (GMT) |
commit | 7832cd6141116cc64f6304f1107631e28cd7ee08 (patch) | |
tree | b67aff680ade3afb5752fcd931e043ad62b91ea2 /Objects | |
parent | 674d56b82e59dfe481f355999b49a7b99b8d763d (diff) | |
download | cpython-7832cd6141116cc64f6304f1107631e28cd7ee08.zip cpython-7832cd6141116cc64f6304f1107631e28cd7ee08.tar.gz cpython-7832cd6141116cc64f6304f1107631e28cd7ee08.tar.bz2 |
Apply tuple/list pre-sizing optimization to a broader class of objects.
Formerly, length data fetched from sequence objects.
Now, any object that reports its length can benefit from pre-sizing.
On one sample timing, it gave a threefold speedup for list(s) where s
was a set object.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 2 | ||||
-rw-r--r-- | Objects/listobject.c | 12 |
2 files changed, 5 insertions, 9 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 7e246db..1259ad4 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1380,7 +1380,7 @@ PySequence_Tuple(PyObject *v) return NULL; /* Guess result size and allocate space. */ - n = PySequence_Size(v); + n = PyObject_Size(v); if (n < 0) { PyErr_Clear(); n = 10; /* arbitrary */ diff --git a/Objects/listobject.c b/Objects/listobject.c index 47673be..3397fbb 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2266,15 +2266,11 @@ list_fill(PyListObject *result, PyObject *v) return -1; /* 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 */ + } NRESIZE(result->ob_item, PyObject*, n); if (result->ob_item == NULL) { PyErr_NoMemory(); |