|
41 | 41 |
|
42 | 42 | __all__ = [ |
43 | 43 | "FileStreamProgramCache", |
| 44 | + "InMemoryProgramCache", |
44 | 45 | "ProgramCacheResource", |
45 | 46 | "make_program_cache_key", |
46 | 47 | ] |
@@ -871,6 +872,133 @@ def _probe(label: str, fn): |
871 | 872 | return hasher.digest() |
872 | 873 |
|
873 | 874 |
|
| 875 | +# --------------------------------------------------------------------------- |
| 876 | +# In-memory backend |
| 877 | +# --------------------------------------------------------------------------- |
| 878 | + |
| 879 | + |
| 880 | +class InMemoryProgramCache(ProgramCacheResource): |
| 881 | + """In-memory program cache with LRU eviction. |
| 882 | +
|
| 883 | + Suitable for single-process workflows that want to avoid disk I/O -- |
| 884 | + a typical application compiles its kernels once per process and |
| 885 | + looks them up many times. Entries live only for the lifetime of |
| 886 | + the process; use :class:`FileStreamProgramCache` when the cache |
| 887 | + should persist across runs. |
| 888 | +
|
| 889 | + Like :class:`FileStreamProgramCache`, this backend is bytes-in / |
| 890 | + bytes-out: ``__setitem__`` accepts ``bytes``, ``bytearray``, |
| 891 | + ``memoryview``, or any :class:`~cuda.core.ObjectCode` (path-backed |
| 892 | + too -- the file is read at write time so the cached entry holds the |
| 893 | + binary content, not a path). ``__getitem__`` returns ``bytes``. |
| 894 | +
|
| 895 | + Parameters |
| 896 | + ---------- |
| 897 | + max_size_bytes: |
| 898 | + Optional cap on the sum of stored payload sizes. When exceeded, |
| 899 | + LRU eviction runs until the total fits. ``None`` means |
| 900 | + unbounded. The size-only bound mirrors |
| 901 | + :class:`FileStreamProgramCache`. |
| 902 | +
|
| 903 | + Notes |
| 904 | + ----- |
| 905 | + Recency is updated on :meth:`__getitem__`; :meth:`__contains__` is |
| 906 | + read-only and does not shift LRU order, matching |
| 907 | + :class:`FileStreamProgramCache`. |
| 908 | +
|
| 909 | + Thread safety: a :class:`threading.RLock` serialises every method, |
| 910 | + so the cache can be shared across threads without external |
| 911 | + locking. |
| 912 | + """ |
| 913 | + |
| 914 | + def __init__( |
| 915 | + self, |
| 916 | + *, |
| 917 | + max_size_bytes: int | None = None, |
| 918 | + ) -> None: |
| 919 | + if max_size_bytes is not None and max_size_bytes < 0: |
| 920 | + raise ValueError("max_size_bytes must be non-negative or None") |
| 921 | + self._max_size_bytes = max_size_bytes |
| 922 | + # Key insertion order encodes LRU order: oldest first, newest last. |
| 923 | + # Each value is ``(payload_bytes, payload_size)``; caching the size |
| 924 | + # avoids recomputing ``len(data)`` on every eviction pass. |
| 925 | + self._entries: collections.OrderedDict[bytes, tuple[bytes, int]] = collections.OrderedDict() |
| 926 | + self._total_bytes = 0 |
| 927 | + # Reentrant so helper methods that also take the lock can nest |
| 928 | + # without deadlocking. |
| 929 | + self._lock = threading.RLock() |
| 930 | + |
| 931 | + def __getitem__(self, key: object) -> bytes: |
| 932 | + k = _as_key_bytes(key) |
| 933 | + with self._lock: |
| 934 | + try: |
| 935 | + data, _size = self._entries[k] |
| 936 | + except KeyError: |
| 937 | + raise KeyError(key) from None |
| 938 | + # Touch LRU: a real read promotes the entry to "most recent" |
| 939 | + # so eviction prefers genuinely cold entries. |
| 940 | + self._entries.move_to_end(k) |
| 941 | + return data |
| 942 | + |
| 943 | + def __setitem__( |
| 944 | + self, key: object, value: bytes | bytearray | memoryview | ObjectCode |
| 945 | + ) -> None: |
| 946 | + data = _extract_bytes(value) |
| 947 | + size = len(data) |
| 948 | + k = _as_key_bytes(key) |
| 949 | + with self._lock: |
| 950 | + existing = self._entries.pop(k, None) |
| 951 | + if existing is not None: |
| 952 | + self._total_bytes -= existing[1] |
| 953 | + self._entries[k] = (data, size) |
| 954 | + self._total_bytes += size |
| 955 | + self._evict_to_caps() |
| 956 | + |
| 957 | + def __contains__(self, key: object) -> bool: |
| 958 | + # Validate the key (mirror FileStream's behaviour: a non-str, |
| 959 | + # non-bytes key is a programming error and should surface, not |
| 960 | + # quietly report "not present"). |
| 961 | + k = _as_key_bytes(key) |
| 962 | + with self._lock: |
| 963 | + return k in self._entries |
| 964 | + |
| 965 | + def __delitem__(self, key: object) -> None: |
| 966 | + k = _as_key_bytes(key) |
| 967 | + with self._lock: |
| 968 | + try: |
| 969 | + _data, size = self._entries.pop(k) |
| 970 | + except KeyError: |
| 971 | + raise KeyError(key) from None |
| 972 | + self._total_bytes -= size |
| 973 | + |
| 974 | + def __len__(self) -> int: |
| 975 | + with self._lock: |
| 976 | + return len(self._entries) |
| 977 | + |
| 978 | + def clear(self) -> None: |
| 979 | + with self._lock: |
| 980 | + self._entries.clear() |
| 981 | + self._total_bytes = 0 |
| 982 | + |
| 983 | + # -- eviction ------------------------------------------------------------ |
| 984 | + |
| 985 | + def _evict_to_caps(self) -> None: |
| 986 | + """Evict oldest entries until the size cap is satisfied. |
| 987 | +
|
| 988 | + Called from ``__setitem__`` after an insert/update. Pops from |
| 989 | + the front of the OrderedDict (oldest first). If the |
| 990 | + just-inserted entry on its own exceeds ``max_size_bytes``, the |
| 991 | + loop will evict it too -- mirroring |
| 992 | + :class:`FileStreamProgramCache` (a write that cannot fit does |
| 993 | + not survive its own size-cap pass). |
| 994 | + """ |
| 995 | + if self._max_size_bytes is None: |
| 996 | + return |
| 997 | + while self._entries and self._total_bytes > self._max_size_bytes: |
| 998 | + _k, (_data, size) = self._entries.popitem(last=False) |
| 999 | + self._total_bytes -= size |
| 1000 | + |
| 1001 | + |
874 | 1002 | # --------------------------------------------------------------------------- |
875 | 1003 | # FileStream backend |
876 | 1004 | # --------------------------------------------------------------------------- |
|
0 commit comments