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

pull/1610/head
matejcik 3 years ago committed by matejcik
parent 5f4240d93c
commit 94521a2065

@ -6,7 +6,13 @@ import storage
import storage.device import storage.device
if __debug__: if __debug__:
import storage.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: if not utils.BITCOIN_ONLY:
import storage.fido2 # noqa: F401 import storage.fido2 # noqa: F401

@ -82,6 +82,20 @@ class unimport:
gc.collect() 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: def ensure(cond: bool, msg: str | None = None) -> None:
if not cond: if not cond:
if msg is None: if msg is None:

Loading…
Cancel
Save