diff options
author | Stefan Krah <skrah@bytereef.org> | 2012-04-10 14:27:58 (GMT) |
---|---|---|
committer | Stefan Krah <skrah@bytereef.org> | 2012-04-10 14:27:58 (GMT) |
commit | cc74b6ab14b66c2880beacefcd5df44a5a475232 (patch) | |
tree | 9bccdb38290c488aa8ef322f9f1184bd4a9e163d /Modules | |
parent | 9deb1887993b5476fdab8939a61057661e833656 (diff) | |
download | cpython-cc74b6ab14b66c2880beacefcd5df44a5a475232.zip cpython-cc74b6ab14b66c2880beacefcd5df44a5a475232.tar.gz cpython-cc74b6ab14b66c2880beacefcd5df44a5a475232.tar.bz2 |
Issue #14478: Cache the hash of a Decimal in the C version.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_decimal/_decimal.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index d5bb52f..168e8ec 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -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 @@ PyDecType_New(PyTypeObject *type) 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 @@ dec_floor(PyObject *self, PyObject *dummy UNUSED) /* 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 @@ malloc_error: 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) |