1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-12 17:48:09 +00:00

feat(vendor): reimplement utils.presize_module() in C

[no changelog]
This commit is contained in:
Roman Zeyde 2025-04-21 10:49:46 +03:00 committed by Roman Zeyde
parent ffea765a85
commit 7661c13a22
7 changed files with 37 additions and 54 deletions

View File

@ -254,6 +254,26 @@ STATIC mp_obj_t mod_trezorutils_sd_hotswap_enabled(void) {
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_sd_hotswap_enabled_obj, STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_sd_hotswap_enabled_obj,
mod_trezorutils_sd_hotswap_enabled); mod_trezorutils_sd_hotswap_enabled);
/// def presize_module(mod: module, n: int):
/// """
/// 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.
/// """
STATIC mp_obj_t mod_trezorutils_presize_module(mp_obj_t mod, mp_obj_t n) {
if (!mp_obj_is_type(mod, &mp_type_module)) {
mp_raise_TypeError("expected module type");
}
mp_uint_t size = trezor_obj_get_uint(n);
mp_obj_dict_t *globals = mp_obj_module_get_globals(mod);
mp_obj_dict_presize(globals, size);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorutils_presize_module_obj,
mod_trezorutils_presize_module);
#if !PYOPT #if !PYOPT
#if LOG_STACK_USAGE #if LOG_STACK_USAGE
/// def zero_unused_stack() -> None: /// def zero_unused_stack() -> None:
@ -601,6 +621,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
#endif #endif
{MP_ROM_QSTR(MP_QSTR_sd_hotswap_enabled), {MP_ROM_QSTR(MP_QSTR_sd_hotswap_enabled),
MP_ROM_PTR(&mod_trezorutils_sd_hotswap_enabled_obj)}, MP_ROM_PTR(&mod_trezorutils_sd_hotswap_enabled_obj)},
{MP_ROM_QSTR(MP_QSTR_presize_module),
MP_ROM_PTR(&mod_trezorutils_presize_module_obj)},
// various built-in constants // various built-in constants
{MP_ROM_QSTR(MP_QSTR_SCM_REVISION), {MP_ROM_QSTR(MP_QSTR_SCM_REVISION),
MP_ROM_PTR(&mod_trezorutils_revision_obj)}, MP_ROM_PTR(&mod_trezorutils_revision_obj)},

View File

@ -798,38 +798,6 @@ Q(Y)
Q(z) Q(z)
Q(Z) Q(Z)
// generate module presizing identifiers
Q(___PRESIZE_MODULE_0)
Q(___PRESIZE_MODULE_1)
Q(___PRESIZE_MODULE_2)
Q(___PRESIZE_MODULE_3)
Q(___PRESIZE_MODULE_4)
Q(___PRESIZE_MODULE_5)
Q(___PRESIZE_MODULE_6)
Q(___PRESIZE_MODULE_7)
Q(___PRESIZE_MODULE_8)
Q(___PRESIZE_MODULE_9)
Q(___PRESIZE_MODULE_10)
Q(___PRESIZE_MODULE_11)
Q(___PRESIZE_MODULE_12)
Q(___PRESIZE_MODULE_13)
Q(___PRESIZE_MODULE_14)
Q(___PRESIZE_MODULE_15)
Q(___PRESIZE_MODULE_16)
Q(___PRESIZE_MODULE_17)
Q(___PRESIZE_MODULE_18)
Q(___PRESIZE_MODULE_19)
Q(___PRESIZE_MODULE_20)
Q(___PRESIZE_MODULE_21)
Q(___PRESIZE_MODULE_22)
Q(___PRESIZE_MODULE_23)
Q(___PRESIZE_MODULE_24)
Q(___PRESIZE_MODULE_25)
Q(___PRESIZE_MODULE_26)
Q(___PRESIZE_MODULE_27)
Q(___PRESIZE_MODULE_28)
Q(___PRESIZE_MODULE_29)
Q()) Q())
Q(() Q(()
Q(<) Q(<)

View File

@ -108,11 +108,6 @@ Q(${letter})
Q(${letter.upper()}) Q(${letter.upper()})
% endfor % endfor
// generate module presizing identifiers
% for i in range(30):
Q(___PRESIZE_MODULE_${i})
% endfor
Q()) Q())
Q(() Q(()
Q(<) Q(<)

View File

@ -87,6 +87,17 @@ def sd_hotswap_enabled() -> bool:
""" """
# upymod/modtrezorutils/modtrezorutils.c
def presize_module(mod: module, n: int):
"""
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.
"""
# upymod/modtrezorutils/modtrezorutils.c # upymod/modtrezorutils/modtrezorutils.c
def zero_unused_stack() -> None: def zero_unused_stack() -> None:
""" """

View File

@ -11,13 +11,13 @@ import trezor
# trezor.utils import only C modules # trezor.utils import only C modules
from trezor import utils from trezor import utils
# we need space for 30 items in the trezor module # we need space for 30 items in the trezor module
utils.presize_module("trezor", 30) utils.presize_module(trezor, 30)
# storage imports storage.common, storage.cache and storage.device. # storage imports storage.common, storage.cache and storage.device.
# These import trezor, trezor.config (which is a C module), trezor.utils, and each other. # These import trezor, trezor.config (which is a C module), trezor.utils, and each other.
import storage import storage
# we will need space for 12 items in the storage module # we will need space for 12 items in the storage module
utils.presize_module("storage", 12) utils.presize_module(storage, 12)
if not utils.BITCOIN_ONLY: if not utils.BITCOIN_ONLY:
# storage.fido2 only imports C modules # storage.fido2 only imports C modules

View File

@ -28,6 +28,7 @@ from trezorutils import ( # noqa: F401
firmware_vendor, firmware_vendor,
halt, halt,
memcpy, memcpy,
presize_module,
reboot_to_bootloader, reboot_to_bootloader,
sd_hotswap_enabled, sd_hotswap_enabled,
unit_btconly, unit_btconly,
@ -114,20 +115,6 @@ class unimport:
self.free_heap = check_free_heap(self.free_heap) self.free_heap = check_free_heap(self.free_heap)
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, f"___PRESIZE_MODULE_{i}", None)
for i in range(size):
delattr(module, f"___PRESIZE_MODULE_{i}")
if __debug__: if __debug__:
from ubinascii import hexlify from ubinascii import hexlify

2
vendor/micropython vendored

@ -1 +1 @@
Subproject commit 476c569f3512f634b7f2e356d754bc17e5348d10 Subproject commit b989de647c772e2bf8d604fdc47eb604f5a7ad50