Skip to content

Commit

Permalink
Make sure to check the module cache before loading a module (again)
Browse files Browse the repository at this point in the history
This hopefully results in some performance improvements (maybe numpy?).
  • Loading branch information
davidhalter committed Jan 5, 2020
1 parent bf446f2 commit ea0972d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
5 changes: 2 additions & 3 deletions jedi/inference/gradual/typeshed.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ def _cache_stub_file_map(version_info):
def import_module_decorator(func):
@wraps(func)
def wrapper(inference_state, import_names, parent_module_value, sys_path, prefer_stubs):
try:
python_value_set = inference_state.module_cache.get(import_names)
except KeyError:
python_value_set = inference_state.module_cache.get(import_names)
if python_value_set is None:
if parent_module_value is not None and parent_module_value.is_stub():
parent_module_values = parent_module_value.non_stub_value_set
else:
Expand Down
26 changes: 12 additions & 14 deletions jedi/inference/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@

class ModuleCache(object):
def __init__(self):
self._path_cache = {}
self._name_cache = {}

def add(self, string_names, value_set):
if string_names is not None:
self._name_cache[string_names] = value_set

def get(self, string_names):
return self._name_cache[string_names]

def get_from_path(self, path):
return self._path_cache[path]
return self._name_cache.get(string_names)


# This memoization is needed, because otherwise we will infinitely loop on
Expand Down Expand Up @@ -286,6 +282,14 @@ def follow(self):
if not self.import_path or not self._infer_possible:
return NO_VALUES

# Check caches first
from_cache = self._inference_state.stub_module_cache.get(self._str_import_path)
if from_cache is not None:
return ValueSet({from_cache})
from_cache = self._inference_state.module_cache.get(self._str_import_path)
if from_cache is not None:
return from_cache

sys_path = self._sys_path_with_modifications(is_completion=False)

return import_module_by_names(
Expand Down Expand Up @@ -455,7 +459,7 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
return NO_VALUES
else:
module = _load_python_module(
inference_state, file_io_or_ns, sys_path,
inference_state, file_io_or_ns,
import_names=import_names,
is_package=is_pkg,
)
Expand All @@ -467,13 +471,8 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
return ValueSet([module])


def _load_python_module(inference_state, file_io, sys_path=None,
def _load_python_module(inference_state, file_io,
import_names=None, is_package=False):
try:
return inference_state.module_cache.get_from_path(file_io.path)
except KeyError:
pass

module_node = inference_state.parse(
file_io=file_io,
cache=True,
Expand Down Expand Up @@ -511,7 +510,6 @@ def load_module_from_path(inference_state, file_io, base_names=None):
here to ensure that a random path is still properly loaded into the Jedi
module structure.
"""
e_sys_path = inference_state.get_sys_path()
path = file_io.path
if base_names:
module_name = os.path.basename(path)
Expand All @@ -522,11 +520,11 @@ def load_module_from_path(inference_state, file_io, base_names=None):
else:
import_names = base_names + (module_name,)
else:
e_sys_path = inference_state.get_sys_path()
import_names, is_package = sys_path.transform_path_to_dotted(e_sys_path, path)

module = _load_python_module(
inference_state, file_io,
sys_path=e_sys_path,
import_names=import_names,
is_package=is_package,
)
Expand Down
2 changes: 1 addition & 1 deletion test/static_analysis/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from datetime.date import today

#! 16 import-error
import datetime.date
import datetime.datetime
#! 7 import-error
import not_existing_nested.date

Expand Down
2 changes: 1 addition & 1 deletion test/static_analysis/try_except.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
pass
try:
#! 7 import-error
import not_existing_import
import not_existing_import2
except AttributeError:
pass

Expand Down

0 comments on commit ea0972d

Please sign in to comment.