1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-09 15:00:58 +00:00

fixup! fixup! refactor(core): abstract cache and context [no changelog]

This commit is contained in:
M1nd3r 2024-11-22 11:22:41 +01:00
parent cebd147d76
commit 0a878b7558
3 changed files with 9 additions and 9 deletions

View File

@ -4,18 +4,10 @@ import gc
from storage import cache_codec from storage import cache_codec
from storage.cache_common import SESSIONLESS_FLAG, SessionlessCache from storage.cache_common import SESSIONLESS_FLAG, SessionlessCache
# XXX
# Allocation notes:
# Instantiation of a DataCache subclass should make as little garbage as possible, so
# that the preallocated bytearrays are compact in memory.
# That is why the initialization is two-step: first create appropriately sized
# bytearrays, then later call `clear()` on all the existing objects, which resets them
# to zero length. This is producing some trash - `b[:]` allocates a slice.
# Cache initialization
_SESSIONLESS_CACHE = SessionlessCache() _SESSIONLESS_CACHE = SessionlessCache()
_PROTOCOL_CACHE = cache_codec _PROTOCOL_CACHE = cache_codec
_PROTOCOL_CACHE.initialize() _PROTOCOL_CACHE.initialize()
_SESSIONLESS_CACHE.clear() _SESSIONLESS_CACHE.clear()

View File

@ -63,6 +63,13 @@ _SESSIONS: list[SessionCache] = []
def initialize() -> None: def initialize() -> None:
# Allocation notes:
# Instantiation of any DataCache subclass should make as little garbage
# as possible so that the preallocated bytearrays are compact in memory.
# That is why the initialization is two-step: first, create appropriately
# sized bytearrays, then call `clear()` on all existing objects, which
# resets them to zero length. The `clear()` function uses `arr[:]`, which
# allocates a slice.
global _SESSIONS global _SESSIONS
for _ in range(_MAX_SESSIONS_COUNT): for _ in range(_MAX_SESSIONS_COUNT):
_SESSIONS.append(SessionCache()) _SESSIONS.append(SessionCache())

View File

@ -103,6 +103,7 @@ class DataCache:
def delete(self, key: int) -> None: def delete(self, key: int) -> None:
utils.ensure(key < len(self.fields)) utils.ensure(key < len(self.fields))
# `arr[:]` allocates a slice to prevent memory fragmentation.
self.data[key][:] = b"\x00" self.data[key][:] = b"\x00"
def clear(self) -> None: def clear(self) -> None: