Skip to content

Commit d3b39be

Browse files
committed
refactor: some performance tweaks
1 parent 21f9910 commit d3b39be

File tree

7 files changed

+162
-132
lines changed

7 files changed

+162
-132
lines changed

packages/core/src/robotcode/core/ignore_spec.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def iter_files(
288288
include_hidden: bool = True,
289289
parent_spec: Optional[IgnoreSpec] = None,
290290
verbose_callback: Optional[Callable[[str], None]] = None,
291+
verbose_trace: bool = False,
291292
) -> Iterator[Path]:
292293
if isinstance(paths, Path):
293294
paths = [paths]
@@ -300,6 +301,7 @@ def iter_files(
300301
include_hidden=include_hidden,
301302
parent_spec=parent_spec,
302303
verbose_callback=verbose_callback,
304+
verbose_trace=verbose_trace,
303305
)
304306

305307

@@ -310,7 +312,10 @@ def _iter_files(
310312
include_hidden: bool = True,
311313
parent_spec: Optional[IgnoreSpec] = None,
312314
verbose_callback: Optional[Callable[[str], None]] = None,
315+
verbose_trace: bool = False,
313316
) -> Iterator[Path]:
317+
if verbose_callback is not None and verbose_trace:
318+
verbose_callback(f"iter_files: {path}")
314319

315320
if root is None:
316321
root = path if path.is_dir() else path.parent
@@ -368,6 +373,7 @@ def _iter_files(
368373
include_hidden=include_hidden,
369374
parent_spec=spec,
370375
verbose_callback=verbose_callback,
376+
verbose_trace=verbose_trace,
371377
)
372378
elif p.is_file():
373379
yield p

packages/language_server/src/robotcode/language_server/common/parts/diagnostics.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ class DiagnosticsResult:
6565
skipped: bool = False
6666

6767

68-
@dataclass
69-
class WorkspaceDocumentsResult:
70-
name: Optional[str]
71-
document: TextDocument
72-
73-
7468
@dataclass
7569
class DiagnosticsData:
7670
lock: RLock
@@ -158,7 +152,7 @@ def collect(
158152
@event
159153
def load_workspace_documents(
160154
sender,
161-
) -> Optional[List[WorkspaceDocumentsResult]]: ...
155+
) -> None: ...
162156

163157
@event
164158
def on_workspace_loaded(sender: Any) -> None: ...

