From 5f4240d93c0e0f11b14513a336e30c94c08511e6 Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 9 Apr 2021 11:55:14 +0200 Subject: [PATCH] feat(core): preallocate sys.modules to an appropriate size --- core/embed/firmware/mpconfigport.h | 1 + core/embed/unix/mpconfigport.h | 1 + core/src/trezor/utils.py | 5 +++++ vendor/micropython | 2 +- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/embed/firmware/mpconfigport.h b/core/embed/firmware/mpconfigport.h index 2a98b0836..333940472 100644 --- a/core/embed/firmware/mpconfigport.h +++ b/core/embed/firmware/mpconfigport.h @@ -40,6 +40,7 @@ // memory allocation policies #define MICROPY_ALLOC_PATH_MAX (128) #define MICROPY_ENABLE_PYSTACK (1) +#define MICROPY_LOADED_MODULES_DICT_SIZE (160) // emitters #define MICROPY_PERSISTENT_CODE_LOAD (0) diff --git a/core/embed/unix/mpconfigport.h b/core/embed/unix/mpconfigport.h index 4d9dfe176..2c4e943d1 100644 --- a/core/embed/unix/mpconfigport.h +++ b/core/embed/unix/mpconfigport.h @@ -45,6 +45,7 @@ #define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1) #define MICROPY_MEM_STATS (1) #define MICROPY_ENABLE_PYSTACK (1) +#define MICROPY_LOADED_MODULES_DICT_SIZE (160) // emitters #define MICROPY_PERSISTENT_CODE_LOAD (0) diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index ce8f2865a..3e835699b 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -41,6 +41,11 @@ def unimport_begin() -> Set[str]: def unimport_end(mods: Set[str], collect: bool = True) -> None: + # static check that the size of sys.modules never grows above value of + # MICROPY_LOADED_MODULES_DICT_SIZE, so that the sys.modules dict is never + # reallocated at run-time + assert len(sys.modules) <= 160, "Please bump preallocated size in mpconfigport.h" + for mod in sys.modules: if mod not in mods: # remove reference from sys.modules diff --git a/vendor/micropython b/vendor/micropython index 3a3068da9..f7e780ae1 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit 3a3068da97ee45b812db61f09af8fefd17dc80f8 +Subproject commit f7e780ae16bc62519e6b78672e43ecae9138ed0a