Skip to content

Commit 5fedd0c

Browse files
committed
py: Fix dict.copy() and low-level map/set allocation.
Two things: 1) set flags in copy properly; make mp_map_init() not be too smart and do something with requested alloc size. Policy of using prime numbers for alloc size is high-level policy which should be applied at corresponding high levels. Low-level functions should just do what they're asked to, because they don't have enough context to be smarter than that. For example, munging with alloc size of course breaks dict copying (as changing sizes requires rehashing).
1 parent ea85a12 commit 5fedd0c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

py/map.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void mp_map_init(mp_map_t *map, int n) {
3030
map->alloc = 0;
3131
map->table = NULL;
3232
} else {
33-
map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
33+
map->alloc = n;
3434
map->table = m_new0(mp_map_elem_t, map->alloc);
3535
}
3636
map->used = 0;
@@ -197,7 +197,7 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
197197
/* set */
198198

199199
void mp_set_init(mp_set_t *set, int n) {
200-
set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
200+
set->alloc = n;
201201
set->used = 0;
202202
set->table = m_new0(mp_obj_t, set->alloc);
203203
}

py/objdict.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
164164
mp_obj_dict_t *self = self_in;
165165
mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
166166
other->map.used = self->map.used;
167+
other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
168+
other->map.table_is_fixed_array = 0;
167169
memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
168170
return other;
169171
}

0 commit comments

Comments
 (0)