Skip to content

Commit fa83b30

Browse files
committed
perf(imports): cache get_module_spec results in ImportsManager
1 parent 795c420 commit fa83b30

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/robot/src/robotcode/robot/diagnostics/imports_manager.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ def __init__(
651651
self._library_files_cache = SimpleLRUCache(2048)
652652
self._resource_files_cache = SimpleLRUCache(2048)
653653
self._variables_files_cache = SimpleLRUCache(2048)
654+
self._module_spec_cache: Dict[str, ModuleSpec] = {}
654655

655656
self._executor_lock = RLock(default_timeout=120, name="ImportsManager._executor_lock")
656657
self._executor: Optional[ProcessPoolExecutor] = None
@@ -1187,6 +1188,16 @@ def __remove_variables_entry(
11871188
finally:
11881189
self._variables_files_cache.clear()
11891190

1191+
def _get_module_spec_cached(self, module_name: str) -> Optional[ModuleSpec]:
1192+
cached = self._module_spec_cache.get(module_name)
1193+
if cached is not None:
1194+
return cached
1195+
1196+
spec = get_module_spec(module_name)
1197+
if spec is not None:
1198+
self._module_spec_cache[module_name] = spec
1199+
return spec
1200+
11901201
def get_library_meta(
11911202
self,
11921203
name: str,
@@ -1203,7 +1214,7 @@ def get_library_meta(
12031214
if (p := Path(import_name)).exists():
12041215
result = LibraryMetaData(p.stem, None, import_name, None, True)
12051216
else:
1206-
module_spec = get_module_spec(import_name)
1217+
module_spec = self._get_module_spec_cached(import_name)
12071218
if module_spec is not None and module_spec.origin is not None:
12081219
result = LibraryMetaData(
12091220
module_spec.name,
@@ -1282,7 +1293,7 @@ def get_variables_meta(
12821293
if (p := Path(import_name)).exists():
12831294
result = LibraryMetaData(p.stem, None, import_name, None, True)
12841295
else:
1285-
module_spec = get_module_spec(import_name)
1296+
module_spec = self._get_module_spec_cached(import_name)
12861297
if module_spec is not None and module_spec.origin is not None:
12871298
result = LibraryMetaData(
12881299
module_spec.name,

0 commit comments

Comments
 (0)