Skip to content

Commit bbe8d51

Browse files
committed
py/map: Add fast-path for hashing of map index when it is a qstr.
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.
1 parent e5ce5e2 commit bbe8d51

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

py/map.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,14 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
201201
}
202202
}
203203

204-
mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
204+
// get hash of index, with fast path for common case of qstr
205+
mp_uint_t hash;
206+
if (MP_OBJ_IS_QSTR(index)) {
207+
hash = qstr_hash(MP_OBJ_QSTR_VALUE(index));
208+
} else {
209+
hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
210+
}
211+
205212
mp_uint_t pos = hash % map->alloc;
206213
mp_uint_t start_pos = pos;
207214
mp_map_elem_t *avail_slot = NULL;

0 commit comments

Comments
 (0)