Skip to content

Commit 1831cab

Browse files
committed
fix doompling#5 slow diagnostics
1 parent f76d33f commit 1831cab

File tree

4 files changed

+67
-73
lines changed

4 files changed

+67
-73
lines changed

client/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export async function activate(_context: ExtensionContext) {
5454
initializationOptions: {
5555
allocationType: client_config.get("allocationType"),
5656
indexGems: client_config.get("indexGems"),
57+
reportDiagnostics: client_config.get("reportDiagnostics"),
5758
},
5859
};
5960

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
"type": "boolean",
5959
"default": true,
6060
"description": "Set if gems are included in the search index."
61+
},
62+
"fuzzyRubyServer.reportDiagnostics": {
63+
"scope": "window",
64+
"type": "boolean",
65+
"default": true,
66+
"description": "Set if diagnostics should be shown."
6167
}
6268
}
6369
}

src/main.rs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,58 +114,29 @@ impl LanguageServer for Backend {
114114
}
115115
}
116116

117-
self.client
118-
.publish_diagnostics(
119-
params.text_document.uri,
120-
diagnostics,
121-
Some(params.text_document.version),
122-
)
123-
.await;
117+
118+
if persistence.report_diagnostics {
119+
self.client
120+
.publish_diagnostics(
121+
params.text_document.uri,
122+
diagnostics,
123+
Some(params.text_document.version),
124+
)
125+
.await;
126+
}
124127
}
125128

126129
async fn did_change(&self, params: DidChangeTextDocumentParams) {
127130
let mut persistence = self.persistence.lock().await;
128-
let mut diagnostics: Vec<tower_lsp::lsp_types::Diagnostic> = vec![];
129131

130132
for content_change in &params.content_changes {
131-
let change_diagnostics =
132-
&persistence.reindex_modified_file(&content_change.text, &params.text_document.uri);
133-
134-
for diagnostic in change_diagnostics {
135-
for unwrapped_diagnostic in diagnostic {
136-
if let Some(finally_diagnostic) = unwrapped_diagnostic {
137-
diagnostics.push(finally_diagnostic.to_owned());
138-
}
139-
}
140-
}
133+
persistence.reindex_modified_file(&self.client, &content_change.text, &params.text_document.uri).await;
141134
}
142-
143-
self.client
144-
.publish_diagnostics(
145-
params.text_document.uri,
146-
diagnostics,
147-
Some(params.text_document.version),
148-
)
149-
.await;
150135
}
151136

152137
async fn did_save(&self, params: DidSaveTextDocumentParams) {
153138
let mut persistence = self.persistence.lock().await;
154-
let mut diagnostics: Vec<tower_lsp::lsp_types::Diagnostic> = vec![];
155-
let change_diagnostics =
156-
persistence.reindex_modified_file(&params.text.unwrap(), &params.text_document.uri);
157-
158-
for diagnostic in change_diagnostics {
159-
for unwrapped_diagnostic in diagnostic {
160-
if let Some(finally_diagnostic) = unwrapped_diagnostic {
161-
diagnostics.push(finally_diagnostic.to_owned());
162-
}
163-
}
164-
}
165-
166-
self.client
167-
.publish_diagnostics(params.text_document.uri, diagnostics, None)
168-
.await;
139+
persistence.reindex_modified_file(&self.client, &params.text.unwrap(), &params.text_document.uri).await;
169140
}
170141

