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

refactor(lsp): use deno_ast and cache swc ASTs #11780

Merged
merged 31 commits into from
Sep 7, 2021

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Aug 19, 2021

This change integrates deno_ast and lazily caches/reuses swc AST in the language server.

I've yet to come up with a benchmark I think is worthwhile to have... given that swc is already so fast this change is only noticable with very large files. I would say this isn't so much a performance improvement, but rather just a refactoring.

Closes #11345
Closes #11854

@dsherret dsherret requested a review from kitsonk August 19, 2021 20:43
cli/Cargo.toml Outdated Show resolved Hide resolved
cli/Cargo.toml Outdated Show resolved Hide resolved
cli/ast/mod.rs Show resolved Hide resolved
cli/lsp/document_source.rs Show resolved Hide resolved
cli/lsp/documents.rs Outdated Show resolved Hide resolved
@@ -91,14 +89,10 @@ pub async fn format(

/// Formats markdown (using https://github.com/dprint/dprint-plugin-markdown) and its code blocks
/// (ts/tsx, js/jsx).
fn format_markdown(
file_text: &str,
ts_config: dprint_plugin_typescript::configuration::Configuration,
Copy link
Member Author

Choose a reason for hiding this comment

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

Config objects were being needlessly passed around.

cli/tools/lint.rs Outdated Show resolved Hide resolved
@dsherret
Copy link
Member Author

@kitsonk this is in draft because of the failing build due to problems I encountered integrating, but it's good enough for a review.

(see the PR description—I shouldn't have updated swc in the crates so that I wouldn't need to bump deno_doc)

Copy link
Contributor

@kitsonk kitsonk left a comment

Choose a reason for hiding this comment

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

LGTM, while it may only appreciably impact performance with large documents, especially with the language server, we likely want to take every ms.

cli/lsp/document_source.rs Show resolved Hide resolved
cli/lsp/documents.rs Outdated Show resolved Hide resolved
@dsherret dsherret changed the title refactor(lsp): cache swc ASTs refactor(lsp): use deno_ast and cache swc ASTs Sep 3, 2021
Copy link
Member Author

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

Apologies for the massive PR, but at least it removes 700 lines of code from the CLI 🙂

cli/lsp/documents.rs Show resolved Hide resolved
@@ -488,7 +488,7 @@ fn syntax_error() {
Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]),
false,
);
assert!(out.ends_with("parse error: Expected ';', '}' or <eof> at 1:7\n2\n"));
assert!(out.ends_with("parse error: Expected ';', '}' or <eof> at 1:8\n2\n"));
Copy link
Member Author

Choose a reason for hiding this comment

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

The column in parse errors is now 1-indexed for display (matches line number and what is shown in editors).

@@ -52,7 +52,7 @@ pub struct File {
/// The resolved media type for the file.
pub media_type: MediaType,
/// The source of the file as a string.
pub source: String,
pub source: Arc<String>,
Copy link
Member Author

Choose a reason for hiding this comment

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

This PR should reduce memory usage and clones by using Arc<String> for the file text as much as possible (I may have still missed some areas). I didn't profile this or anything, but seems like something we should proactively do and is what's done in deno_ast.

Copy link
Member

Choose a reason for hiding this comment

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

That's reasonable 👍

@dsherret dsherret marked this pull request as ready for review September 6, 2021 16:50
// reuse by other processes
let (_, lint_diagnostics) =
linter.lint(specifier.to_string(), source_code.to_string())?;
let lint_diagnostics = linter.lint_with_ast(parsed_source);
Copy link
Member

Choose a reason for hiding this comment

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

Nice

Comment on lines -386 to -397
let source = language_server
.get_text_content(specifier)
.ok_or_else(|| anyhow!("Missing text content: {}", specifier))?;
let media_type = language_server
.get_media_type(specifier)
.ok_or_else(|| anyhow!("Missing media type: {}", specifier))?;
// we swallow parsed errors, as they are meaningless here.
// TODO(@kitsonk) consider caching previous code_lens results to return if
// there is a parse error to avoid issues of lenses popping in and out
if let Ok(parsed_module) =
analysis::parse_module(specifier, &source, &media_type)
{
Copy link
Member

Choose a reason for hiding this comment

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

This is way clearer

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Great to see so much code being removed

Copy link
Contributor

@kitsonk kitsonk left a comment

Choose a reason for hiding this comment

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

LGTM

cli/lsp/analysis.rs Show resolved Hide resolved
cli/lsp/documents.rs Show resolved Hide resolved
@dsherret dsherret merged commit 2c2e3ec into denoland:main Sep 7, 2021
@dsherret dsherret deleted the lsp-document-cache-parsed-module branch September 7, 2021 14:39
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.

Support class static blocks (Stage-3) lsp: cache swc ASTs
3 participants