Skip to content

Commit af622eb

Browse files
committed
py/map: Change mp_uint_t to size_t where appropriate.
The internal map/set functions now use size_t exclusively for computing addresses. size_t is enough to reach all of available memory when computing addresses so is the right type to use. In particular, for nanbox builds it saves quite a bit of code size and RAM compared to the original use of mp_uint_t (which is 64-bits on nanbox builds).
1 parent a25aa2b commit af622eb

2 files changed

Lines changed: 29 additions & 29 deletions

File tree

py/map.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ STATIC const uint16_t hash_allocation_sizes[] = {
5656
3229, 4831, 7243, 10861, 16273, 24407, 36607, 54907, // *1.5
5757
};
5858

59-
STATIC mp_uint_t get_hash_alloc_greater_or_equal_to(mp_uint_t x) {
59+
STATIC size_t get_hash_alloc_greater_or_equal_to(size_t x) {
6060
for (size_t i = 0; i < MP_ARRAY_SIZE(hash_allocation_sizes); i++) {
6161
if (hash_allocation_sizes[i] >= x) {
6262
return hash_allocation_sizes[i];
@@ -70,7 +70,7 @@ STATIC mp_uint_t get_hash_alloc_greater_or_equal_to(mp_uint_t x) {
7070
/******************************************************************************/
7171
/* map */
7272

73-
void mp_map_init(mp_map_t *map, mp_uint_t n) {
73+
void mp_map_init(mp_map_t *map, size_t n) {
7474
if (n == 0) {
7575
map->alloc = 0;
7676
map->table = NULL;
@@ -84,7 +84,7 @@ void mp_map_init(mp_map_t *map, mp_uint_t n) {
8484
map->is_ordered = 0;
8585
}
8686

87-
void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table) {
87+
void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
8888
map->alloc = n;
8989
map->used = n;
9090
map->all_keys_are_qstrs = 1;
@@ -93,7 +93,7 @@ void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table)
9393
map->table = (mp_map_elem_t*)table;
9494
}
9595

96-
mp_map_t *mp_map_new(mp_uint_t n) {
96+
mp_map_t *mp_map_new(size_t n) {
9797
mp_map_t *map = m_new(mp_map_t, 1);
9898
mp_map_init(map, n);
9999
return map;
@@ -124,16 +124,16 @@ void mp_map_clear(mp_map_t *map) {
124124
}
125125

126126
STATIC void mp_map_rehash(mp_map_t *map) {
127-
mp_uint_t old_alloc = map->alloc;
128-
mp_uint_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
127+
size_t old_alloc = map->alloc;
128+
size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
129129
mp_map_elem_t *old_table = map->table;
130130
mp_map_elem_t *new_table = m_new0(mp_map_elem_t, new_alloc);
131131
// If we reach this point, table resizing succeeded, now we can edit the old map.
132132
map->alloc = new_alloc;
133133
map->used = 0;
134134
map->all_keys_are_qstrs = 1;
135135
map->table = new_table;
136-
for (mp_uint_t i = 0; i < old_alloc; i++) {
136+
for (size_t i = 0; i < old_alloc; i++) {
137137
if (old_table[i].key != MP_OBJ_NULL && old_table[i].key != MP_OBJ_SENTINEL) {
138138
mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
139139
}
@@ -220,8 +220,8 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
220220
hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
221221
}
222222

223-
mp_uint_t pos = hash % map->alloc;
224-
mp_uint_t start_pos = pos;
223+
size_t pos = hash % map->alloc;
224+
size_t start_pos = pos;
225225
mp_map_elem_t *avail_slot = NULL;
226226
for (;;) {
227227
mp_map_elem_t *slot = &map->table[pos];
@@ -296,19 +296,19 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
296296

297297
#if MICROPY_PY_BUILTINS_SET
298298

299-
void mp_set_init(mp_set_t *set, mp_uint_t n) {
299+
void mp_set_init(mp_set_t *set, size_t n) {
300300
set->alloc = n;
301301
set->used = 0;
302302
set->table = m_new0(mp_obj_t, set->alloc);
303303
}
304304

305305
STATIC void mp_set_rehash(mp_set_t *set) {
306-
mp_uint_t old_alloc = set->alloc;
306+
size_t old_alloc = set->alloc;
307307
mp_obj_t *old_table = set->table;
308308
set->alloc = get_hash_alloc_greater_or_equal_to(set->alloc + 1);
309309
set->used = 0;
310310
set->table = m_new0(mp_obj_t, set->alloc);
311-
for (mp_uint_t i = 0; i < old_alloc; i++) {
311+
for (size_t i = 0; i < old_alloc; i++) {
312312
if (old_table[i] != MP_OBJ_NULL && old_table[i] != MP_OBJ_SENTINEL) {
313313
mp_set_lookup(set, old_table[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
314314
}
@@ -328,8 +328,8 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku
328328
}
329329
}
330330
mp_uint_t hash = MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, index));
331-
mp_uint_t pos = hash % set->alloc;
332-
mp_uint_t start_pos = pos;
331+
size_t pos = hash % set->alloc;
332+
size_t start_pos = pos;
333333
mp_obj_t *avail_slot = NULL;
334334
for (;;) {
335335
mp_obj_t elem = set->table[pos];
@@ -390,7 +390,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku
390390
}
391391

392392
mp_obj_t mp_set_remove_first(mp_set_t *set) {
393-
for (mp_uint_t pos = 0; pos < set->alloc; pos++) {
393+
for (size_t pos = 0; pos < set->alloc; pos++) {
394394
if (MP_SET_SLOT_IS_FILLED(set, pos)) {
395395
mp_obj_t elem = set->table[pos];
396396
// delete element
@@ -418,7 +418,7 @@ void mp_set_clear(mp_set_t *set) {
418418

419419
#if defined(DEBUG_PRINT) && DEBUG_PRINT
420420
void mp_map_dump(mp_map_t *map) {
421-
for (mp_uint_t i = 0; i < map->alloc; i++) {
421+
for (size_t i = 0; i < map->alloc; i++) {
422422
if (map->table[i].key != NULL) {
423423
mp_obj_print(map->table[i].key, PRINT_REPR);
424424
} else {

py/obj.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ typedef struct _mp_rom_map_elem_t {
353353
// would also need a trucated dict structure
354354

355355
typedef struct _mp_map_t {
356-
mp_uint_t all_keys_are_qstrs : 1;
357-
mp_uint_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered
358-
mp_uint_t is_ordered : 1; // an ordered array
359-
mp_uint_t used : (8 * sizeof(mp_uint_t) - 3);
360-
mp_uint_t alloc;
356+
size_t all_keys_are_qstrs : 1;
357+
size_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered
358+
size_t is_ordered : 1; // an ordered array
359+
size_t used : (8 * sizeof(size_t) - 3);
360+
size_t alloc;
361361
mp_map_elem_t *table;
362362
} mp_map_t;
363363

@@ -371,11 +371,11 @@ typedef enum _mp_map_lookup_kind_t {
371371

372372
extern const mp_map_t mp_const_empty_map;
373373

374-
static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, mp_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
374+
static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, size_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
375375

376-
void mp_map_init(mp_map_t *map, mp_uint_t n);
377-
void mp_map_init_fixed_table(mp_map_t *map, mp_uint_t n, const mp_obj_t *table);
378-
mp_map_t *mp_map_new(mp_uint_t n);
376+
void mp_map_init(mp_map_t *map, size_t n);
377+
void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
378+
mp_map_t *mp_map_new(size_t n);
379379
void mp_map_deinit(mp_map_t *map);
380380
void mp_map_free(mp_map_t *map);
381381
mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
@@ -385,14 +385,14 @@ void mp_map_dump(mp_map_t *map);
385385
// Underlying set implementation (not set object)
386386

387387
typedef struct _mp_set_t {
388-
mp_uint_t alloc;
389-
mp_uint_t used;
388+
size_t alloc;
389+
size_t used;
390390
mp_obj_t *table;
391391
} mp_set_t;
392392

393-
static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, mp_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
393+
static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, size_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
394394

395-
void mp_set_init(mp_set_t *set, mp_uint_t n);
395+
void mp_set_init(mp_set_t *set, size_t n);
396396
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
397397
mp_obj_t mp_set_remove_first(mp_set_t *set);
398398
void mp_set_clear(mp_set_t *set);

0 commit comments

Comments
 (0)