Skip to content

Commit 645582f

Browse files
committed
py: Make id() return small int for the most common address space mapping.
Many OSes/CPUs have affinity to put "user" data into lower half of address space. Take advantage of that and remap such addresses into full small int range (including negative part). If address is from upper half, long int will be used. Previously, small int was returned for lower quarter of address space, and upper quarter. For 2 middle quarters, long int was used, which is clearly worse schedule than the above.
1 parent adf0f2a commit 645582f

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

py/builtin.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,21 @@ STATIC mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *k
503503
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
504504

505505
STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
506-
return mp_obj_new_int((mp_int_t)o_in);
506+
mp_int_t id = (mp_int_t)o_in;
507+
if (!MP_OBJ_IS_OBJ(o_in)) {
508+
return mp_obj_new_int(id);
509+
} else if (id >= 0) {
510+
// Many OSes and CPUs have affinity for putting "user" memories
511+
// into low half of address space, and "system" into upper half.
512+
// We're going to take advantage of that and return small int
513+
// (signed) for such "user" addresses.
514+
return MP_OBJ_NEW_SMALL_INT(id);
515+
} else {
516+
// If that didn't work, well, let's return long int, just as
517+
// a (big) positve value, so it will never clash with the range
518+
// of small int returned in previous case.
519+
return mp_obj_new_int_from_uint((mp_uint_t)id);
520+
}
507521
}
508522

509523
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);

0 commit comments

Comments
 (0)