changeset: 76206:a012d5df2c73 user: Stefan Krah date: Tue Apr 10 16:27:58 2012 +0200 files: Modules/_decimal/_decimal.c description: Issue #14478: Cache the hash of a Decimal in the C version. diff -r b6820ef06745 -r a012d5df2c73 Modules/_decimal/_decimal.c --- a/Modules/_decimal/_decimal.c Tue Apr 10 19:51:58 2012 +0800 +++ b/Modules/_decimal/_decimal.c Tue Apr 10 16:27:58 2012 +0200 @@ -60,6 +60,7 @@ typedef struct { PyObject_HEAD + Py_hash_t hash; mpd_t dec; mpd_uint_t data[_Py_DEC_MINALLOC]; } PyDecObject; @@ -1805,6 +1806,8 @@ return NULL; } + dec->hash = -1; + MPD(dec)->flags = MPD_STATIC|MPD_STATIC_DATA; MPD(dec)->exp = 0; MPD(dec)->digits = 0; @@ -4210,7 +4213,7 @@ /* Always uses the module context */ static Py_hash_t -dec_hash(PyObject *v) +_dec_hash(PyDecObject *v) { #if defined(CONFIG_64) && _PyHASH_BITS == 61 /* 2**61 - 1 */ @@ -4323,6 +4326,16 @@ goto finish; } +static Py_hash_t +dec_hash(PyDecObject *self) +{ + if (self->hash == -1) { + self->hash = _dec_hash(self); + } + + return self->hash; +} + /* __reduce__ */ static PyObject * dec_reduce(PyObject *self, PyObject *dummy UNUSED)