summaryrefslogtreecommitdiffstatshomepage
path: root/py/objtuple.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-05-11 12:25:19 +0000
committerDamien George <damien.p.george@gmail.com>2015-05-12 22:46:02 +0100
commitc2a4e4effc81d8ab21bb014e34355643e5ca0da2 (patch)
treed08f5921b2053c5d49ba7b10688eb8a771ddd7d0 /py/objtuple.c
parent6738c1dded8e436686f85008ec0a4fc47406ab7a (diff)
downloadmicropython-c2a4e4effc81d8ab21bb014e34355643e5ca0da2.tar.gz
micropython-c2a4e4effc81d8ab21bb014e34355643e5ca0da2.zip
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/objtuple.c')
-rw-r--r--py/objtuple.c19
1 files changed, 8 insertions, 11 deletions
diff --git a/py/objtuple.c b/py/objtuple.c
index 3ba37d0278..2fd1815863 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -126,6 +126,14 @@ mp_obj_t mp_obj_tuple_unary_op(mp_uint_t op, mp_obj_t self_in) {
mp_obj_tuple_t *self = self_in;
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
+ case MP_UNARY_OP_HASH: {
+ // start hash with pointer to empty tuple, to make it fairly unique
+ mp_int_t hash = (mp_int_t)mp_const_empty_tuple;
+ for (mp_uint_t i = 0; i < self->len; i++) {
+ hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, self->items[i]));
+ }
+ return MP_OBJ_NEW_SMALL_INT(hash);
+ }
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
default: return MP_OBJ_NULL; // op not supported
}
@@ -258,17 +266,6 @@ void mp_obj_tuple_del(mp_obj_t self_in) {
m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
}
-mp_int_t mp_obj_tuple_hash(mp_obj_t self_in) {
- assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
- mp_obj_tuple_t *self = self_in;
- // start hash with pointer to empty tuple, to make it fairly unique
- mp_int_t hash = (mp_int_t)mp_const_empty_tuple;
- for (mp_uint_t i = 0; i < self->len; i++) {
- hash += mp_obj_hash(self->items[i]);
- }
- return hash;
-}
-
/******************************************************************************/
/* tuple iterator */