Skip to content

Commit cced51c

Browse files
committed
Limit qstr pool size to reduce memory waste.
1 parent 9de611c commit cced51c

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

py/mpconfig.h

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@
130130
#define MICROPY_ALLOC_QSTR_CHUNK_INIT (128)
131131
#endif
132132

133+
// Max number of entries in newly allocated QSTR pools. Smaller numbers may make QSTR lookups
134+
// slightly slower but reduce the waste of unused spots.
135+
#ifndef MICROPY_QSTR_POOL_MAX_ENTRIES
136+
#define MICROPY_QSTR_POOL_MAX_ENTRIES (64)
137+
#endif
138+
133139
// Initial amount for lexer indentation level
134140
#ifndef MICROPY_ALLOC_LEXER_INDENT_INIT
135141
#define MICROPY_ALLOC_LEXER_INDENT_INIT (10)

py/qstr.c

100644100755
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,18 @@ STATIC qstr qstr_add(const byte *q_ptr) {
144144

145145
// make sure we have room in the pool for a new qstr
146146
if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
147-
qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
147+
uint32_t new_pool_length = MP_STATE_VM(last_pool)->alloc * 2;
148+
if (new_pool_length > MICROPY_QSTR_POOL_MAX_ENTRIES) {
149+
new_pool_length = MICROPY_QSTR_POOL_MAX_ENTRIES;
150+
}
151+
qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, new_pool_length);
148152
if (pool == NULL) {
149153
QSTR_EXIT();
150-
m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2);
154+
m_malloc_fail(new_pool_length);
151155
}
152156
pool->prev = MP_STATE_VM(last_pool);
153157
pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
154-
pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
158+
pool->alloc = new_pool_length;
155159
pool->len = 0;
156160
MP_STATE_VM(last_pool) = pool;
157161
DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);

0 commit comments

Comments
 (0)