80
80
#define QSTR_EXIT ()
81
81
#endif
82
82
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
+
83
87
// this must match the equivalent function in makeqstrdata.py
84
88
mp_uint_t qstr_compute_hash (const byte * data , size_t len ) {
85
89
// 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) {
98
102
const qstr_pool_t mp_qstr_const_pool = {
99
103
NULL , // no previous pool
100
104
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 ,
102
106
MP_QSTRnumber_of , // corresponds to number of strings in array just below
103
107
{
104
108
#ifndef NO_QSTR
@@ -141,14 +145,19 @@ STATIC qstr qstr_add(const byte *q_ptr) {
141
145
142
146
// make sure we have room in the pool for a new qstr
143
147
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 );
145
154
if (pool == NULL ) {
146
155
QSTR_EXIT ();
147
- m_malloc_fail (MP_STATE_VM ( last_pool ) -> alloc * 2 );
156
+ m_malloc_fail (new_alloc );
148
157
}
149
158
pool -> prev = MP_STATE_VM (last_pool );
150
159
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 ;
152
161
pool -> len = 0 ;
153
162
MP_STATE_VM (last_pool ) = pool ;
154
163
DEBUG_printf ("QSTR: allocate new pool of size %d\n" , MP_STATE_VM (last_pool )-> alloc );
0 commit comments