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

Skip to content

Commit 63e6c32

Browse files
committed
Consolidate the occurrances of the prime used as the multiplier when hashing
to a single #define instead of having several copies in several files. This excludes the Modules/ tree (datetime and expat both have a copy for their own purposes with no need for it to be the same).
1 parent 515687a commit 63e6c32

4 files changed

Lines changed: 7 additions & 4 deletions

File tree

Include/pyport.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ Used in: PY_LONG_LONG
131131
#endif
132132
#endif
133133

134+
/* Prime multiplier used in string and various other hashes. */
135+
#define _PyHASH_MULTIPLIER 1000003 /* 0xf4243 */
136+
134137
/* Parameters used for the numeric hash implementation. See notes for
135138
_PyHash_Double in Objects/object.c. Numeric hashes are based on
136139
reduction modulo the prime 2**_PyHASH_BITS - 1. */
@@ -143,7 +146,7 @@ Used in: PY_LONG_LONG
143146
#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
144147
#define _PyHASH_INF 314159
145148
#define _PyHASH_NAN 0
146-
#define _PyHASH_IMAG 1000003UL
149+
#define _PyHASH_IMAG _PyHASH_MULTIPLIER
147150

148151
/* uintptr_t is the C9X name for an unsigned integral type such that a
149152
* legitimate void* can be cast to uintptr_t and then back to void* again

Objects/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ bytes_hash(PyBytesObject *a)
881881
p = (unsigned char *) a->ob_sval;
882882
x = *p << 7;
883883
while (--len >= 0)
884-
x = (1000003*x) ^ *p++;
884+
x = (_PyHASH_MULTIPLIER*x) ^ *p++;
885885
x ^= Py_SIZE(a);
886886
if (x == -1)
887887
x = -2;

Objects/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ tuplehash(PyTupleObject *v)
315315
register Py_hash_t x, y;
316316
register Py_ssize_t len = Py_SIZE(v);
317317
register PyObject **p;
318-
Py_hash_t mult = 1000003L;
318+
Py_hash_t mult = _PyHASH_MULTIPLIER;
319319
x = 0x345678L;
320320
p = v->ob_item;
321321
while (--len >= 0) {

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7666,7 +7666,7 @@ unicode_hash(PyUnicodeObject *self)
76667666
p = self->str;
76677667
x = *p << 7;
76687668
while (--len >= 0)
7669-
x = (1000003*x) ^ *p++;
7669+
x = (_PyHASH_MULTIPLIER*x) ^ *p++;
76707670
x ^= Py_SIZE(self);
76717671
if (x == -1)
76727672
x = -2;

0 commit comments

Comments
 (0)