perf(lsp): only store parsed sources for open documents (#23454)

This commit is contained in:
Nayeem Rahman 2024-04-20 02:00:03 +01:00 committed by GitHub
parent 472a370640
commit 79e6751cf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 199 additions and 118 deletions

View file

@ -1,6 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::collectors::TestCollector;
use super::definitions::TestModule;
use super::execution::TestRun;
use super::lsp_custom;
@ -12,7 +11,6 @@ use crate::lsp::documents::DocumentsFilter;
use crate::lsp::language_server::StateSnapshot;
use crate::lsp::performance::Performance;
use deno_ast::swc::visit::VisitWith;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json::json;
@ -36,6 +34,9 @@ fn as_delete_notification(uri: ModuleSpecifier) -> TestingNotification {
)
}
pub type TestServerTests =
Arc<tokio::sync::Mutex<HashMap<ModuleSpecifier, (TestModule, String)>>>;
/// The main structure which handles requests and sends notifications related
/// to the Testing API.
#[derive(Debug)]
@ -47,7 +48,7 @@ pub struct TestServer {
/// A map of run ids to test runs
runs: Arc<Mutex<HashMap<u32, TestRun>>>,
/// Tests that are discovered from a versioned document
tests: Arc<Mutex<HashMap<ModuleSpecifier, TestModule>>>,
tests: TestServerTests,
/// A channel for requesting that changes to documents be statically analyzed
/// for tests
update_channel: mpsc::UnboundedSender<Arc<StateSnapshot>>,
@ -59,8 +60,7 @@ impl TestServer {
performance: Arc<Performance>,
maybe_root_uri: Option<ModuleSpecifier>,
) -> Self {
let tests: Arc<Mutex<HashMap<ModuleSpecifier, TestModule>>> =
Arc::new(Mutex::new(HashMap::new()));
let tests = Default::default();
let (update_channel, mut update_rx) =
mpsc::unbounded_channel::<Arc<StateSnapshot>>();
@ -88,7 +88,7 @@ impl TestServer {
None => break,
Some(snapshot) => {
let mark = performance.mark("lsp.testing_update");
let mut tests = tests.lock();
let mut tests = tests.lock().await;
// we create a list of test modules we currently are tracking
// eliminating any we go over when iterating over the document
let mut keys: HashSet<ModuleSpecifier> =
@ -106,37 +106,33 @@ impl TestServer {
}
keys.remove(specifier);
let script_version = document.script_version();
let valid = if let Some(test) = tests.get(specifier) {
test.script_version == script_version
} else {
false
};
let valid =
if let Some((_, old_script_version)) = tests.get(specifier) {
old_script_version == &script_version
} else {
false
};
if !valid {
if let Some(Ok(parsed_source)) =
document.maybe_parsed_source()
{
let was_empty = tests
.remove(specifier)
.map(|tm| tm.is_empty())
.unwrap_or(true);
let mut collector = TestCollector::new(
specifier.clone(),
script_version,
parsed_source.text_info().clone(),
let was_empty = tests
.remove(specifier)
.map(|(tm, _)| tm.is_empty())
.unwrap_or(true);
let test_module = document
.maybe_test_module()
.await
.map(|tm| tm.as_ref().clone())
.unwrap_or_else(|| TestModule::new(specifier.clone()));
if !test_module.is_empty() {
client.send_test_notification(
test_module.as_replace_notification(mru.as_ref()),
);
parsed_source.module().visit_with(&mut collector);
let test_module = collector.take();
if !test_module.is_empty() {
client.send_test_notification(
test_module.as_replace_notification(mru.as_ref()),
);
} else if !was_empty {
client.send_test_notification(as_delete_notification(
specifier.clone(),
));
}
tests.insert(specifier.clone(), test_module);
} else if !was_empty {
client.send_test_notification(as_delete_notification(
specifier.clone(),
));
}
tests
.insert(specifier.clone(), (test_module, script_version));
}
}
for key in keys {
@ -205,14 +201,14 @@ impl TestServer {
}
/// A request from the client to start a test run.
pub fn run_request(
pub async fn run_request(
&self,
params: lsp_custom::TestRunRequestParams,
workspace_settings: config::WorkspaceSettings,
) -> LspResult<Option<Value>> {
let test_run =
{ TestRun::new(&params, self.tests.clone(), workspace_settings) };
let enqueued = test_run.as_enqueued();
{ TestRun::init(&params, self.tests.clone(), workspace_settings).await };
let enqueued = test_run.as_enqueued().await;
{
let mut runs = self.runs.lock();
runs.insert(params.id, test_run);