Skip to content

Commit

Permalink
perf(langserver): speedup Visitor and AsyncVisitor a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Oct 15, 2023
1 parent 88ee386 commit 3d8a22d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
// "--no-pager",
//"config", "info", "list",
// "analyze",
"profiles", "list"
// "profiles", "list"
"discover", "tests", "--tags"
// "."
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(
) -> None:
from robot.parsing.model.statements import Template, TestTemplate

super().__init__()

self.model = model
self.namespace = namespace
self.finder = finder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CodeActionDataBase:

class FindSectionsVisitor(Visitor):
def __init__(self) -> None:
super().__init__()
self.keyword_sections: List[ast.AST] = []
self.variable_sections: List[ast.AST] = []
self.setting_sections: List[ast.AST] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def range_from_token(token: Token) -> Range:

class FirstAndLastRealStatementFinder(async_ast.Visitor):
def __init__(self) -> None:
super().__init__()
self.first_statement: Optional[ast.AST] = None
self.last_statement: Optional[ast.AST] = None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ast
from typing import Any, AsyncIterator, Callable, Iterator, Optional, Type, cast
from typing import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Type

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

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


class VisitorFinder:
def _find_visitor(self, cls: Type[Any]) -> Optional[Callable[..., Any]]:
__NOT_SET = object()

def __init__(self) -> None:
self.__cache: Dict[Type[Any], Optional[Callable[..., Any]]] = {}

def __find_visitor(self, cls: Type[Any]) -> Optional[Callable[..., Any]]:
if cls is ast.AST:
return None
method_name = "visit_" + cls.__name__
if hasattr(self, method_name):
method = getattr(self, method_name)
if callable(method):
return cast("Callable[..., Any]", method)
return method # type: ignore
for base in cls.__bases__:
method = self._find_visitor(base)
if method:
return cast("Callable[..., Any]", method)
return method # type: ignore
return None

def _find_visitor(self, cls: Type[Any]) -> Optional[Callable[..., Any]]:
r = self.__cache.get(cls, self.__NOT_SET)
if r is self.__NOT_SET:
self.__cache[cls] = r = self.__find_visitor(cls)
return r # type: ignore


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

0 comments on commit 3d8a22d

Please sign in to comment.