From cced51cbd23aa732f0243fe555b4427ff46267b4 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jun 2018 10:43:39 -0700 Subject: [PATCH] Limit qstr pool size to reduce memory waste. --- py/mpconfig.h | 6 ++++++ py/qstr.c | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) mode change 100644 => 100755 py/mpconfig.h mode change 100644 => 100755 py/qstr.c diff --git a/py/mpconfig.h b/py/mpconfig.h old mode 100644 new mode 100755 index 765ae1e8f0..74d7c2c929 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -130,6 +130,12 @@ #define MICROPY_ALLOC_QSTR_CHUNK_INIT (128) #endif +// Max number of entries in newly allocated QSTR pools. Smaller numbers may make QSTR lookups +// slightly slower but reduce the waste of unused spots. +#ifndef MICROPY_QSTR_POOL_MAX_ENTRIES +#define MICROPY_QSTR_POOL_MAX_ENTRIES (64) +#endif + // Initial amount for lexer indentation level #ifndef MICROPY_ALLOC_LEXER_INDENT_INIT #define MICROPY_ALLOC_LEXER_INDENT_INIT (10) diff --git a/py/qstr.c b/py/qstr.c old mode 100644 new mode 100755 index 49b2d9daaa..de4dc6240a --- a/py/qstr.c +++ b/py/qstr.c @@ -144,14 +144,18 @@ STATIC qstr qstr_add(const byte *q_ptr) { // make sure we have room in the pool for a new qstr if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) { - qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2); + uint32_t new_pool_length = MP_STATE_VM(last_pool)->alloc * 2; + if (new_pool_length > MICROPY_QSTR_POOL_MAX_ENTRIES) { + new_pool_length = MICROPY_QSTR_POOL_MAX_ENTRIES; + } + qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, new_pool_length); if (pool == NULL) { QSTR_EXIT(); - m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2); + m_malloc_fail(new_pool_length); } pool->prev = MP_STATE_VM(last_pool); pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len; - pool->alloc = MP_STATE_VM(last_pool)->alloc * 2; + pool->alloc = new_pool_length; pool->len = 0; MP_STATE_VM(last_pool) = pool; DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);