8000 py/qstr: Put a lower bound on new qstr pool allocation. · msuszko/micropython@0d165fe · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 0d165fe

Browse files
committed
py/qstr: Put a lower bound on new qstr pool allocation.
1 parent 0c46419 commit 0d165fe

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

py/qstr.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
#define QSTR_EXIT()
8181
#endif
8282

83+
// Initial number of entries for qstr pool, set so that the first dynamically
84+
// allocated pool is twice this size. The value here must be <= MP_QSTRnumber_of.
85+
#define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10)
86+
8387
// this must match the equivalent function in makeqstrdata.py
8488
mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
8589
// djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
@@ -98,7 +102,7 @@ mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
98102
const qstr_pool_t mp_qstr_const_pool = {
99103
NULL, // no previous pool
100104
0, // no previous pool
101-
10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
105+
MICROPY_ALLOC_QSTR_ENTRIES_INIT,
102106
MP_QSTRnumber_of, // corresponds to number of strings in array just below
103107
{
104108
#ifndef NO_QSTR
@@ -141,14 +145,19 @@ STATIC qstr qstr_add(const byte *q_ptr) {
141145

142146
// make sure we have room in the pool for a new qstr
143147
if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
144-
qstr_pool_t *pool = m_new_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
148+
size_t new_alloc = MP_STATE_VM(last_pool)->alloc * 2;
149+
#ifdef MICROPY_QSTR_EXTRA_POOL
150+
// Put a lower bound on the allocation size in case the extra qstr pool has few entries
151+
new_alloc = MAX(MICROPY_ALLOC_QSTR_ENTRIES_INIT, new_alloc);
152+
#endif
153+
qstr_pool_t *pool = m_new_obj_var_maybe(qstr_pool_t, const char*, new_alloc);
145154
if (pool == NULL) {
146155
QSTR_EXIT();
147-
m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2);
156+
m_malloc_fail(new_alloc);
148157
}
149158
pool->prev = MP_STATE_VM(last_pool);
150159
pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
151-
pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
160+
pool->alloc = new_alloc;
152161
pool->len = 0;
153162
MP_STATE_VM(last_pool) = pool;
154163
DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);

0 commit comments

Comments
 (0)
0