From 0a878b755824db098d9e2ece42e45d6536709291 Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Fri, 22 Nov 2024 11:22:41 +0100 Subject: [PATCH] fixup! fixup! refactor(core): abstract cache and context [no changelog] --- core/src/storage/cache.py | 10 +--------- core/src/storage/cache_codec.py | 7 +++++++ core/src/storage/cache_common.py | 1 + 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/src/storage/cache.py b/core/src/storage/cache.py index 28f17e3cac..851f3edbf3 100644 --- a/core/src/storage/cache.py +++ b/core/src/storage/cache.py @@ -4,18 +4,10 @@ import gc from storage import cache_codec 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() - _PROTOCOL_CACHE = cache_codec - _PROTOCOL_CACHE.initialize() _SESSIONLESS_CACHE.clear() diff --git a/core/src/storage/cache_codec.py b/core/src/storage/cache_codec.py index ac5bb7d4d2..7be5989032 100644 --- a/core/src/storage/cache_codec.py +++ b/core/src/storage/cache_codec.py @@ -63,6 +63,13 @@ _SESSIONS: list[SessionCache] = [] 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 for _ in range(_MAX_SESSIONS_COUNT): _SESSIONS.append(SessionCache()) diff --git a/core/src/storage/cache_common.py b/core/src/storage/cache_common.py index 01145cc8c7..90cead81db 100644 --- a/core/src/storage/cache_common.py +++ b/core/src/storage/cache_common.py @@ -103,6 +103,7 @@ class DataCache: def delete(self, key: int) -> None: utils.ensure(key < len(self.fields)) + # `arr[:]` allocates a slice to prevent memory fragmentation. self.data[key][:] = b"\x00" def clear(self) -> None: