mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 05:54:25 +08:00
Make sure to check the module cache before loading a module (again)
This hopefully results in some performance improvements (maybe numpy?).
This commit is contained in:
@@ -91,9 +91,8 @@ def _cache_stub_file_map(version_info):
|
|||||||
def import_module_decorator(func):
|
def import_module_decorator(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(inference_state, import_names, parent_module_value, sys_path, prefer_stubs):
|
def wrapper(inference_state, import_names, parent_module_value, sys_path, prefer_stubs):
|
||||||
try:
|
python_value_set = inference_state.module_cache.get(import_names)
|
||||||
python_value_set = inference_state.module_cache.get(import_names)
|
if python_value_set is None:
|
||||||
except KeyError:
|
|
||||||
if parent_module_value is not None and parent_module_value.is_stub():
|
if parent_module_value is not None and parent_module_value.is_stub():
|
||||||
parent_module_values = parent_module_value.non_stub_value_set
|
parent_module_values = parent_module_value.non_stub_value_set
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ from jedi.plugins import plugin_manager
|
|||||||
|
|
||||||
class ModuleCache(object):
|
class ModuleCache(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._path_cache = {}
|
|
||||||
self._name_cache = {}
|
self._name_cache = {}
|
||||||
|
|
||||||
def add(self, string_names, value_set):
|
def add(self, string_names, value_set):
|
||||||
@@ -46,10 +45,7 @@ class ModuleCache(object):
|
|||||||
self._name_cache[string_names] = value_set
|
self._name_cache[string_names] = value_set
|
||||||
|
|
||||||
def get(self, string_names):
|
def get(self, string_names):
|
||||||
return self._name_cache[string_names]
|
return self._name_cache.get(string_names)
|
||||||
|
|
||||||
def get_from_path(self, path):
|
|
||||||
return self._path_cache[path]
|
|
||||||
|
|
||||||
|
|
||||||
# This memoization is needed, because otherwise we will infinitely loop on
|
# This memoization is needed, because otherwise we will infinitely loop on
|
||||||
@@ -286,6 +282,14 @@ class Importer(object):
|
|||||||
if not self.import_path or not self._infer_possible:
|
if not self.import_path or not self._infer_possible:
|
||||||
return NO_VALUES
|
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)
|
sys_path = self._sys_path_with_modifications(is_completion=False)
|
||||||
|
|
||||||
return import_module_by_names(
|
return import_module_by_names(
|
||||||
@@ -455,7 +459,7 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
|
|||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
else:
|
else:
|
||||||
module = _load_python_module(
|
module = _load_python_module(
|
||||||
inference_state, file_io_or_ns, sys_path,
|
inference_state, file_io_or_ns,
|
||||||
import_names=import_names,
|
import_names=import_names,
|
||||||
is_package=is_pkg,
|
is_package=is_pkg,
|
||||||
)
|
)
|
||||||
@@ -467,13 +471,8 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
|
|||||||
return ValueSet([module])
|
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):
|
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(
|
module_node = inference_state.parse(
|
||||||
file_io=file_io,
|
file_io=file_io,
|
||||||
cache=True,
|
cache=True,
|
||||||
@@ -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
|
here to ensure that a random path is still properly loaded into the Jedi
|
||||||
module structure.
|
module structure.
|
||||||
"""
|
"""
|
||||||
e_sys_path = inference_state.get_sys_path()
|
|
||||||
path = file_io.path
|
path = file_io.path
|
||||||
if base_names:
|
if base_names:
|
||||||
module_name = os.path.basename(path)
|
module_name = os.path.basename(path)
|
||||||
@@ -522,11 +520,11 @@ def load_module_from_path(inference_state, file_io, base_names=None):
|
|||||||
else:
|
else:
|
||||||
import_names = base_names + (module_name,)
|
import_names = base_names + (module_name,)
|
||||||
else:
|
else:
|
||||||
|
e_sys_path = inference_state.get_sys_path()
|
||||||
import_names, is_package = sys_path.transform_path_to_dotted(e_sys_path, path)
|
import_names, is_package = sys_path.transform_path_to_dotted(e_sys_path, path)
|
||||||
|
|
||||||
module = _load_python_module(
|
module = _load_python_module(
|
||||||
inference_state, file_io,
|
inference_state, file_io,
|
||||||
sys_path=e_sys_path,
|
|
||||||
import_names=import_names,
|
import_names=import_names,
|
||||||
is_package=is_package,
|
is_package=is_package,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ date.not_existing_attribute
|
|||||||
from datetime.date import today
|
from datetime.date import today
|
||||||
|
|
||||||
#! 16 import-error
|
#! 16 import-error
|
||||||
import datetime.date
|
import datetime.datetime
|
||||||
#! 7 import-error
|
#! 7 import-error
|
||||||
import not_existing_nested.date
|
import not_existing_nested.date
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
#! 7 import-error
|
#! 7 import-error
|
||||||
import not_existing_import
|
import not_existing_import2
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user