[Python-checkins] r58925 - python/trunk/Objects/dictobject.c

raymond.hettinger python-checkins at python.org
Sat Nov 10 00:14:45 CET 2007


Author: raymond.hettinger
Date: Sat Nov 10 00:14:44 2007
New Revision: 58925

Modified:
   python/trunk/Objects/dictobject.c
Log:
Optimize common case for dict.fromkeys().

Modified: python/trunk/Objects/dictobject.c
==============================================================================
--- python/trunk/Objects/dictobject.c	(original)
+++ python/trunk/Objects/dictobject.c	Sat Nov 10 00:14:44 2007
@@ -1191,7 +1191,7 @@
 		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 @@
 		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;
 


More information about the Python-checkins mailing list