From ecfabf9d7aa0c20c48fed29fb39ad029748ff77c Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 25 Mar 2025 11:32:56 +0200 Subject: [PATCH] py/runtime: make `__main__` preallocate to a configurable size --- py/mpconfig.h | 5 +++++ py/runtime.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/py/mpconfig.h b/py/mpconfig.h index d6e2ce571a756..a9d8887ab4d2b 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -259,6 +259,11 @@ #define MICROPY_LOADED_MODULES_DICT_SIZE (3) #endif +// Initial size of __main__ dict +#ifndef MICROPY_MAIN_DICT_SIZE +#define MICROPY_MAIN_DICT_SIZE (1) +#endif + // Whether realloc/free should be passed allocated memory region size // You must enable this if MICROPY_MEM_STATS is enabled #ifndef MICROPY_MALLOC_USES_ALLOCATED_SIZE diff --git a/py/runtime.c b/py/runtime.c index e6d8c68070e5c..696b99825fab9 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -105,7 +105,7 @@ void mp_init(void) { mp_obj_dict_init(&MP_STATE_VM(mp_loaded_modules_dict), MICROPY_LOADED_MODULES_DICT_SIZE); // initialise the __main__ module - mp_obj_dict_init(&MP_STATE_VM(dict_main), 1); + mp_obj_dict_init(&MP_STATE_VM(dict_main), MICROPY_MAIN_DICT_SIZE); mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(dict_main)), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__)); // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())