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

lsp: Parse LSP messages on background thread - again #23122

Merged
merged 1 commit into from
Jan 14, 2025

Conversation

osiewicz
Copy link
Contributor

@osiewicz osiewicz commented Jan 14, 2025

This is a follow-up to #12640.
While profiling latency of working with a project with 8192 diagnostics I've noticed that while we're parsing the LSP messages into a generic message struct on a background thread, we can still block the main thread as the conversion between that generic message struct and the actual LSP message (for use by callback) is still happening on the main thread.
This PR significantly constrains what a message callback can use, so that it can be executed on any thread; we also send off message conversion to the background thread. In practice new callback constraints were already satisfied by all call sites, so no code outside of the lsp crate had to be adjusted.

This has improved throughput of my 8192-benchmark from 40s to send out all diagnostics after saving to ~20s. Now main thread is spending most of the time updating our diagnostics sets, which can probably be improved too.

Closes #ISSUE

Release Notes:

  • Improved app responsiveness with huge # of diagnostics.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jan 14, 2025
@osiewicz osiewicz force-pushed the parse-lsp-messages-on-background-thread branch 3 times, most recently from a9944c3 to f452cfb Compare January 14, 2025 13:04
This is a follow-up to #12640.
While profiling latency of working with a project with 8192 diagnostics I've noticed that while we're parsing the LSP messages into a generic message struct on a background thread, we can still block the main thread
as the conversion between that generic message struct and the actual LSP message (for use by callback) is still happening on the main thread.
This PR significantly constrains what a message callback can use, so that it can be executed on any thread; we also send off message conversion to the background thread.

This has improved throughput of my 8192-benchmark from 40s to send out all diagnostics after saving to ~20s. Now main thread is spending most of the time updating our diagnostics sets, which can probably be improved.
@osiewicz osiewicz force-pushed the parse-lsp-messages-on-background-thread branch from f452cfb to decb198 Compare January 14, 2025 13:30
@osiewicz osiewicz added this pull request to the merge queue Jan 14, 2025
Merged via the queue into main with commit 1b3b825 Jan 14, 2025
13 checks passed
@osiewicz osiewicz deleted the parse-lsp-messages-on-background-thread branch January 14, 2025 14:04
ConradIrwin added a commit that referenced this pull request Jan 17, 2025
ConradIrwin added a commit that referenced this pull request Jan 17, 2025
#23301)

This reverts commit 1b3b825.

When debugging git diffs we found that this introduced a re-ordering of
messages sent to the LSP:

* User hits "format"
* Zed adjusts spacing, and sends "spaces changed" to the LSP
* Zed sends "format" to LSP

With the async approach here, the format request can now arrive before
the space changed request.

You can reproduce this with `test_strip_whitespace_and_format_via_lsp`
under some conditions.

Release Notes:

- N/A
osiewicz added a commit that referenced this pull request Jan 17, 2025
osiewicz added a commit that referenced this pull request Jan 17, 2025
As @ConradIrwin found out in #23301 the LSP messages could be processed out of order after #23122; this PR makes our
approach to event processing a bit more conservative - instead of detaching the parsing task we'll now await them in the main loop.
This keeps the benefit of #23122 in that deserialization still happens off the main thread, but some of the performance benefit of #23122 could've been a result of the buggy interaction
that Conrad discovered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant