1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-22 20:42:03 +00:00

feat(core): presize some modules to account for their sub-imports

This commit is contained in:
matejcik 2021-04-12 13:52:37 +02:00 committed by matejcik
parent 5f4240d93c
commit 94521a2065
2 changed files with 21 additions and 1 deletions

View File

@ -6,7 +6,13 @@ import storage
import storage.device
if __debug__:
import storage.debug
from trezor import config, pin, utils # noqa: F401
import trezor
import trezor.pin # noqa: F401
from trezor import utils
utils.presize_module("trezor", 30)
utils.presize_module("storage", 12)
if not utils.BITCOIN_ONLY:
import storage.fido2 # noqa: F401

View File

@ -82,6 +82,20 @@ class unimport:
gc.collect()
def presize_module(modname: str, size: int) -> None:
"""Ensure the module's dict is preallocated to an expected size.
This is used in modules like `trezor`, whose dict size depends not only on the
symbols defined in the file itself, but also on the number of submodules that will
be inserted into the module's namespace.
"""
module = sys.modules[modname]
for i in range(size):
setattr(module, "___PRESIZE_MODULE_%d" % i, None)
for i in range(size):
delattr(module, "___PRESIZE_MODULE_%d" % i)
def ensure(cond: bool, msg: str | None = None) -> None:
if not cond:
if msg is None: