Limit qstr pool size to reduce memory waste.

This commit is contained in:
Scott Shawcroft 2018-06-28 10:43:39 -07:00
parent 9de611c056
commit cced51cbd2
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
2 changed files with 13 additions and 3 deletions

6
py/mpconfig.h Normal file → Executable file
View File

@ -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)

10
py/qstr.c Normal file → Executable file
View File

@ -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);