[Python-checkins] cpython (merge 3.2 -> default): Consolidate the occurrances of the prime used as the multiplier when hashing.

gregory.p.smith python-checkins at python.org
Sun Jan 15 00:45:32 CET 2012


http://hg.python.org/cpython/rev/40e1be1e0707
changeset:   74416:40e1be1e0707
parent:      74414:83e56004c12b
parent:      74415:050c07b31192
user:        Gregory P. Smith <greg at krypto.org>
date:        Sat Jan 14 15:45:13 2012 -0800
summary:
  Consolidate the occurrances of the prime used as the multiplier when hashing.

files:
  Include/pyport.h        |  5 ++++-
  Objects/object.c        |  2 +-
  Objects/tupleobject.c   |  2 +-
  Objects/unicodeobject.c |  2 +-
  4 files changed, 7 insertions(+), 4 deletions(-)


diff --git a/Include/pyport.h b/Include/pyport.h
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -131,6 +131,9 @@
 #endif
 #endif
 
+/* Prime multiplier used in string and various other hashes. */
+#define _PyHASH_MULTIPLIER 1000003  /* 0xf4243 */
+
 /* Parameters used for the numeric hash implementation.  See notes for
    _Py_HashDouble in Objects/object.c.  Numeric hashes are based on
    reduction modulo the prime 2**_PyHASH_BITS - 1. */
@@ -143,7 +146,7 @@
 #define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
 #define _PyHASH_INF 314159
 #define _PyHASH_NAN 0
-#define _PyHASH_IMAG 1000003UL
+#define _PyHASH_IMAG _PyHASH_MULTIPLIER
 
 /* uintptr_t is the C9X name for an unsigned integral type such that a
  * legitimate void* can be cast to uintptr_t and then back to void* again
diff --git a/Objects/object.c b/Objects/object.c
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -761,7 +761,7 @@
 
     x = (Py_uhash_t) *p << 7;
     for (i = 0; i < len; i++)
-        x = (1000003U * x) ^ (Py_uhash_t) *p++;
+        x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
     x ^= (Py_uhash_t) len;
     if (x == -1)
         x = -2;
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -316,7 +316,7 @@
     register Py_hash_t y;
     register Py_ssize_t len = Py_SIZE(v);
     register PyObject **p;
-    Py_uhash_t mult = 1000003;
+    Py_uhash_t mult = _PyHASH_MULTIPLIER;
     x = 0x345678;
     p = v->ob_item;
     while (--len >= 0) {
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11210,7 +11210,7 @@
 #define HASH(P) \
     x = (Py_uhash_t)*P << 7; \
     while (--len >= 0) \
-        x = (1000003*x) ^ (Py_uhash_t)*P++;
+        x = (_PyHASH_MULTIPLIER*x) ^ (Py_uhash_t)*P++;
 
     switch (PyUnicode_KIND(self)) {
     case PyUnicode_1BYTE_KIND: {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list