refactor: share one derived-view cache

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 05:59:51 +01:00
1 parent 35f335d001
commit eef13547e1
8 files changed
+79 -72

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
"""A view derived from a dict, built once and rebuilt when the dict changes."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any, Generic, TypeVar
T = TypeVar("T")
class DerivedView(Generic[T]):
"""One slot: the dict a view was built from, its size then, and the view.
The dict itself is held, never its id(): a database freed and replaced
by another at the same address got the old view back, answering from a
collection that no longer existed. A size change rebuilds too, for a
dict grown in place. Arguments after the dict reach `build` and are not
part of the key.
"""
def __init__(self, build: Callable[..., T]) -> None:
self._build = build
self._slot: tuple[dict, int, T] | None = None
def __call__(self, source: dict, *extra: Any) -> T:
slot = self._slot
if slot is not None and slot[0] is source and slot[1] == len(source):
return slot[2]
view = self._build(source, *extra)
self._slot = (source, len(source), view)
return view
def clear(self) -> None:
self._slot = None