Skip to content

Commit 6db10d3

Browse files
authored
refactor: prefer explicit cache over circular reference (#2)
1 parent fa3be8e commit 6db10d3

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

packages/testing/src/execution_testing/fixtures/pre_alloc_groups.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,14 @@ class GroupPreAlloc(Alloc):
193193
string or JSON format depending on the last request.
194194
"""
195195

196-
_pre_alloc_group: "PreAllocGroup" = PrivateAttr(init=False)
196+
_cached_state_root: Hash | None = PrivateAttr(None)
197197
_model_dump_cache: ModelDumpCache | None = PrivateAttr(None)
198-
_cache_miss_count: int = PrivateAttr(0)
199198

200199
def state_root(self) -> Hash:
201200
"""On pre-alloc groups, which are normally very big, we always cache."""
202-
return self._pre_alloc_group.genesis.state_root
201+
if self._cached_state_root is not None:
202+
return self._cached_state_root
203+
return super().state_root()
203204

204205
def model_dump( # type: ignore[override]
205206
self, mode: Literal["json", "python"], **kwargs: Any
@@ -219,7 +220,6 @@ def model_dump( # type: ignore[override]
219220
):
220221
return self._model_dump_cache.data
221222

222-
self._cache_miss_count += 1
223223
data = super().model_dump(mode=mode, **kwargs)
224224
self._model_dump_cache = ModelDumpCache(
225225
model_dump_mode=mode,
@@ -239,7 +239,6 @@ def model_dump_json(self, **kwargs: Any) -> str:
239239
):
240240
return self._model_dump_cache.data
241241

242-
self._cache_miss_count += 1
243242
data = super().model_dump_json(**kwargs)
244243
self._model_dump_cache = ModelDumpCache(
245244
model_dump_mode="json",
@@ -267,10 +266,10 @@ class PreAllocGroup(PreAllocGroupBuilder):
267266

268267
def model_post_init(self, __context: Any) -> None:
269268
"""
270-
Model post init method to set GroupPreAlloc reference.
269+
Model post init method to cache the state root in GroupPreAlloc.
271270
"""
272271
super().model_post_init(__context)
273-
self.pre._pre_alloc_group = self
272+
self.pre._cached_state_root = self.genesis.state_root
274273

275274
@classmethod
276275
def from_file(cls, file: Path) -> Self:

0 commit comments

Comments
 (0)