Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by caching find_spec #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@

from . import util

_spec_cache = {}


def clear_spec_cache():
_spec_cache.clear()


# The MetaPathFinder protocol comes from typeshed, which says:
# Intentionally omits one deprecated and one optional method of `importlib.abc.MetaPathFinder`
Expand Down Expand Up @@ -423,6 +429,18 @@ def _find_spec_with_path(
raise ImportError(f"No module named {'.'.join(module_parts)}")


def spec_cache(func):
def wrapper(*args):
key = ".".join(args[0])
if key not in _spec_cache:
_spec_cache[key] = func(*args)

return _spec_cache[key]

return wrapper


@spec_cache
def find_spec(modpath: list[str], path: Sequence[str] | None = None) -> ModuleSpec:
"""Find a spec for the given module.

Expand Down
2 changes: 2 additions & 0 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,12 @@ def clear_cache(self) -> None:
# pylint: disable=import-outside-toplevel
from astroid.brain.helpers import register_all_brains
from astroid.inference_tip import clear_inference_tip_cache
from astroid.interpreter._import.spec import clear_spec_cache
from astroid.interpreter.objectmodel import ObjectModel
from astroid.nodes._base_nodes import LookupMixIn
from astroid.nodes.scoped_nodes import ClassDef

clear_spec_cache()
clear_inference_tip_cache()
_invalidate_cache() # inference context cache

Expand Down
2 changes: 2 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
AttributeInferenceError,
)
from astroid.interpreter._import import util
from astroid.interpreter._import.spec import clear_spec_cache
from astroid.modutils import EXT_LIB_DIRS, module_in_path
from astroid.nodes import Const
from astroid.nodes.scoped_nodes import ClassDef, Module
Expand All @@ -41,6 +42,7 @@ class AstroidManagerTest(
):
def setUp(self) -> None:
super().setUp()
clear_spec_cache()
self.manager = test_utils.brainless_manager()

def test_ast_from_file(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from astroid import modutils
from astroid.const import PY310_PLUS
from astroid.interpreter._import import spec
from astroid.interpreter._import.spec import clear_spec_cache

from . import resources

Expand All @@ -41,6 +42,7 @@ class ModuleFileTest(unittest.TestCase):
package = "mypypa"

def tearDown(self) -> None:
clear_spec_cache()
for k in list(sys.path_importer_cache):
if "MyPyPa" in k:
del sys.path_importer_cache[k]
Expand Down
Loading