[Python-checkins] r69582 - in python/trunk: Misc/NEWS Objects/object.c

antoine.pitrou python-checkins at python.org
Fri Feb 13 14:52:33 CET 2009


Author: antoine.pitrou
Date: Fri Feb 13 14:52:33 2009
New Revision: 69582

Log:
Issue #5186: Reduce hash collisions for objects with no __hash__ method by
rotating the object pointer by 4 bits to the right.



Modified:
   python/trunk/Misc/NEWS
   python/trunk/Objects/object.c

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Feb 13 14:52:33 2009
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #5186: Reduce hash collisions for objects with no __hash__ method by
+  rotating the object pointer by 4 bits to the right.
+
 - Issue #4575: Fix Py_IS_INFINITY macro to work correctly on x87 FPUs:
   it now forces its argument to double before testing for infinity.
 

Modified: python/trunk/Objects/object.c
==============================================================================
--- python/trunk/Objects/object.c	(original)
+++ python/trunk/Objects/object.c	Fri Feb 13 14:52:33 2009
@@ -1072,23 +1072,15 @@
 long
 _Py_HashPointer(void *p)
 {
-#if SIZEOF_LONG >= SIZEOF_VOID_P
-	return (long)p;
-#else
-	/* convert to a Python long and hash that */
-	PyObject* longobj;
 	long x;
-
-	if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
-		x = -1;
-		goto finally;
-	}
-	x = PyObject_Hash(longobj);
-
-finally:
-	Py_XDECREF(longobj);
+	size_t y = (size_t)p;
+	/* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
+	   excessive hash collisions for dicts and sets */
+	y = (y >> 4) | (y << 8*SIZEOF_VOID_P - 4);
+	x = (long)y;
+	if (x == -1)
+		x = -2;
 	return x;
-#endif
 }
 
 long


More information about the Python-checkins mailing list