Skip to content

Commit fda1f02

Browse files
committed
perf(analyze): optimize find unused refences (1.5x-2x faster)
1 parent b561607 commit fda1f02

File tree

2 files changed

+13
-66
lines changed

2 files changed

+13
-66
lines changed

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

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ast
22
from concurrent.futures import CancelledError
3-
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Type, cast
3+
from typing import TYPE_CHECKING, Any, Callable, Iterable, List, Optional, Type, cast
44

55
from robot.parsing.lexer.tokens import Token as RobotToken
66
from robot.parsing.model import statements
@@ -150,7 +150,7 @@ def _find_references_in_workspace(
150150
self,
151151
document: TextDocument,
152152
stop_at_first: bool,
153-
func: Callable[..., List[Location]],
153+
func: Callable[..., Iterable[Location]],
154154
*args: Any,
155155
**kwargs: Any,
156156
) -> List[Location]:
@@ -249,73 +249,44 @@ def find_variable_references_in_file(
249249
doc: TextDocument,
250250
variable: VariableDefinition,
251251
include_declaration: bool = True,
252-
) -> List[Location]:
252+
) -> Iterable[Location]:
253253
try:
254254
namespace = self.parent.documents_cache.get_namespace(doc)
255255

256-
if (
257-
variable.source
258-
and variable.source != str(doc.uri.to_path())
259-
and not any(e for e in (namespace.get_resources()).values() if e.library_doc.source == variable.source)
260-
and not any(
261-
e for e in namespace.get_variables_imports().values() if e.library_doc.source == variable.source
262-
)
263-
and not any(e for e in namespace.get_command_line_variables() if e.source == variable.source)
264-
):
265-
return []
266-
267-
result = set()
268-
if include_declaration and variable.source:
269-
result.add(Location(str(Uri.from_path(variable.source)), variable.name_range))
270-
271256
refs = namespace.get_variable_references()
272257
if variable in refs:
273-
result |= refs[variable]
258+
if include_declaration and variable.source == namespace.source:
259+
yield Location(str(Uri.from_path(variable.source)), variable.name_range)
260+
261+
yield from refs[variable]
274262

275-
return list(result)
276263
except (SystemExit, KeyboardInterrupt, CancelledError):
277264
raise
278265
except BaseException as e:
279266
self._logger.exception(e)
280267

281-
return []
282-
283268
@_logger.call
284269
def find_keyword_references_in_file(
285270
self,
286271
doc: TextDocument,
287272
kw_doc: KeywordDoc,
288-
lib_doc: Optional[LibraryDoc] = None,
289273
include_declaration: bool = True,
290-
) -> List[Location]:
274+
) -> Iterable[Location]:
291275
try:
292276
namespace = self.parent.documents_cache.get_namespace(doc)
293277

294-
if (
295-
lib_doc is not None
296-
and lib_doc.source is not None
297-
and lib_doc.source != str(doc.uri.to_path())
298-
and lib_doc not in (e.library_doc for e in (namespace.get_libraries()).values())
299-
and lib_doc not in (e.library_doc for e in (namespace.get_resources()).values())
300-
):
301-
return []
302-
303-
result = set()
304-
if include_declaration and kw_doc.source:
305-
result.add(Location(str(Uri.from_path(kw_doc.source)), kw_doc.range))
306-
307278
refs = namespace.get_keyword_references()
308279
if kw_doc in refs:
309-
result |= refs[kw_doc]
280+
if include_declaration and kw_doc.source == namespace.source:
281+
yield Location(str(Uri.from_path(kw_doc.source)), kw_doc.range)
282+
283+
yield from refs[kw_doc]
310284

311-
return list(result)
312285
except (SystemExit, KeyboardInterrupt, CancelledError):
313286
raise
314287
except BaseException as e:
315288
self._logger.exception(e)
316289

317-
return []
318-
319290
def has_cached_keyword_references(
320291
self,
321292
document: TextDocument,
@@ -346,28 +317,6 @@ def _find_keyword_references(
346317
include_declaration: bool = True,
347318
stop_at_first: bool = False,
348319
) -> List[Location]:
349-
namespace = self.parent.documents_cache.get_namespace(document)
350-
351-
lib_doc = (
352-
next(
353-
(
354-
e.library_doc
355-
for e in (namespace.get_libraries()).values()
356-
if kw_doc in e.library_doc.keywords.values()
357-
),
358-
None,
359-
)
360-
or next(
361-
(
362-
e.library_doc
363-
for e in (namespace.get_resources()).values()
364-
if kw_doc in e.library_doc.keywords.values()
365-
),
366-
None,
367-
)
368-
or namespace.get_library_doc()
369-
)
370-
371320
result = []
372321

373322
if include_declaration and kw_doc.source:
@@ -379,7 +328,6 @@ def _find_keyword_references(
379328
stop_at_first,
380329
self.find_keyword_references_in_file,
381330
kw_doc,
382-
lib_doc,
383331
False,
384332
)
385333
)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
final,
2626
)
2727

28+
from robot.utils.text import split_args_from_name_or_path
2829
from robotcode.core.concurrent import RLock, run_as_task
2930
from robotcode.core.documents_manager import DocumentsManager
3031
from robotcode.core.event import event
@@ -603,8 +604,6 @@ def clear_cache(self) -> None:
603604

604605
@_logger.call
605606
def get_command_line_variables(self) -> List[VariableDefinition]:
606-
from robot.utils.text import split_args_from_name_or_path
607-
608607
with self._command_line_variables_lock:
609608
if self._command_line_variables is None:
610609
command_line_vars: List[VariableDefinition] = []

0 commit comments

Comments
 (0)