diff options
author | Damien George <damien.p.george@gmail.com> | 2015-05-11 12:25:19 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-05-12 22:46:02 +0100 |
commit | c2a4e4effc81d8ab21bb014e34355643e5ca0da2 (patch) | |
tree | d08f5921b2053c5d49ba7b10688eb8a771ddd7d0 /py/objint_mpz.c | |
parent | 6738c1dded8e436686f85008ec0a4fc47406ab7a (diff) |
py: Convert hash API to use MP_UNARY_OP_HASH instead of ad-hoc function.
Hashing is now done using mp_unary_op function with MP_UNARY_OP_HASH as
the operator argument. Hashing for int, str and bytes still go via
fast-path in mp_unary_op since they are the most common objects which
need to be hashed.
This lead to quite a bit of code cleanup, and should be more efficient
if anything. It saves 176 bytes code space on Thumb2, and 360 bytes on
x86.
The only loss is that the error message "unhashable type" is now the
more generic "unsupported type for __hash__".
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r-- | py/objint_mpz.c | 9 |
1 files changed, 1 insertions, 8 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 369e5af32..936e2cb2b 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -96,14 +96,6 @@ char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_ return str; } -mp_int_t mp_obj_int_hash(mp_obj_t self_in) { - if (MP_OBJ_IS_SMALL_INT(self_in)) { - return MP_OBJ_SMALL_INT_VALUE(self_in); - } - mp_obj_int_t *self = self_in; - return mpz_hash(&self->mpz); -} - void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len, byte *buf) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int)); mp_obj_int_t *self = self_in; @@ -143,6 +135,7 @@ mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) { mp_obj_int_t *o = o_in; switch (op) { case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz)); + case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mpz_hash(&o->mpz)); case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; } case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; } |