Skip to content

py/map: allow disabling reallocation for specific dicts #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions py/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void mp_map_init(mp_map_t *map, size_t n) {
map->all_keys_are_qstrs = 1;
map->is_fixed = 0;
map->is_ordered = 0;
map->no_realloc = 0;
}

void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
Expand All @@ -106,6 +107,7 @@ void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table) {
map->all_keys_are_qstrs = 1;
map->is_fixed = 1;
map->is_ordered = 1;
map->no_realloc = 1;
map->table = (mp_map_elem_t *)table;
}

Expand All @@ -129,6 +131,9 @@ void mp_map_clear(mp_map_t *map) {
}

STATIC void mp_map_rehash(mp_map_t *map) {
if (map->no_realloc) {
mp_raise_msg(&mp_type_MemoryError, "Cannot reallocate map");
}
size_t old_alloc = map->alloc;
size_t new_alloc = get_hash_alloc_greater_or_equal_to(map->alloc + 1);
DEBUG_printf("mp_map_rehash(%p): " UINT_FMT " -> " UINT_FMT "\n", map, old_alloc, new_alloc);
Expand Down
3 changes: 2 additions & 1 deletion py/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ typedef struct _mp_map_t {
size_t all_keys_are_qstrs : 1;
size_t is_fixed : 1; // if set, table is fixed/read-only and can't be modified
size_t is_ordered : 1; // if set, table is an ordered array, not a hash map
size_t used : (8 * sizeof(size_t) - 3);
size_t no_realloc : 1; // if set, table may not be reallocated (only relevant if `is_fixed` == 0)
size_t used : (8 * sizeof(size_t) - 4);
size_t alloc;
mp_map_elem_t *table;
} mp_map_t;
Expand Down
1 change: 1 addition & 0 deletions py/objdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ mp_obj_t mp_obj_dict_copy(mp_obj_t self_in) {
other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
other->map.is_fixed = 0;
other->map.is_ordered = self->map.is_ordered;
other->map.no_realloc = self->map.no_realloc;
memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
return other_out;
}
Expand Down
Loading