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_longlong.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_longlong.c')
-rw-r--r-- | py/objint_longlong.c | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 64376e957..075fabe49 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -53,16 +53,6 @@ const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX}; #endif -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; - // truncate value to fit in mp_int_t, which gives the same hash as - // small int if the value fits without truncation - return self->val; -} - 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; @@ -117,6 +107,11 @@ 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(o->val != 0); + + // truncate value to fit in mp_int_t, which gives the same hash as + // small int if the value fits without truncation + case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_int_t)o->val); + case MP_UNARY_OP_POSITIVE: return o_in; case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val); case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val); |