diff options
author | Raymond Hettinger <python@rcn.com> | 2007-11-09 23:14:44 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-11-09 23:14:44 (GMT) |
commit | 34448790db8ce27eee5a72f7175c47cd4a8f8940 (patch) | |
tree | fca2d4b7a0bbb5df06c8212fde9de3fb59619fff /Objects | |
parent | d3d0baf0a1062686a04aaeedc4dbdb879b32c200 (diff) | |
download | cpython-34448790db8ce27eee5a72f7175c47cd4a8f8940.zip cpython-34448790db8ce27eee5a72f7175c47cd4a8f8940.tar.gz cpython-34448790db8ce27eee5a72f7175c47cd4a8f8940.tar.bz2 |
Optimize common case for dict.fromkeys().
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/dictobject.c | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 00f9bc8..978071b 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1191,7 +1191,7 @@ dict_fromkeys(PyObject *cls, PyObject *args) PyObject *key; long hash; - if (dictresize(mp, ((PyDictObject *)seq)->ma_used)) + if (dictresize(mp, PySet_GET_SIZE(seq))) return NULL; while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { @@ -1227,19 +1227,24 @@ dict_fromkeys(PyObject *cls, PyObject *args) return NULL; } - for (;;) { - key = PyIter_Next(it); - if (key == NULL) { - if (PyErr_Occurred()) + if (PyDict_CheckExact(d)) { + while ((key = PyIter_Next(it)) != NULL) { + status = PyDict_SetItem(d, key, value); + Py_DECREF(key); + if (status < 0) + goto Fail; + } + } else { + while ((key = PyIter_Next(it)) != NULL) { + status = PyObject_SetItem(d, key, value); + Py_DECREF(key); + if (status < 0) goto Fail; - break; } - status = PyObject_SetItem(d, key, value); - Py_DECREF(key); - if (status < 0) - goto Fail; } + if (PyErr_Occurred()) + goto Fail; Py_DECREF(it); return d; |