cuda.core.utils.InMemoryProgramCache#
- class cuda.core.utils.InMemoryProgramCache(*, max_size_bytes: int | None = None)#
In-memory program cache with LRU eviction.
Suitable for single-process workflows that want to avoid disk I/O – a typical application compiles its kernels once per process and looks them up many times. Entries live only for the lifetime of the process; use
FileStreamProgramCachewhen the cache should persist across runs.Like
FileStreamProgramCache, this backend is bytes-in / bytes-out:__setitem__acceptsbytes,bytearray,memoryview, or anyObjectCode(path-backed too – the file is read at write time so the cached entry holds the binary content, not a path).__getitem__returnsbytes.- Parameters:
max_size_bytes – Optional cap on the sum of stored payload sizes. When exceeded, LRU eviction runs until the total fits.
Nonemeans unbounded. The size-only bound mirrorsFileStreamProgramCache.
Notes
Recency is updated on
__getitem__();getis the recommended lookup since the cache deliberately omits__contains__(theif key in cache: ...idiom is racy across processes; seeProgramCacheResource).Thread safety: a
threading.RLockserialises every method, so the cache can be shared across threads without external locking.Methods
- close() None#
Release backend resources.
The default implementation does nothing. Subclasses that hold long-lived state (open file handles, database connections, network sockets, …) should override this to release them.
Callers should use the context-manager form (
with cache:) or callclose()explicitly when finished, so code stays portable across backends that do hold resources.
- update(
- items: Mapping[bytes | str, bytes | bytearray | memoryview | ObjectCode] | Iterable[tuple[bytes | str, bytes | bytearray | memoryview | ObjectCode]],
- /,
Bulk
__setitem__.Accepts a mapping or an iterable of
(key, value)pairs. Each write goes through__setitem__so backend-specific value coercion (e.g. extracting bytes from anObjectCode) and size-cap enforcement run on every entry. Not transactional – a failure mid-iteration leaves earlier writes committed.