Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79eee6b

Browse files
committed
Merged revisions 69582-69583 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r69582 | antoine.pitrou | 2009-02-13 14:52:33 +0100 (ven., 13 févr. 2009) | 4 lines Issue #5186: Reduce hash collisions for objects with no __hash__ method by rotating the object pointer by 4 bits to the right. ........ r69583 | antoine.pitrou | 2009-02-13 14:57:40 +0100 (ven., 13 févr. 2009) | 3 lines Fix compiler warning (gcc) ........
1 parent b5de0f2 commit 79eee6b

2 files changed

Lines changed: 10 additions & 15 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #5186: Reduce hash collisions for objects with no __hash__ method by
16+
rotating the object pointer by 4 bits to the right.
17+
1518
- Issue #4575: Fix Py_IS_INFINITY macro to work correctly on x87 FPUs:
1619
it now forces its argument to double before testing for infinity.
1720

Objects/object.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -699,23 +699,15 @@ _Py_HashDouble(double v)
699699
long
700700
_Py_HashPointer(void *p)
701701
{
702-
#if SIZEOF_LONG >= SIZEOF_VOID_P
703-
return (long)p;
704-
#else
705-
/* convert to a Python long and hash that */
706-
PyObject* longobj;
707702
long x;
708-
709-
if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
710-
x = -1;
711-
goto finally;
712-
}
713-
x = PyObject_Hash(longobj);
714-
715-
finally:
716-
Py_XDECREF(longobj);
703+
size_t y = (size_t)p;
704+
/* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
705+
excessive hash collisions for dicts and sets */
706+
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
707+
x = (long)y;
708+
if (x == -1)
709+
x = -2;
717710
return x;
718-
#endif
719711
}
720712

721713
long

0 commit comments

Comments
 (0)