packages/language_server/src/robotcode/language_server/robotframework/parts/inlay_hint.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ def handle_LibraryImport( # noqa: N802
329329

330330
except (SystemExit, KeyboardInterrupt):
331331
raise
332-
except BaseException as e:
333-
self._logger.exception(e)
332+
except BaseException:
334333
return None
335334

336335
arguments = library_node.get_tokens(RobotToken.ARGUMENT)
@@ -373,8 +372,7 @@ def handle_VariablesImport( # noqa: N802
373372

374373
except (SystemExit, KeyboardInterrupt):
375374
raise
376-
except BaseException as e:
377-
self._logger.exception(e)
375+
except BaseException:
378376
return None
379377

380378
arguments = library_node.get_tokens(RobotToken.ARGUMENT)

packages/language_server/src/robotcode/language_server/robotframework/parts/robot_workspace.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
from concurrent.futures import CancelledError
33
from logging import CRITICAL
4+
from pathlib import Path
45
from threading import Event
56
from typing import TYPE_CHECKING, Any, List, Optional
67

@@ -12,7 +13,6 @@
1213
from robotcode.language_server.common.parts.diagnostics import (
1314
AnalysisProgressMode,
1415
DiagnosticsMode,
15-
WorkspaceDocumentsResult,
1616
)
1717
from robotcode.language_server.robotframework.configuration import AnalysisConfig
1818
from robotcode.robot.diagnostics.library_doc import (
@@ -58,10 +58,10 @@ def on_get_analysis_progress_mode(self, sender: Any, uri: Uri) -> Optional[Analy
5858
config = self.parent.workspace.get_configuration(AnalysisConfig, uri)
5959
return config.progress_mode
6060

61-
def load_workspace_documents(self, sender: Any) -> List[WorkspaceDocumentsResult]:
61+
def load_workspace_documents(self, sender: Any) -> None:
6262
start = time.monotonic()
6363
try:
64-
result: List[WorkspaceDocumentsResult] = []
64+
result: List[Path] = []
6565

6666
for folder in self.parent.workspace.workspace_folders:
6767
config = self.parent.workspace.get_configuration(RobotCodeConfig, folder.uri)
@@ -79,10 +79,14 @@ def load_workspace_documents(self, sender: Any) -> List[WorkspaceDocumentsResult
7979
[*DEFAULT_SPEC_RULES, *(config.workspace.exclude_patterns or [])],
8080
folder.uri.to_path(),
8181
),
82+
verbose_callback=self._logger.debug,
83+
verbose_trace=False,
8284
),
8385
)
8486
)
8587

88+
result.extend(files)
89+
8690
canceled = False
8791
with self.parent.window.progress(
8892
"Load workspace", current=0, max=len(files), start=False, cancellable=False
@@ -111,13 +115,11 @@ def load_workspace_documents(self, sender: Any) -> List[WorkspaceDocumentsResult
111115
except BaseException as e:
112116
ex = e
113117
self._logger.exception(lambda: f"Can't load document {f}: {ex}", level=CRITICAL)
114-
115-
if canceled:
116-
return []
117-
118-
return result
119118
finally:
120-
self._logger.info(lambda: f"Workspace loaded {len(result)} documents in {time.monotonic() - start}s")
119+
if canceled:
120+
self._logger.info(lambda: "Workspace loading canceled")
121+
else:
122+
self._logger.info(lambda: f"Workspace loaded {len(result)} documents in {time.monotonic() - start}s")
121123

122124
@rpc_method(name="robot/cache/clear", threaded=True)
123125
def robot_cache_clear(self) -> None:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def __eq__(self, o: object) -> bool:
160160
return False
161161

162162
def __hash__(self) -> int:
163-
return hash(self.name)
163+
return hash(self.normalized_name)
164164

165165
def __str__(self) -> str:
166166
return self.name
@@ -252,7 +252,7 @@ class BuiltInVariableDefinition(VariableDefinition):
252252

253253
@single_call
254254
def __hash__(self) -> int:
255-
return hash((type(self), self.name, self.type))
255+
return hash((type(self), self.name, self.type, None, None))
256256

257257

258258
@dataclass

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

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import sys
77
import threading
8+
import time
89
import weakref
910
import zlib
1011
from abc import ABC, abstractmethod
@@ -1322,82 +1323,86 @@ def _get_variables_libdoc(
13221323
)
13231324

13241325
self._logger.debug(lambda: f"Load variables {source}{args!r}")
1325-
if meta is not None:
1326-
meta_file = Path(
1327-
self.variables_doc_cache_path,
1328-
meta.filepath_base + ".meta.json",
1329-
)
1330-
if meta_file.exists():
1331-
try:
1332-
spec_path = None
1333-
try:
1334-
saved_meta = from_json(meta_file.read_text("utf-8"), LibraryMetaData)
1335-
if saved_meta == meta:
1336-
spec_path = Path(
1337-
self.variables_doc_cache_path,
1338-
meta.filepath_base + ".spec.json",
1339-
)
1340-
return from_json(spec_path.read_text("utf-8"), VariablesDoc)
1341-
except (SystemExit, KeyboardInterrupt):
1342-
raise
1343-
except BaseException as e:
1344-
raise RuntimeError(
1345-
f"Failed to load library meta data for library {name} from {spec_path}"
1346-
) from e
1347-
except (SystemExit, KeyboardInterrupt):
1348-
raise
1349-
except BaseException as e:
1350-
self._logger.exception(e)
1351-
1352-
executor = ProcessPoolExecutor(max_workers=1, mp_context=mp.get_context("spawn"))
1353-
try:
1354-
result = executor.submit(
1355-
get_variables_doc,
1356-
name,
1357-
args,
1358-
working_dir,
1359-
base_dir,
1360-
self.get_resolvable_command_line_variables() if resolve_command_line_vars else None,
1361-
variables,
1362-
).result(LOAD_LIBRARY_TIME_OUT)
1363-
except (SystemExit, KeyboardInterrupt):
1364-
raise
1365-
except BaseException as e:
1366-
self._logger.exception(e)
1367-
raise
1368-
finally:
1369-
executor.shutdown(True)
1370-
1371-
if result.stdout:
1372-
self._logger.warning(lambda: f"stdout captured at loading variables {name}{args!r}:\n{result.stdout}")
1373-
1326+
start_time = time.monotonic()
13741327
try:
13751328
if meta is not None:
13761329
meta_file = Path(
13771330
self.variables_doc_cache_path,
13781331
meta.filepath_base + ".meta.json",
13791332
)
1380-
spec_file = Path(
1381-
self.variables_doc_cache_path,
1382-
meta.filepath_base + ".spec.json",
1383-
)
1384-
spec_file.parent.mkdir(parents=True, exist_ok=True)
1333+
if meta_file.exists():
1334+
try:
1335+
spec_path = None
1336+
try:
1337+
saved_meta = from_json(meta_file.read_text("utf-8"), LibraryMetaData)
1338+
if saved_meta == meta:
1339+
spec_path = Path(
1340+
self.variables_doc_cache_path,
1341+
meta.filepath_base + ".spec.json",
1342+
)
1343+
return from_json(spec_path.read_text("utf-8"), VariablesDoc)
1344+
except (SystemExit, KeyboardInterrupt):
1345+
raise
1346+
except BaseException as e:
1347+
raise RuntimeError(
1348+
f"Failed to load library meta data for library {name} from {spec_path}"
1349+
) from e
1350+
except (SystemExit, KeyboardInterrupt):
1351+
raise
1352+
except BaseException as e:
1353+
self._logger.exception(e)
13851354

1386-
try:
1387-
spec_file.write_text(as_json(result), "utf-8")
1388-
except (SystemExit, KeyboardInterrupt):
1389-
raise
1390-
except BaseException as e:
1391-
raise RuntimeError(f"Cannot write spec file for variables '{name}' to '{spec_file}'") from e
1392-
meta_file.write_text(as_json(meta), "utf-8")
1393-
else:
1394-
self._logger.debug(lambda: f"Skip caching variables {name}{args!r}")
1395-
except (SystemExit, KeyboardInterrupt):
1396-
raise
1397-
except BaseException as e:
1398-
self._logger.exception(e)
1355+
executor = ProcessPoolExecutor(max_workers=1, mp_context=mp.get_context("spawn"))
1356+
try:
1357+
result = executor.submit(
1358+
get_variables_doc,
1359+
name,
1360+
args,
1361+
working_dir,
1362+
base_dir,
1363+
self.get_resolvable_command_line_variables() if resolve_command_line_vars else None,
1364+
variables,
1365+
).result(LOAD_LIBRARY_TIME_OUT)
1366+
except (SystemExit, KeyboardInterrupt):
1367+
raise
1368+
except BaseException as e:
1369+
self._logger.exception(e)
1370+
raise
1371+
finally:
1372+
executor.shutdown(True)
1373+
1374+
if result.stdout:
1375+
self._logger.warning(lambda: f"stdout captured at loading variables {name}{args!r}:\n{result.stdout}")
1376+
1377+
try:
1378+
if meta is not None:
1379+
meta_file = Path(
1380+
self.variables_doc_cache_path,
1381+
meta.filepath_base + ".meta.json",
1382+
)
1383+
spec_file = Path(
1384+
self.variables_doc_cache_path,
1385+
meta.filepath_base + ".spec.json",
1386+
)
1387+
spec_file.parent.mkdir(parents=True, exist_ok=True)
13991388

1400-
return result
1389+
try:
1390+
spec_file.write_text(as_json(result), "utf-8")
1391+
except (SystemExit, KeyboardInterrupt):
1392+
raise
1393+
except BaseException as e:
1394+
raise RuntimeError(f"Cannot write spec file for variables '{name}' to '{spec_file}'") from e
1395+
meta_file.write_text(as_json(meta), "utf-8")
1396+
else:
1397+
self._logger.debug(lambda: f"Skip caching variables {name}{args!r}")
1398+
except (SystemExit, KeyboardInterrupt):
1399+
raise
1400+
except BaseException as e:
1401+
self._logger.exception(e)
1402+
1403+
return result
1404+
finally:
1405+
self._logger.debug(lambda: f"Load variables {source}{args!r} took {time.monotonic() - start_time} seconds")
14011406

14021407
@_logger.call
14031408
def get_libdoc_for_variables_import(

0 commit comments

Comments
 (0)