From edc5a1b82e28017c740fd0b8ec1ea0f0e457e505 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Fri, 15 Aug 2025 02:47:08 +0000 Subject: [PATCH 1/2] feat: microoptimize `_LRUCacheWrapper._task_done_callback` --- async_lru/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/async_lru/__init__.py b/async_lru/__init__.py index 447e9cd..1562a15 100644 --- a/async_lru/__init__.py +++ b/async_lru/__init__.py @@ -172,22 +172,25 @@ def _task_done_callback( ) -> None: self.__tasks.discard(task) + cache = self.__cache + if task.cancelled(): fut.cancel() - self.__cache.pop(key, None) + cache.pop(key, None) return exc = task.exception() if exc is not None: fut.set_exception(exc) - self.__cache.pop(key, None) + cache.pop(key, None) return - cache_item = self.__cache.get(key) - if self.__ttl is not None and cache_item is not None: + cache_item = cache.get(key) + ttl = self.__ttl + if ttl is not None and cache_item is not None: loop = asyncio.get_running_loop() cache_item.later_call = loop.call_later( - self.__ttl, self.__cache.pop, key, None + ttl, cache.pop, key, None ) fut.set_result(task.result()) From 4d5e3f0e757696e851c3be3296686d227861307e Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sat, 27 Sep 2025 15:44:21 -0400 Subject: [PATCH 2/2] lint --- async_lru/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/async_lru/__init__.py b/async_lru/__init__.py index 1562a15..3c75c69 100644 --- a/async_lru/__init__.py +++ b/async_lru/__init__.py @@ -189,9 +189,7 @@ def _task_done_callback( ttl = self.__ttl if ttl is not None and cache_item is not None: loop = asyncio.get_running_loop() - cache_item.later_call = loop.call_later( - ttl, cache.pop, key, None - ) + cache_item.later_call = loop.call_later(ttl, cache.pop, key, None) fut.set_result(task.result())