Skip to content

Commit

Permalink
py/map: Add fast-path for hashing of map index when it is a qstr.
Browse files Browse the repository at this point in the history
Map indicies are most commonly a qstr, and adding a fast-path for hashing
of a qstr increases overall performance of the runtime.

On pyboard there is a 4% improvement in the pystone benchmark for a cost
of 20 bytes of code size.  It's about a 2% improvement on unix.
  • Loading branch information
dpgeorge committed Dec 26, 2015
1 parent e5ce5e2 commit bbe8d51
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion py/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
}
}

mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
// get hash of index, with fast path for common case of qstr
mp_uint_t hash;
if (MP_OBJ_IS_QSTR(index)) {
hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
} else {
hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
}

mp_uint_t pos = hash % map->alloc;
mp_uint_t start_pos = pos;
mp_map_elem_t *avail_slot = NULL;
Expand Down

0 comments on commit bbe8d51

Please sign in to comment.