Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DocumentSyncListener more efficient if no server is running #2532

Merged
merged 4 commits into from
Oct 20, 2024

Conversation

jwortmann
Copy link
Member

@jwortmann jwortmann commented Oct 16, 2024

Currently if LSP (this package) is installed, it runs quite some code in the background whenever the callbacks from DocumentSyncListener, which is a subclass of sublime.ViewEventListener, are triggered. This happens for all views, not only when a server is active. For example it creates an empty (if no server is running) sublime.CompletionList and schedules to run more code on the async thread in

LSP/plugin/documents.py

Lines 555 to 560 in a6b17a4

def on_query_completions(self, prefix: str, locations: list[int]) -> sublime.CompletionList | None:
completion_list = sublime.CompletionList()
triggered_manually = self._auto_complete_triggered_manually
self._auto_complete_triggered_manually = False # reset state for next completion popup
sublime.set_timeout_async(
lambda: self._on_query_completions_async(completion_list, locations[0], triggered_manually))

or it runs the debouncing in
def on_selection_modified_async(self) -> None:
via sublime.set_timeout_async each time when the cursor position changes.

All of this runs on the async thread, so you won't immediately experience any lag from this. But still it feels very inefficient and I wondered how it could be prevented or improved. We can't use the is_applicable classmethod for DocumentSyncListener, because if the view's syntax is changed later, an LSP session might still get attached to this view. So my idea would be to add a check in each of the ST callback methods and return early if there is no session running for this view. Does it make sense or is there a better solution?

plugin/documents.py Outdated Show resolved Hide resolved
@jwortmann jwortmann force-pushed the listeners-return-early branch from 0d5e561 to 4c37df7 Compare October 16, 2024 22:32
@jwortmann jwortmann force-pushed the listeners-return-early branch from 793abe7 to 1b5fb95 Compare October 17, 2024 14:30
@jwortmann jwortmann marked this pull request as ready for review October 17, 2024 14:39
Copy link
Member

@rchl rchl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that is a potential concern is that there could be a case where we rely on given method cleaning up stuff after session is destroyed. For example on restarting session. Though I know that there already exist issues with that so if we'll notice something not working correctly with this change included, it might not be due to it actually...

Comment on lines -58 to +61
from typing import Any, Callable, Generator, Iterable
from functools import wraps
from typing import Any, Callable, Generator, Iterable, TypeVar
from typing import cast
from typing_extensions import Concatenate, ParamSpec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some crazy type stuff that I'm not gonna invest time in learning now but I'm glad you found a way to type this properly :)

@rchl rchl merged commit e29883a into sublimelsp:main Oct 20, 2024
8 checks passed
@jwortmann jwortmann deleted the listeners-return-early branch October 21, 2024 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants