Skip to content

Commit 3d8a22d

Browse files
committed
perf(langserver): speedup Visitor and AsyncVisitor a little bit
1 parent 88ee386 commit 3d8a22d

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
// "--no-pager",
4545
//"config", "info", "list",
4646
// "analyze",
47-
"profiles", "list"
47+
// "profiles", "list"
48+
"discover", "tests", "--tags"
4849
// "."
4950
]
5051
},

packages/language_server/src/robotcode/language_server/robotframework/diagnostics/analyzer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def __init__(
7575
) -> None:
7676
from robot.parsing.model.statements import Template, TestTemplate
7777

78+
super().__init__()
79+
7880
self.model = model
7981
self.namespace = namespace
8082
self.finder = finder

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CodeActionDataBase:
2929

3030
class FindSectionsVisitor(Visitor):
3131
def __init__(self) -> None:
32+
super().__init__()
3233
self.keyword_sections: List[ast.AST] = []
3334
self.variable_sections: List[ast.AST] = []
3435
self.setting_sections: List[ast.AST] = []

packages/language_server/src/robotcode/language_server/robotframework/utils/ast_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def range_from_token(token: Token) -> Range:
120120

121121
class FirstAndLastRealStatementFinder(async_ast.Visitor):
122122
def __init__(self) -> None:
123+
super().__init__()
123124
self.first_statement: Optional[ast.AST] = None
124125
self.last_statement: Optional[ast.AST] = None
125126

packages/language_server/src/robotcode/language_server/robotframework/utils/async_ast.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ast
2-
from typing import Any, AsyncIterator, Callable, Iterator, Optional, Type, cast
2+
from typing import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Type
33

44
__all__ = ["iter_fields", "iter_child_nodes", "AsyncVisitor"]
55

@@ -47,20 +47,31 @@ async def iter_nodes(node: ast.AST) -> AsyncIterator[ast.AST]:
4747

4848

4949
class VisitorFinder:
50-
def _find_visitor(self, cls: Type[Any]) -> Optional[Callable[..., Any]]:
50+
__NOT_SET = object()
51+
52+
def __init__(self) -> None:
53+
self.__cache: Dict[Type[Any], Optional[Callable[..., Any]]] = {}
54+
55+
def __find_visitor(self, cls: Type[Any]) -> Optional[Callable[..., Any]]:
5156
if cls is ast.AST:
5257
return None
5358
method_name = "visit_" + cls.__name__
5459
if hasattr(self, method_name):
5560
method = getattr(self, method_name)
5661
if callable(method):
57-
return cast("Callable[..., Any]", method)
62+
return method # type: ignore
5863
for base in cls.__bases__:
5964
method = self._find_visitor(base)
6065
if method:
61-
return cast("Callable[..., Any]", method)
66+
return method # type: ignore
6267
return None
6368

69+
def _find_visitor(self, cls: Type[Any]) -> Optional[Callable[..., Any]]:
70+
r = self.__cache.get(cls, self.__NOT_SET)
71+
if r is self.__NOT_SET:
72+
self.__cache[cls] = r = self.__find_visitor(cls)
73+
return r # type: ignore
74+
6475

6576
class AsyncVisitor(VisitorFinder):
6677
async def visit(self, node: ast.AST) -> None:

0 commit comments

Comments
 (0)