summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2018-06-04 07:08:42 -0700
committerClaudiu Popa <pcmanticore@gmail.com>2018-06-04 07:08:42 -0700
commit180aec84a2699158c52572cced82d79b443203cc (patch)
tree727353464a77c70f727396ca6e2ce8a2ac1d07ea
parentcdd9756b0f9ac270a2ca365ecde972d47a891e94 (diff)
downloadastroid-git-180aec84a2699158c52572cced82d79b443203cc.tar.gz
Add a cache for the inference tips
The inference tips were triggered each time we wanted to infer a particular node for which an inference tip was registered, but this results in them being called too often. This caching should prevent that.
-rw-r--r--astroid/__init__.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/astroid/__init__.py b/astroid/__init__.py
index 3c9c80cb..500d5a45 100644
--- a/astroid/__init__.py
+++ b/astroid/__init__.py
@@ -30,10 +30,12 @@ Main modules are:
* builder contains the class responsible to build astroid trees
"""
+import enum
+import itertools
import os
import sys
-import enum
+import wrapt
_Context = enum.Enum('Context', 'Load Store Del')
@@ -72,6 +74,21 @@ del AstroidManager
# transform utilities (filters and decorator)
+
+@wrapt.decorator
+def _inference_tip_cached(func, instance, args, kwargs, _cache={}):
+ """Cache decorator used for inference tips"""
+ node = args[0]
+ try:
+ return iter(_cache[func, node])
+ except KeyError:
+ result = func(*args, **kwargs)
+ # Need to keep an iterator around
+ original, copy = itertools.tee(result)
+ _cache[func, node] = list(copy)
+ return original
+
+
def inference_tip(infer_function, raise_on_overwrite=False):
"""Given an instance specific inference function, return a function to be
given to MANAGER.register_transform to set this inference function.
@@ -103,7 +120,7 @@ def inference_tip(infer_function, raise_on_overwrite=False):
.format(existing_inference=infer_function,
new_inference=node._explicit_inference,
node=node))
- node._explicit_inference = infer_function
+ node._explicit_inference = _inference_tip_cached(infer_function)
return node
return transform