summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2023-05-14 21:43:14 -0400
committerJacob Walls <jacobtylerwalls@gmail.com>2023-05-15 10:09:17 -0400
commitf34070a410ede44be5b0c7ef0dba1ca4320181b6 (patch)
tree3d8d895d642f15ddcf680abef4b531c72009acb4
parent8d026e0c61cf7f445ba9f8110e8e120df0940029 (diff)
downloadastroid-git-f34070a410ede44be5b0c7ef0dba1ca4320181b6.tar.gz
Add a bound to the inference tips cache
Small bounds still yield about equal hits and misses. Further work could determine if storing only the last result is optimal.
-rw-r--r--astroid/inference_tip.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/astroid/inference_tip.py b/astroid/inference_tip.py
index 2e472437..b2ac1198 100644
--- a/astroid/inference_tip.py
+++ b/astroid/inference_tip.py
@@ -6,6 +6,7 @@
from __future__ import annotations
+from collections import OrderedDict
from collections.abc import Generator
from typing import Any, TypeVar
@@ -18,9 +19,9 @@ from astroid.typing import (
TransformFn,
)
-_cache: dict[
+_cache: OrderedDict[
tuple[InferFn[Any], NodeNG, InferenceContext | None], list[InferenceResult]
-] = {}
+] = OrderedDict()
_CURRENTLY_INFERRING: set[tuple[InferFn[Any], NodeNG]] = set()
@@ -61,7 +62,9 @@ def _inference_tip_cached(func: InferFn[_NodesT]) -> InferFn[_NodesT]:
_CURRENTLY_INFERRING.add(partial_cache_key)
try:
# May raise UseInferenceDefault
- result = _cache[func, node, context] = list(func(node, context, **kwargs))
+ result = _cache[func, node, context] = list(
+ func(node, context, **kwargs)
+ )
finally:
# Remove recursion guard.
try:
@@ -69,6 +72,9 @@ def _inference_tip_cached(func: InferFn[_NodesT]) -> InferFn[_NodesT]:
except KeyError:
pass # Recursion may beat us to the punch.
+ if len(_cache) > 64:
+ _cache.popitem(last=False)
+
yield from result
return inner