perf(server): avoid unnecessary reparses and cache navigation definitions

Introduce several changes to reduce full-document reparses, lock contention and
redundant work when handling TclOO analysis and navigation:

- Add a cheap may_contain_classes pre-check and several cursor/receiver
  heuristics so completions, signature help and tcloo definitions skip the
  expensive marker reparse when the document cannot contain useful OO info.
- Allow passing an existing parsed tree into tcloo completion/signature/definition
  helpers; update callers to use the server's cached tree when available.
- Use a thread-local parser for request-time parse_source to avoid blocking the
  shared parser during background indexing, and add navigation_state() which
  returns cached definition identities (invalidated on index generation changes).
- Add a cheap name pre-filter (_may_resolve_to) for symbol matching and only
  run class highlighting when classes may exist.

These changes reduce contention and repeated parsing, improve responsiveness for
requests during background indexing, and cache navigation definition identities.
Tests were added/updated to assert caching and non-blocking behavior.
This commit is contained in:
Christoph Brandau
2026-09-23 12:04:36 +02:00
parent d9d619c1dc
commit b2e6e9d250
10 changed files with 276 additions and 40 deletions
+26 -24
View File
@@ -60,7 +60,6 @@ from tools.navigation import (
SymbolIdentity,
call_hierarchy_identity,
call_hierarchy_items,
definition_identities,
document_highlights,
incoming_call_hierarchy,
matching_occurrences,
@@ -77,7 +76,7 @@ from tools.semantic_tokens import (
from tools.signature_help import build_signature_help
from tools.tcloo_arguments import method_signature_help
from tclint.lexer import TclSyntaxError
from tools.tcloo_completion import parse_completion_source, tcloo_completions
from tools.tcloo_completion import may_contain_classes, parse_completion_source, tcloo_completions
from tools.tcloo_symbols import class_completion_items
from tools.tcloo_navigation import tcloo_definition
from tools.tcl_command_completion import (
@@ -270,7 +269,14 @@ def on_completion(params: lsp.CompletionParams) -> lsp.CompletionList:
doc = LSP_SERVER.workspace.get_text_document(params.text_document.uri)
position = params.position
source_lines = LSP_SERVER.get_lines(doc)
oo_items = tcloo_completions(source_lines, position, LSP_SERVER.class_snapshot(doc.path))
def current_tree():
try:
return LSP_SERVER.get_tree(doc)
except TclSyntaxError:
return None
oo_items = tcloo_completions(source_lines, position, LSP_SERVER.class_snapshot(doc.path), current_tree)
if oo_items is not None:
return lsp.CompletionList(is_incomplete=False, items=oo_items)
array_items = array_element_completions(
@@ -434,15 +440,15 @@ def on_completion(params: lsp.CompletionParams) -> lsp.CompletionList:
)
def signature_help(params: lsp.SignatureHelpParams) -> lsp.SignatureHelp | None:
document = LSP_SERVER.workspace.get_text_document(params.text_document.uri)
method_help = method_signature_help(document.source, params.position, LSP_SERVER.class_snapshot(document.path))
if method_help is not None:
return method_help
try:
tree = LSP_SERVER.get_tree(document)
except TclSyntaxError:
tree = parse_completion_source(document.source)
if tree is None:
return None
method_help = method_signature_help(document.source, params.position, LSP_SERVER.class_snapshot(document.path), tree)
if method_help is not None:
return method_help
custom_signatures, custom_docs = LSP_SERVER.proc_metadata_snapshot(document.path)
@@ -522,8 +528,9 @@ def semantic_tokens(params: lsp.SemanticTokensParams):
# Reuse cached AST
tree = LSP_SERVER.get_tree(document)
classes = LSP_SERVER.class_snapshot(document.path)
hl.highlight_classes(tree, classes)
hl.highlight_methods(tree, document.source, document.uri, classes)
if may_contain_classes(document.source, classes):
hl.highlight_classes(tree, classes)
hl.highlight_methods(tree, document.source, document.uri, classes)
tree.accept(hl, recurse=True)
tokens = hl.tokens()
@@ -619,8 +626,12 @@ def goto_definition(params: lsp.DefinitionParams):
workspace = None
if workspace is not None:
document = workspace.get_text_document(params.text_document.uri)
try:
tree = LSP_SERVER.get_tree(document)
except TclSyntaxError:
tree = None # tcloo_definition repairs open delimiters itself.
target = tcloo_definition(document.source, document.uri, params.position,
LSP_SERVER.class_snapshot(document.path))
LSP_SERVER.class_snapshot(document.path), tree)
if target is not None:
return [target]
context = _navigation_context(params.text_document.uri, params.position)
@@ -633,18 +644,17 @@ def goto_definition(params: lsp.DefinitionParams):
def _navigation_context(uri: str, position: lsp.Position):
indexes = LSP_SERVER.navigation_snapshot()
indexes, definitions = LSP_SERVER.navigation_state()
filepath = str(pathlib.Path(uris.to_fs_path(uri)))
index = indexes.get(filepath)
if index is None or LSP_SERVER.index_update_pending(filepath):
document = LSP_SERVER.workspace.get_text_document(uri)
LSP_SERVER.update_poco_completion_for_file(document)
indexes = LSP_SERVER.navigation_snapshot()
indexes, definitions = LSP_SERVER.navigation_state()
index = indexes.get(filepath)
if index is None:
return None
definitions = definition_identities(indexes)
result = symbol_at_position(index, position, definitions)
if result is None:
return None
@@ -786,12 +796,8 @@ def incoming_calls(params: lsp.CallHierarchyIncomingCallsParams):
if identity is None:
return []
indexes = LSP_SERVER.navigation_snapshot()
return incoming_call_hierarchy(
identity,
indexes,
definition_identities(indexes),
)
indexes, definitions = LSP_SERVER.navigation_state()
return incoming_call_hierarchy(identity, indexes, definitions)
@LSP_SERVER.feature(lsp.CALL_HIERARCHY_OUTGOING_CALLS)
@@ -800,12 +806,8 @@ def outgoing_calls(params: lsp.CallHierarchyOutgoingCallsParams):
if identity is None:
return []
indexes = LSP_SERVER.navigation_snapshot()
return outgoing_call_hierarchy(
identity,
indexes,
definition_identities(indexes),
)
indexes, definitions = LSP_SERVER.navigation_state()
return outgoing_call_hierarchy(identity, indexes, definitions)
# **********************************************************