Skip to content

Commit b9cde10

Browse files
committed
bpo-43475: Fix worst case collision behavior for NaN instances
1 parent d35eef3 commit b9cde10

File tree

7 files changed

+17
-11
lines changed

7 files changed

+17
-11
lines changed

Doc/library/sys.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ always available.
855855
+---------------------+--------------------------------------------------+
856856
| :const:`inf` | hash value returned for a positive infinity |
857857
+---------------------+--------------------------------------------------+
858-
| :const:`nan` | hash value returned for a nan |
858+
| :const:`nan` | (this attribute is no longer used) |
859859
+---------------------+--------------------------------------------------+
860860
| :const:`imag` | multiplier used for the imaginary part of a |
861861
| | complex number |

Include/pyhash.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern "C" {
77

88
/* Helpers for hash functions */
99
#ifndef Py_LIMITED_API
10-
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(double);
10+
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(PyObject *, double);
1111
PyAPI_FUNC(Py_hash_t) _Py_HashPointer(const void*);
1212
// Similar to _Py_HashPointer(), but don't replace -1 with -2
1313
PyAPI_FUNC(Py_hash_t) _Py_HashPointerRaw(const void*);
@@ -29,7 +29,6 @@ PyAPI_FUNC(Py_hash_t) _Py_HashBytes(const void*, Py_ssize_t);
2929

3030
#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
3131
#define _PyHASH_INF 314159
32-
#define _PyHASH_NAN 0
3332
#define _PyHASH_IMAG _PyHASH_MULTIPLIER
3433

3534

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hashes of NaN values now depend on object identity. Formerly, they always
2+
hashed to 0 even though NaN values are not equal to one another. Having the
3+
same hash for unequal values caused pile-ups in hash tables.

Objects/complexobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,10 @@ static Py_hash_t
412412
complex_hash(PyComplexObject *v)
413413
{
414414
Py_uhash_t hashreal, hashimag, combined;
415-
hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real);
415+
hashreal = (Py_uhash_t)_Py_HashDouble(v, v->cval.real);
416416
if (hashreal == (Py_uhash_t)-1)
417417
return -1;
418-
hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag);
418+
hashimag = (Py_uhash_t)_Py_HashDouble(v, v->cval.imag);
419419
if (hashimag == (Py_uhash_t)-1)
420420
return -1;
421421
/* Note: if the imaginary part is 0, hashimag is 0 now,

Objects/floatobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ float_richcompare(PyObject *v, PyObject *w, int op)
556556
static Py_hash_t
557557
float_hash(PyFloatObject *v)
558558
{
559-
return _Py_HashDouble(v->ob_fval);
559+
return _Py_HashDouble(v, v->ob_fval);
560560
}
561561

562562
static PyObject *

Python/pyhash.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
5656
If the result of the reduction is infinity (this is impossible for
5757
integers, floats and Decimals) then use the predefined hash value
5858
_PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead.
59-
_PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the
60-
hashes of float and Decimal infinities and nans.
59+
_PyHASH_INF and -_PyHASH_INF are also used for the
60+
hashes of float and Decimal infinities.
61+
62+
NaNs hash to their object id. Having distinct hash values prevents
63+
catastrophic piles-up for unique NaNs which used to always have
64+
the same hash value but would compare unequal.
6165
6266
A selling point for the above strategy is that it makes it possible
6367
to compute hashes of decimal and binary floating-point numbers
@@ -83,7 +87,7 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
8387
*/
8488

8589
Py_hash_t
86-
_Py_HashDouble(double v)
90+
_Py_HashDouble(PyObject *inst, double v)
8791
{
8892
int e, sign;
8993
double m;
@@ -93,7 +97,7 @@ _Py_HashDouble(double v)
9397
if (Py_IS_INFINITY(v))
9498
return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
9599
else
96-
return _PyHASH_NAN;
100+
return (Py_hash_t) inst;
97101
}
98102

99103
m = frexp(v, &e);

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ get_hash_info(PyThreadState *tstate)
14051405
PyStructSequence_SET_ITEM(hash_info, field++,
14061406
PyLong_FromLong(_PyHASH_INF));
14071407
PyStructSequence_SET_ITEM(hash_info, field++,
1408-
PyLong_FromLong(_PyHASH_NAN));
1408+
PyLong_FromLong(0)); // This is now longer used
14091409
PyStructSequence_SET_ITEM(hash_info, field++,
14101410
PyLong_FromLong(_PyHASH_IMAG));
14111411
PyStructSequence_SET_ITEM(hash_info, field++,

0 commit comments

Comments
 (0)