171142
async fn did_close(&self, _: DidCloseTextDocumentParams) {

src/persistence.rs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use lib_ruby_parser::source::DecodedInput;
77
use lib_ruby_parser::{nodes::*, Node, Parser, ParserOptions, Loc};
88
use log::info;
99
use phf::phf_map;
10+
use tower_lsp::Client;
1011
use tower_lsp::lsp_types::InitializeParams;
1112
use std::collections::{HashMap, HashSet};
1213
use std::fs;
@@ -149,6 +150,7 @@ pub struct Persistence {
149150
gems_indexed: bool,
150151
index_interface_only: bool,
151152
class_scope: Vec<String>,
153+
pub report_diagnostics: bool,
152154
}
153155

154156
struct SchemaFields {
@@ -269,6 +271,7 @@ impl Persistence {
269271
let gems_indexed = false;
270272
let index_interface_only = false;
271273
let class_scope = vec![];
274+
let report_diagnostics = true;
272275

273276
Ok(Self {
274277
schema,
@@ -282,6 +285,7 @@ impl Persistence {
282285
gems_indexed,
283286
index_interface_only,
284287
class_scope,
288+
report_diagnostics,
285289
})
286290
}
287291

@@ -313,6 +317,11 @@ impl Persistence {
313317
if skip_indexing_gems {
314318
self.gems_indexed = true;
315319
}
320+
321+
let report_diagnostics = user_config.get("reportDiagnostics").unwrap().as_bool().unwrap();
322+
if !report_diagnostics {
323+
self.report_diagnostics = false;
324+
}
316325
}
317326

318327
pub fn reindex_modified_files(&mut self) -> tantivy::Result<()> {
@@ -378,7 +387,7 @@ impl Persistence {
378387
let files_deleted = self.indexed_file_paths.len() > 0;
379388

380389
if files_added || files_deleted {
381-
let mut index_writer = index.writer(100_000_000).unwrap();
390+
let mut index_writer = index.writer(256_000_000).unwrap();
382391

383392
for path in &self.indexed_file_paths {
384393
let relative_path = path.replace(&self.workspace_path, "");
@@ -470,11 +479,9 @@ impl Persistence {
470479
}
471480
};
472481

473-
let mut index_writer = index.writer(100_000_000).unwrap();
482+
let mut index_writer = index.writer(256_000_000).unwrap();
474483

475484
for gem_path in gem_paths {
476-
info!("Starting indexing gem path: {:#?}", gem_path);
477-
478485
let walk_dir = WalkDirGeneric::<(usize, bool)>::new(gem_path.clone()).process_read_dir(
479486
move |_depth, _path, _read_dir_state, children| {
480487
children.retain(|dir_entry_result| {
@@ -527,8 +534,6 @@ impl Persistence {
527534
self.reindex_modified_file_without_commit(&text, relative_path, &index_writer, false);
528535
}
529536
}
530-
531-
info!("Finished indexing gem path: {:#?}", gem_path);
532537
}
533538

534539
index_writer.commit().unwrap();
@@ -618,23 +623,43 @@ impl Persistence {
618623
}
619624
}
620625

621-
pub fn reindex_modified_file(
626+
pub async fn reindex_modified_file(
622627
&mut self,
628+
client: &Client,
623629
text: &String,
624630
uri: &Url,
625-
) -> tantivy::Result<Vec<Option<tower_lsp::lsp_types::Diagnostic>>> {
626-
if let Some(index) = &self.index {
627-
let mut index_writer = index.writer(100_000_000)?;
628-
let mut documents = Vec::new();
631+
) {
632+
let mut documents = Vec::new();
633+
let diagnostics = match self.parse(text, &mut documents) {
634+
Ok(diagnostics) => diagnostics,
635+
Err(diagnostics) => {
636+
// Return early so existing documents are not deleted when
637+
// there is a syntax error
638+
// return Ok(diagnostics);
639+
diagnostics
640+
}
641+
};
629642

630-
let diagnostics = match self.parse(text, &mut documents) {
631-
Ok(diagnostics) => diagnostics,
632-
Err(diagnostics) => {
633-
// Return early so existing documents are not deleted when
634-
// there is a syntax error
635-
return Ok(diagnostics);
643+
if self.report_diagnostics {
644+
let mut reported_diagnostics = vec![];
645+
646+
for diagnostic in &diagnostics {
647+
for unwrapped_diagnostic in diagnostic {
648+
reported_diagnostics.push(unwrapped_diagnostic.clone());
636649
}
637-
};
650+
}
651+
652+
client
653+
.publish_diagnostics(uri.clone(), reported_diagnostics, None).await;
654+
// .await;
655+
}
656+
657+
if diagnostics.len() > 0 {
658+
return;
659+
}
660+
661+
if let Some(index) = &self.index {
662+
let mut index_writer = index.writer_with_num_threads(1, 30_000_000).unwrap();
638663

639664
let user_space: bool;
640665
let relative_path: String;
@@ -700,14 +725,10 @@ impl Persistence {
700725
fuzzy_doc.add_u64(self.schema_fields.columns_field, col as u64);
701726
}
702727

703-
index_writer.add_document(fuzzy_doc)?;
728+
index_writer.add_document(fuzzy_doc).unwrap();
704729
}
705730

706-
index_writer.commit()?;
707-
708-
Ok(diagnostics)
709-
} else {
710-
Ok(vec![])
731+
index_writer.commit().unwrap();
711732
}
712733
}
713734

@@ -717,14 +738,9 @@ impl Persistence {
717738
_uri: &Url,
718739
) -> tantivy::Result<Vec<Option<tower_lsp::lsp_types::Diagnostic>>> {
719740
let mut documents = Vec::new();
720-
721-
if let Some(_) = &self.index {
722-
match self.parse(text, &mut documents) {
723-
Ok(diagnostics) => Ok(diagnostics),
724-
Err(diagnostics) => Ok(diagnostics),
725-
}
726-
} else {
727-
Ok(Vec::new())
741+
match self.parse(text, &mut documents) {
742+
Ok(diagnostics) => Ok(diagnostics),
743+
Err(diagnostics) => Ok(diagnostics),
728744
}
729745
}
730746

0 commit comments

Comments
 (0)